Skip to content

Commit

Permalink
emit: add support flux-standard-action out of the box
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Jul 23, 2015
1 parent 181860f commit 269df29
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
9 changes: 6 additions & 3 deletions README.md
Expand Up @@ -42,9 +42,11 @@ callback receives the passed in data and a `wait(actions[, cb])` function that
can be used to call other actions internally. `wait()` accepts a single action
or an array of actions and an optional callback as the final argument.

### dispatcher(event[, data])
### dispatcher(action[, data])
Call an action and execute the corresponding callback. Alias:
`dispatcher.emit(event[, data])`.
`dispatcher.emit(action[, data])`. Supports strings and
[flux-standard-action](https://github.com/acdlite/flux-standard-action)s
([example](https://github.com/yoshuawuyts/barracks/blob/master/examples/flux-standard-action.js)).

## Events
### .on('error', cb(err))
Expand Down Expand Up @@ -77,7 +79,8 @@ unsure about something.
### Can I use flux standard actions with barracks?
[Yes you can](https://github.com/yoshuawuyts/barracks/blob/master/examples/flux-standard-action.js)
use [flux standard action](https://github.com/acdlite/flux-standard-action)s
with `barracks`.
with `barracks`. Just pass in an FSA compliant object into barracks, and it
will be parsed correctly.

### Why didn't you include feature X?
An action dispatcher doesn't need a lot of features to pass a message from A to
Expand Down
25 changes: 14 additions & 11 deletions examples/flux-standard-action.js
@@ -1,22 +1,25 @@
const isFsa = require('flux-standard-action').isFSA
const barracks = require('barracks')
const assert = require('assert')

// This is an example of how to use
// `flux-standard-action` with `barracks`.
// FSA's can only have 4 properties:
// 'type', 'payload', 'error', 'meta'
// You can use any library to create FSA's.
// https://github.com/acdlite/flux-standard-action

const d = barracks()

module.exports = fsaWrap
d.on('foo', (action) => console.log(action.error || action.payload))

d.on('foo', () => console.log('something'))
d.on('bar', () => console.log('something else'))
d({
type: 'foo',
payload: { bin: 'baz' }
})
// => {bin: 'baz'}

// consume `flux standard actions`
// obj -> null
function fsaWrap (action) {
assert.ok(isFsa(action), action + ' is not a flux standard action')
d(action.type, action)
}
d({
type: 'foo',
payload: { bin: 'baz' },
error: 'oh no, something went wrong!'
})
// => 'oh no, something went wrong!'
5 changes: 5 additions & 0 deletions index.js
@@ -1,4 +1,5 @@
// const debug = require('debug')('barracks')
const isFsa = require('flux-standard-action').isFSA
const series = require('run-series')
const assert = require('assert')

Expand Down Expand Up @@ -30,6 +31,10 @@ function dispatcher () {
// execute the corresponding callback
// (str, obj?) -> prom
function emit (action, data) {
if (isFsa(action)) {
data = action
action = action.type
}
assert.ok(Array.isArray(actions[action]), 'action exists')
const fn = actions[action][0]
const stack = [action]
Expand Down
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -10,12 +10,12 @@
},
"repository": "yoshuawuyts/barracks",
"keywords": [
"dispatcher",
"flux",
"action",
"minimal",
"dispatcher",
"emitter",
"event",
"flux",
"minimal",
"react",
"react-component"
],
Expand All @@ -29,11 +29,12 @@
},
"dependencies": {
"debug": "^2.0.0",
"flux-standard-action": "^0.6.0",
"run-series": "^1.1.2"
},
"files": [
"README.md",
"LICENSE",
"README.md",
"index.js"
]
}
11 changes: 11 additions & 0 deletions test.js
Expand Up @@ -149,5 +149,16 @@ test('.emit() should throw if no error handler is bound', function (t) {
t.throws(d.bind(null, 'bin'), /unhandled 'error' event/)
})

test('.emit() should be able to handle flux standard actions', function (t) {
t.plan(2)
const d = barracks()

d.on('foo', function (data) {
t.equal(data.type, 'foo')
t.equal(data.payload, 'bar')
})
d({ type: 'foo', payload: 'bar' })
})

function noop () {}
noop()

0 comments on commit 269df29

Please sign in to comment.