Skip to content

Commit

Permalink
refactor(kff.Dispatcher): dispatcher should handle sync and async act…
Browse files Browse the repository at this point in the history
…ions as well

To determine if the action is sync, the dispatcher checks the function arity (length propetry). It should be either 1 or 2. Functions with arity 1 are considered sync and the dispatcher expect the next event to be directly returned. In case of arity 2, the function is considered async - the first argument is the event and the second argument is node-style error-first callback (err, event).
  • Loading branch information
karfcz committed Jan 10, 2015
1 parent 5589498 commit dfe5152
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/kff.Dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@ kff.Dispatcher = kff.createClass({
createCallback: function(fn)
{
var dispatcher = this;
return function(event)
if(fn.length <= 1)
{
fn.call(null, event, dispatcher);
};
return function(event)
{
var nextEvent = fn.call(null, event);
if(nextEvent) dispatcher.trigger(nextEvent.action, nextEvent);
};
}
else
{
return function(event)
{
var done = function(err, nextEvent)
{
if(err) return;
if(nextEvent) dispatcher.trigger(nextEvent.action, nextEvent);
};
fn.call(null, event, done);
};
}
},

registerActions: function(actions)
Expand Down

0 comments on commit dfe5152

Please sign in to comment.