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 5, 2015
1 parent 86db9a5 commit 255509e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
24 changes: 8 additions & 16 deletions src/alt/AltStore.js
Expand Up @@ -5,6 +5,7 @@ import * as Sym from './symbols/symbols'

const {
ALL_LISTENERS,
HANDLING_ERRORS,
LIFECYCLE,
LISTENERS,
PUBLIC_METHODS,
Expand All @@ -17,7 +18,7 @@ const EE = Symbol()
export default class AltStore {
constructor(alt, model, state, StoreModel) {
this[EE] = new EventEmitter()
this[LIFECYCLE] = {}
this[LIFECYCLE] = model[LIFECYCLE]
this[STATE_CONTAINER] = state || model

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

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

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

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

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

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

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

getEventEmitter() {
Expand All @@ -78,9 +72,7 @@ export default class AltStore {
}

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

Expand Down
12 changes: 3 additions & 9 deletions src/alt/index.js
Expand Up @@ -155,9 +155,7 @@ class Alt {

rollback() {
setAppState(this, this.serialize(this[LAST_SNAPSHOT]), (store) => {
if (store[LIFECYCLE].rollback) {
store[LIFECYCLE].rollback()
}
store[LIFECYCLE].emit('rollback')
store.emitChange()
})
}
Expand All @@ -168,9 +166,7 @@ class Alt {
: this[INIT_SNAPSHOT]

setAppState(this, this.serialize(initialSnapshot), (store) => {
if (store[LIFECYCLE].init) {
store[LIFECYCLE].init()
}
store[LIFECYCLE].emit('init')
store.emitChange()
})
}
Expand All @@ -183,9 +179,7 @@ class Alt {

bootstrap(data) {
setAppState(this, data, (store) => {
if (store[LIFECYCLE].bootstrap) {
store[LIFECYCLE].bootstrap()
}
store[LIFECYCLE].emit('bootstrap')
store.emitChange()
})
}
Expand Down
3 changes: 3 additions & 0 deletions src/alt/symbols/symbols.js
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
Expand Up @@ -29,9 +29,7 @@ export function snapshot(instance, storeNames = []) {
const storeName = storeHandle.displayName || storeHandle
const store = instance.stores[storeName]
const { config } = store.StoreModel
if (store[LIFECYCLE].snapshot) {
store[LIFECYCLE].snapshot()
}
store[LIFECYCLE].emit('snapshot')
const customSnapshot = config.onSerialize &&
config.onSerialize(store[STATE_CONTAINER])
obj[storeName] = customSnapshot ? customSnapshot : store.getState()
Expand Down
6 changes: 5 additions & 1 deletion src/alt/utils/StoreMixins.js
Expand Up @@ -2,6 +2,7 @@ import Symbol from 'es-symbol'
import {
ACTION_KEY,
ALL_LISTENERS,
HANDLING_ERRORS,
LIFECYCLE,
LISTENERS,
PUBLIC_METHODS
Expand Down Expand Up @@ -42,7 +43,10 @@ export const StoreMixinEssentials = {

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

bindAction(symbol, handler) {
Expand Down
5 changes: 3 additions & 2 deletions src/alt/utils/StoreUtils.js
@@ -1,4 +1,5 @@
import assign from 'object-assign'
import EventEmitter from 'eventemitter3'

import AltStore from '../AltStore'
import { getInternalMethods } from './AltUtils'
Expand Down Expand Up @@ -57,7 +58,7 @@ export function createStoreFromObject(alt, StoreModel, key) {

const StoreProto = {}
StoreProto[ALL_LISTENERS] = []
StoreProto[LIFECYCLE] = {}
StoreProto[LIFECYCLE] = new EventEmitter()
StoreProto[LISTENERS] = {}

assign(StoreProto, {
Expand Down Expand Up @@ -129,7 +130,7 @@ export function createStoreFromClass(alt, StoreModel, key, ...argsForClass) {
})

Store.prototype[ALL_LISTENERS] = []
Store.prototype[LIFECYCLE] = {}
Store.prototype[LIFECYCLE] = new EventEmitter()
Store.prototype[LISTENERS] = {}
Store.prototype[PUBLIC_METHODS] = {}

Expand Down

0 comments on commit 255509e

Please sign in to comment.