Skip to content

Commit

Permalink
Allow straight dispatch of FSA actions
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Oct 21, 2015
1 parent feac7ba commit 243828c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/alt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ class Alt {
this.batchingFunction(() => {
const id = Math.random().toString(18).substr(2, 16)

// support straight dispatching of FSA-style actions
if (action.type && action.payload) {
const fsaDetails = {
id: action.type,
namespace: action.type,
name: action.type,
}
return this.dispatcher.dispatch(
utils.fsa(id, action.type, action.payload, fsaDetails)
)
}

if (action.id && action.dispatch) {
return utils.dispatch(id, action, data, this)
}
Expand Down
20 changes: 19 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1484,19 +1484,37 @@ const tests = {
},

'is fsa'(done) {
alt.dispatcher.register((x) => {
const res = alt.dispatcher.register((x) => {
assert.isDefined(x.type, 'there is a type')
assert.isDefined(x.payload, 'there is a payload')
assert.isDefined(x.meta, 'meta exists')
assert.isString(x.meta.dispatchId, 'meta contains a unique dispatch id')

assert(x.payload === 'Jane', 'the payload is correct')

alt.dispatcher.unregister(res)

done()
})

myActions.updateName('Jane')
},

'can dispatch fsa'(done) {
const res = alt.dispatcher.register((x) => {
assert.isDefined(x.type, 'there is a type')
assert(x.type === 'owl')
assert.isDefined(x.payload, 'there is a payload')
assert(x.payload === 'Tawny')
assert.isString(x.meta.dispatchId, 'meta contains a unique dispatch id')

alt.dispatcher.unregister(res)

done()
})

alt.dispatch({ type: 'owl', payload: 'Tawny' })
},
}

export default tests

0 comments on commit 243828c

Please sign in to comment.