Skip to content

Commit

Permalink
fixup! add done() to subs
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Jul 1, 2016
1 parent 319ef68 commit bc4bc5d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ the `DOMContentLoaded` listener.
```js
app.model({
subscriptions: {
emitWoofs: (send) => {
emitWoofs: (send, done) => {
// emit a woof every second
setInterval(() => send('print', { woof: 'meow?' }), 1000)
}
Expand All @@ -258,6 +258,8 @@ app.model({
}
})
```
`done()` is passed as the final argument so if an error occurs in a subscriber,
it can be communicated to the `onError` hook.

## FAQ
### What is an "action dispatcher"?
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function dispatcher (handlers) {
apply(ns, model.effects, effects)
}
if (!subsCalled && model.subscriptions && !opts.noSubscriptions) {
apply(ns, model.subscriptions, subscriptions, createSend)
apply(ns, model.subscriptions, subscriptions, createSend, onError)
}
})

Expand Down Expand Up @@ -188,17 +188,17 @@ function dispatcher (handlers) {
// optionally contains a namespace
// which is used to nest properties.
// (str, obj, obj, fn?) -> null
function apply (ns, source, target, createSend) {
function apply (ns, source, target, createSend, done) {
Object.keys(source).forEach(function (key) {
if (ns) {
if (!target[ns]) target[ns] = {}
target[ns][key] = source[key]
} else {
target[key] = source[key]
}
if (createSend) {
if (createSend && done) {
const send = createSend('subscription: ' + ns ? ns + ':' + key : key)
source[key](send)
source[key](send, done)
}
})
}
Expand Down
29 changes: 27 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,17 @@ tape('handlers: effects', (t) => {

tape('handlers: subscriptions', (t) => {
t.test('should be able to call', (t) => {
t.plan(8)
t.plan(9)
const store = barracks()
store.model({
namespace: 'foo',
subscriptions: {
mySub: (send, done) => {
t.pass('namespaced sub initiated')
}
}
})

store.model({
reducers: {
bar: () => t.pass('reducer called')
Expand Down Expand Up @@ -338,7 +347,22 @@ tape('handlers: subscriptions', (t) => {
store.start()
})

t.test('should be able to emit an error')
t.test('should be able to emit an error', (t) => {
t.plan(2)
const store = barracks({
onError: (err) => t.equal(err.message, 'oh no!', 'err was received')
})

store.model({
subscriptions: {
mySub: (send, done) => {
t.pass('sub initiated')
done(new Error('oh no!'))
}
}
})
store.start()
})
})

tape('hooks: onState')
Expand All @@ -347,4 +371,5 @@ tape('hooks: onAction')
tape('hooks: onError', (t) => {
t.test('should validate input types')
t.test('should have a default err handler')
t.test('should not call itself')
})

0 comments on commit bc4bc5d

Please sign in to comment.