diff --git a/src/alt/store/AltStore.js b/src/alt/store/AltStore.js index 9f43e653..ecda3dd3 100644 --- a/src/alt/store/AltStore.js +++ b/src/alt/store/AltStore.js @@ -48,25 +48,36 @@ class AltStore { state: this.state }) - const actionHandler = model.actionListeners[payload.action] || - model.otherwise + const actionHandlers = model.actionListeners[payload.action] - if (actionHandler) { - const result = handleDispatch(() => { - return actionHandler.call(model, payload.data, payload.action) - }, payload) + if (actionHandlers || model.otherwise) { + let result + + if (actionHandlers) { + result = handleDispatch(() => { + return actionHandlers.filter(Boolean).every((handler) => { + return handler.call(model, payload.data, payload.action) !== false + }) + }, payload) + } else { + result = handleDispatch(() => { + return model.otherwise(payload.data, payload.action) + }, payload) + } if (result !== false && !this.preventDefault) this.emitChange() } + + if (model.reduce) { handleDispatch(() => { this.state = model.reduce(this.state, payload) }, payload) - if (!this.preventDefault) this.emitChange() } + this.lifecycle('afterEach', { payload, state: this.state diff --git a/src/alt/store/StoreMixin.js b/src/alt/store/StoreMixin.js index bde7229a..8584c03f 100644 --- a/src/alt/store/StoreMixin.js +++ b/src/alt/store/StoreMixin.js @@ -125,7 +125,8 @@ const StoreMixin = { // You can pass in the constant or the function itself const key = symbol.id ? symbol.id : symbol - this.actionListeners[key] = handler.bind(this) + this.actionListeners[key] = this.actionListeners[key] || [] + this.actionListeners[key].push(handler.bind(this)) this.boundListeners.push(key) }, diff --git a/test/index.js b/test/index.js index 7ddd460c..3bc42f2e 100644 --- a/test/index.js +++ b/test/index.js @@ -1391,6 +1391,35 @@ const tests = { myStore.listen(null) }, TypeError, 'listen expects a function') }, + + 'lots of listens'() { + const ImportKeysActions = alt.generateActions('change', 'saved') + + const call = sinon.spy() + + const BalanceClaimStore = alt.createStore(class { + constructor() { + this.bindListeners({ + onRefreshBalanceClaims: ImportKeysActions.saved, + onLoadMyAccounts: [ + ImportKeysActions.change, ImportKeysActions.saved + ] + }) + } + + onRefreshBalanceClaims() { + call() + } + + onLoadMyAccounts() { + call() + } + }) + + ImportKeysActions.saved() + + assert(call.calledTwice, 'multiple action handlers are ok') + }, } export default tests