From dfe5152083167827bb487305a50cc948d32fcd99 Mon Sep 17 00:00:00 2001 From: karfcz Date: Sat, 10 Jan 2015 15:01:05 +0100 Subject: [PATCH] refactor(kff.Dispatcher): dispatcher should handle sync and async actions 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). --- src/kff.Dispatcher.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/kff.Dispatcher.js b/src/kff.Dispatcher.js index 3904f61..b31f872 100644 --- a/src/kff.Dispatcher.js +++ b/src/kff.Dispatcher.js @@ -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)