Skip to content

Commit

Permalink
Makes the lifecycle methods eventemitters
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed May 8, 2015
1 parent 9bf17f0 commit cf226f5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
12 changes: 3 additions & 9 deletions src/alt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ class Alt {
this,
this.serialize(this[Sym.LAST_SNAPSHOT]),
storeInst => {
if (storeInst[Sym.LIFECYCLE].rollback) {
storeInst[Sym.LIFECYCLE].rollback()
}
storeInst[Sym.LIFECYCLE].emit('rollback')
storeInst.emitChange()
}
)
Expand All @@ -157,9 +155,7 @@ class Alt {
this,
this.serialize(initialSnapshot),
(storeInst) => {
if (storeInst[Sym.LIFECYCLE].init) {
storeInst[Sym.LIFECYCLE].init()
}
storeInst[Sym.LIFECYCLE].emit('init')
storeInst.emitChange()
}
)
Expand All @@ -173,9 +169,7 @@ class Alt {

bootstrap(data) {
StateFunctions.setAppState(this, data, (storeInst) => {
if (storeInst[Sym.LIFECYCLE].bootstrap) {
storeInst[Sym.LIFECYCLE].bootstrap()
}
storeInst[Sym.LIFECYCLE].emit('bootstrap')
storeInst.emitChange()
})
}
Expand Down
36 changes: 20 additions & 16 deletions src/alt/store/AltStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const EE = Symbol()
class AltStore {
constructor(alt, model, state, StoreModel) {
this[EE] = new EventEmitter()
this[Sym.LIFECYCLE] = {}
this[Sym.LIFECYCLE] = model[Sym.LIFECYCLE]
this[Sym.STATE_CONTAINER] = state || model

this._storeName = model._storeName
Expand All @@ -20,23 +20,29 @@ class AltStore {
this.StoreModel.state = assign({}, StoreModel.state)
}

assign(this[Sym.LIFECYCLE], model[Sym.LIFECYCLE])
assign(this, model[Sym.PUBLIC_METHODS])

// Register dispatcher
this.dispatchToken = alt.dispatcher.register((payload) => {
if (model[Sym.LIFECYCLE].beforeEach) {
model[Sym.LIFECYCLE].beforeEach(payload, this[Sym.STATE_CONTAINER])
}
this[Sym.LIFECYCLE].emit(
'beforeEach',
payload,
this[Sym.STATE_CONTAINER]
)

if (model[Sym.LISTENERS][payload.action]) {
let result = false

try {
result = model[Sym.LISTENERS][payload.action](payload.data)
} catch (e) {
if (this[Sym.LIFECYCLE].error) {
this[Sym.LIFECYCLE].error(e, payload, this[Sym.STATE_CONTAINER])
if (model[Sym.HANDLING_ERRORS]) {
this[Sym.LIFECYCLE].emit(
'error',
e,
payload,
this[Sym.STATE_CONTAINER]
)
} else {
throw e
}
Expand All @@ -47,14 +53,14 @@ class AltStore {
}
}

if (model[Sym.LIFECYCLE].afterEach) {
model[Sym.LIFECYCLE].afterEach(payload, this[Sym.STATE_CONTAINER])
}
this[Sym.LIFECYCLE].emit(
'afterEach',
payload,
this[Sym.STATE_CONTAINER]
)
})

if (this[Sym.LIFECYCLE].init) {
this[Sym.LIFECYCLE].init()
}
this[Sym.LIFECYCLE].emit('init')
}

getEventEmitter() {
Expand All @@ -71,9 +77,7 @@ class AltStore {
}

unlisten(cb) {
if (this[Sym.LIFECYCLE].unlisten) {
this[Sym.LIFECYCLE].unlisten()
}
this[Sym.LIFECYCLE].emit('unlisten')
this[EE].removeListener('change', cb)
}

Expand Down
5 changes: 4 additions & 1 deletion src/alt/store/StoreMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ const StoreMixin = {
},

on(lifecycleEvent, handler) {
this[Sym.LIFECYCLE][lifecycleEvent] = handler.bind(this)
if (lifecycleEvent === 'error') {
this[Sym.HANDLING_ERRORS] = true
}
this[Sym.LIFECYCLE].on(lifecycleEvent, handler.bind(this))
},

bindAction(symbol, handler) {
Expand Down
3 changes: 2 additions & 1 deletion src/alt/store/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assign from 'object-assign'
import EventEmitter from 'eventemitter3'

import * as Sym from '../symbols/symbols'
import * as utils from '../utils/AltUtils'
Expand Down Expand Up @@ -29,7 +30,7 @@ function doSetState(store, storeInstance, state) {

function createPrototype(proto, alt, key, extras) {
proto[Sym.ALL_LISTENERS] = []
proto[Sym.LIFECYCLE] = {}
proto[Sym.LIFECYCLE] = new EventEmitter()
proto[Sym.LISTENERS] = {}
proto[Sym.PUBLIC_METHODS] = {}

Expand Down
3 changes: 3 additions & 0 deletions src/alt/symbols/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const ACTION_UID = Symbol()
// store all of a store's listeners
export const ALL_LISTENERS = Symbol()

// are we handling our own errors
export const HANDLING_ERRORS = Symbol()

// initial snapshot
export const INIT_SNAPSHOT = Symbol()

Expand Down
4 changes: 1 addition & 3 deletions src/alt/utils/StateFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ export function snapshot(instance, storeNames = []) {
const storeName = storeHandle.displayName || storeHandle
const store = instance.stores[storeName]
const { config } = store.StoreModel
if (store[Sym.LIFECYCLE].snapshot) {
store[Sym.LIFECYCLE].snapshot()
}
store[Sym.LIFECYCLE].emit('snapshot')
const customSnapshot = config.onSerialize &&
config.onSerialize(store[Sym.STATE_CONTAINER])
obj[storeName] = customSnapshot ? customSnapshot : store.getState()
Expand Down

0 comments on commit cf226f5

Please sign in to comment.