Skip to content

Commit a57d062

Browse files
committed
Allow listening to same action with multiple methods
1 parent b2c0b31 commit a57d062

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

src/alt/store/AltStore.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,36 @@ class AltStore {
4848
state: this.state
4949
})
5050

51-
const actionHandler = model.actionListeners[payload.action] ||
52-
model.otherwise
51+
const actionHandlers = model.actionListeners[payload.action]
5352

54-
if (actionHandler) {
55-
const result = handleDispatch(() => {
56-
return actionHandler.call(model, payload.data, payload.action)
57-
}, payload)
53+
if (actionHandlers || model.otherwise) {
54+
let result
55+
56+
if (actionHandlers) {
57+
result = handleDispatch(() => {
58+
return actionHandlers.filter(Boolean).every((handler) => {
59+
return handler.call(model, payload.data, payload.action) !== false
60+
})
61+
}, payload)
62+
} else {
63+
result = handleDispatch(() => {
64+
return model.otherwise(payload.data, payload.action)
65+
}, payload)
66+
}
5867

5968
if (result !== false && !this.preventDefault) this.emitChange()
6069
}
6170

71+
72+
6273
if (model.reduce) {
6374
handleDispatch(() => {
6475
this.state = model.reduce(this.state, payload)
6576
}, payload)
66-
6777
if (!this.preventDefault) this.emitChange()
6878
}
6979

80+
7081
this.lifecycle('afterEach', {
7182
payload,
7283
state: this.state

src/alt/store/StoreMixin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ const StoreMixin = {
125125

126126
// You can pass in the constant or the function itself
127127
const key = symbol.id ? symbol.id : symbol
128-
this.actionListeners[key] = handler.bind(this)
128+
this.actionListeners[key] = this.actionListeners[key] || []
129+
this.actionListeners[key].push(handler.bind(this))
129130
this.boundListeners.push(key)
130131
},
131132

test/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,35 @@ const tests = {
13911391
myStore.listen(null)
13921392
}, TypeError, 'listen expects a function')
13931393
},
1394+
1395+
'lots of listens'() {
1396+
const ImportKeysActions = alt.generateActions('change', 'saved')
1397+
1398+
const call = sinon.spy()
1399+
1400+
const BalanceClaimStore = alt.createStore(class {
1401+
constructor() {
1402+
this.bindListeners({
1403+
onRefreshBalanceClaims: ImportKeysActions.saved,
1404+
onLoadMyAccounts: [
1405+
ImportKeysActions.change, ImportKeysActions.saved
1406+
]
1407+
})
1408+
}
1409+
1410+
onRefreshBalanceClaims() {
1411+
call()
1412+
}
1413+
1414+
onLoadMyAccounts() {
1415+
call()
1416+
}
1417+
})
1418+
1419+
ImportKeysActions.saved()
1420+
1421+
assert(call.calledTwice, 'multiple action handlers are ok')
1422+
},
13941423
}
13951424

13961425
export default tests

0 commit comments

Comments
 (0)