Comparing version 4.1.5 to 4.1.6
81
index.js
@@ -60,6 +60,10 @@ /** | ||
this._isDispatching = true; | ||
this._isPending = {}; | ||
this._isPending[action] = true; | ||
this._isDispatching = true; | ||
this._isHandled = {}; | ||
this._isPending = {}; | ||
this._isHandled[action] = false; | ||
this.locals = {}; | ||
@@ -89,3 +93,4 @@ this.locals.payload = payload; | ||
dispatcher.waitFor = function(actions, done) { | ||
assert(!done || 'function' == typeof done, 'Callback should be a function'); | ||
done = done || function() {}; | ||
assert('function' == typeof done, 'Callback should be a function'); | ||
@@ -95,33 +100,11 @@ if ('[object Array]' != toString.call(actions)) actions = [actions]; | ||
var arr = actions.map(function(action) { | ||
assert('string' == typeof action, '.waitFor(): requires a string or array of strings'); | ||
try { | ||
assert(!this._isPending[action], 'Circular dependency detected while waiting for \'' + action + '\''); | ||
} catch(e) { | ||
_stopDispatching.call(this); | ||
throw e; | ||
} | ||
this._isPending[action] = true; | ||
this._isHandled[action] = false; | ||
var fn = _getAction.call(this, action); | ||
var nwFn = _thunkify.call(this, fn, action); | ||
return nwFn.bind(this); | ||
var nwAction = _getAction.call(this, action); | ||
return nwAction.bind(this, this.locals.payload); | ||
}.bind(this)); | ||
debug('Waiting for ' + actions.reduce(function(prev, action, i, arr) { | ||
if (i == actions.length - 1) return prev + '\'' + action + '\''; | ||
return prev + '\'' + action + '\', '; | ||
}, '')); | ||
var nwArr = arr.concat(done.bind(this)); | ||
// Call all functions in series and set their handled | ||
// state to `true` when finished. | ||
async.series(nwArr, cb.bind(this)); | ||
function cb() { | ||
actions.forEach(function(action) { | ||
this._isHandled[action] = true; | ||
}.bind(this)); | ||
} | ||
async.series(nwArr); | ||
}; | ||
@@ -145,2 +128,39 @@ | ||
/** | ||
* Thunkify a function to properly set | ||
* `this._isHandled[action] = true` when | ||
* done executing. | ||
* | ||
* @param {Function} fn | ||
* @param {Function} isHandled | ||
* @return {Function} | ||
*/ | ||
function _thunkify(fn, action) { | ||
return function(done) { | ||
try { | ||
assert('string' == typeof action, '.waitFor(): requires a string or array of strings'); | ||
if (this._isPending[action]) { | ||
assert(this._isHandled[action], 'Circular dependency detected while waiting for \'' + action + '\''); | ||
} | ||
} catch(e) { | ||
_stopDispatching.call(this); | ||
throw e; | ||
} | ||
this._isPending[action] = true; | ||
this._isHandled[action] = false; | ||
function fin() { | ||
this._isHandled[action] = true; | ||
done(); | ||
} | ||
debug('Waiting for \'' + action + '\''); | ||
fn(this.locals.payload, fin.bind(this)); | ||
} | ||
} | ||
/** | ||
* Get the dispatched action. Traverses the call stack | ||
@@ -169,4 +189,3 @@ * recursively until a function is found. | ||
assert('function' == typeof fn, 'Action \'' + action + '\' is not registered'); | ||
this._isPending[action] = true; | ||
return fn; | ||
return fn.bind(this); | ||
} | ||
@@ -173,0 +192,0 @@ |
{ | ||
"name": "barracks", | ||
"version": "4.1.5", | ||
"version": "4.1.6", | ||
"description": "An event dispatcher for the flux architecture", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -152,8 +152,6 @@ # Barracks | ||
function listen(done) { | ||
this.waitFor(['user_add'], handleWaitfor); | ||
function handleWaitfor() { | ||
this.waitFor(['user_add'], function() { | ||
var url = 'myapi.co/me?token=' + this.locals.token; | ||
request(url, handleRequest); | ||
} | ||
}); | ||
@@ -160,0 +158,0 @@ function handleRequest(err, res) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10881
30
156
178