Browse files

Added ability to pass results from one callback to another

  • Loading branch information...
1 parent 09621f5 commit ad16382660537dd9550acc66447cfe0ff3612ee6 Artem Skvira committed Jan 7, 2011
Showing with 60 additions and 12 deletions.
  1. +30 −11 index.js
  2. +30 −1 tests.js
View
41 index.js
@@ -8,8 +8,8 @@ function defaultErrorHandler(err) {
function runChain(chain, index) {
var chainIndex = index || 0,
currentElem,
- currArgs = [],
- argIndex = 0;
+ argIndex = 0,
+ currentArgs = [];
if (!chain || chain.length === 0) {
return;
@@ -24,7 +24,10 @@ function runChain(chain, index) {
function checkErrorCallback(err) {
var handler,
- msg;
+ msg,
+ args,
+ resultArgs,
+ i = 0;
if (err) {
// If error handler callback is provided, call it supplying 'err' object,
@@ -35,19 +38,35 @@ function runChain(chain, index) {
}
chainIndex += 1;
- runChain(chain, chainIndex);
- }
+ debugger;
+ args = [chain, chainIndex];
- // Add pre-defined arguments
- if (currentElem.args) {
- for (; argIndex < currentElem.args.length; argIndex++) {
- currArgs.push(currentElem.args[argIndex]);
+ // If passResultToNextStep was specified, need to pass result of previous callback
+ // to next function in chain, skipping the 'err' object
+ if (currentElem.passResultToNextStep) {
+ resultArgs = Array.prototype.splice.apply(arguments, [1, arguments.length-1]);
+ for (; i < resultArgs.length; i++) {
+ args.push(resultArgs[i]);
+ }
}
+
+ runChain.apply(null, args);
}
- currArgs.push(checkErrorCallback);
+ if (arguments.length > 2) {
+ // Looks like we were supplied agruments from previous call.
+ // Let's take away 'chain' and 'chainIndex' arguments and pass it on further
+ // down the call chain
+ currentArgs = Array.prototype.splice.apply(arguments, [2, arguments.length-2]);
+ } else {
+ // No arguments from prevous call, just use pre-defined arguments, if any
+ if (currentElem.args) {
+ currentArgs = currentElem.args.slice();
+ }
+ }
- currentElem.target.apply(this, currArgs);
+ currentArgs.push(checkErrorCallback);
+ currentElem.target.apply(this, currentArgs);
}
View
31 tests.js
@@ -150,7 +150,7 @@ chain = [
target: function(a, b, c, callback) {
test_args.push([a, b, c]);
test_stack.push(3);
- callback(null);
+ callback(null, a, b, c);
},
args: [7, 8, 9]
}
@@ -163,3 +163,32 @@ assertArraysEqual(chain[0].args, test_args[0]);
assertArraysEqual(chain[1].args, test_args[1]);
assertArraysEqual(test_stack, [1, 2]);
assertEquals(ERROR_MESSAGE, test_error);
+
+
+//
+// 5. Test passing of callback results to the next function inthe chain
+//
+test_stack = [];
+test_args = [];
+
+chain = [
+ {
+ target: function(callback) {
+ test_stack.push(1);
+ callback(null, 1, 2, 3);
+ },
+ passResultToNextStep: true
+ },
+ {
+ target: function(a, b, c) {
+ test_stack.push(2);
+ test_args.push([a, b, c]);
+ //callback(null);
+ }
+ }
+];
+
+c.runChain(chain);
+
+assertArraysEqual(test_stack, [1, 2]);
+assertArraysEqual(test_args[0], [1, 2, 3]);

0 comments on commit ad16382

Please sign in to comment.