Skip to content

Commit

Permalink
Qualify name of not found action with namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
aknuds1 committed Dec 14, 2016
1 parent 8437552 commit d3ba070
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
57 changes: 31 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,49 +212,54 @@ function dispatcher (hooks) {
assert.equal(typeof caller, 'string', 'barracks._send: caller should be a string')
assert.equal(typeof cb, 'function', 'barracks._send: cb should be a function')

// validate if a namespace exists. Namespaces are delimited by ':'.
var actionName = name
if (/:/.test(name)) {
var arr = name.split(':')
var ns = arr.shift()
actionName = arr.join(':')
}

var isNamespaced = !!ns
var _reducers = isNamespaced ? reducers[ns] : reducers
var _effects = isNamespaced ? effects[ns] : effects
var reducer, effect
if (_reducers && _reducers[actionName]) {
reducer = _reducers[actionName]
} else if (_effects && _effects[actionName]) {
effect = _effects[actionName]
} else {
const qualifiedActionName = isNamespaced ? ns + ':' + actionName : actionName
throw new Error('Could not find action \'' + qualifiedActionName + '\'')
}

;(tick(function () {
var reducersCalled = false
var effectsCalled = false
var newState = xtend(_state)

if (onActionHooks.length) {
applyHook(onActionHooks, _state, data, name, caller, createSend)
}

// validate if a namespace exists. Namespaces are delimited by ':'.
var actionName = name
if (/:/.test(name)) {
var arr = name.split(':')
var ns = arr.shift()
actionName = arr.join(':')
}

var _reducers = ns ? reducers[ns] : reducers
if (_reducers && _reducers[actionName]) {
if (ns) {
var reducedState = _reducers[actionName](_state[ns], data)
if (reducer) {
if (isNamespaced) {
var reducedState = reducer(_state[ns], data)
newState[ns] = xtend(_state[ns], reducedState)
} else {
mutate(newState, reducers[actionName](_state, data))
mutate(newState, reducer(_state, data))
}
reducersCalled = true
if (onStateChangeHooks.length) {
applyHook(onStateChangeHooks, newState, data, _state, actionName, createSend)
}
_state = newState
cb(null, newState)
}

var _effects = ns ? effects[ns] : effects
if (!reducersCalled && _effects && _effects[actionName]) {
} else {
var send = createSend('effect: ' + name)
if (ns) _effects[actionName](_state[ns], data, send, cb)
else _effects[actionName](_state, data, send, cb)
effectsCalled = true
}

if (!reducersCalled && !effectsCalled) {
throw new Error('Could not find action ' + actionName)
if (ns) {
effect(_state[ns], data, send, cb)
} else {
effect(_state, data, send, cb)
}
}
}))()
}
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ tape('api: send(name, data?)', (t) => {
const send = createSend('test')
t.throws(send.bind(null, 123), /string/, 'non-string should throw')
})

t.test('should validate action name', (t) => {
t.plan(1)
const store = barracks()
const createSend = store.start()
const send = createSend('test')
t.throws(send, 'namespace:test', 'Could not find action namespace:test')
})
})

tape('api: stop()', (t) => {
Expand Down

0 comments on commit d3ba070

Please sign in to comment.