diff --git a/lib/step.js b/lib/step.js index e91239b..546c14d 100755 --- a/lib/step.js +++ b/lib/step.js @@ -26,7 +26,7 @@ SOFTWARE. // modified to fit my taste and the node.JS error handling system. function Step() { var steps = Array.prototype.slice.call(arguments), - counter, results, lock; + pending, counter, results, lock; // Define the main callback that's given as `this` to the steps. function next() { @@ -42,7 +42,7 @@ function Step() { // Get the next step to execute var fn = steps.shift(); - counter = 0; + counter = pending = 0; results = []; // Run the step in a try..catch block so exceptions don't get out of hand. @@ -64,11 +64,11 @@ function Step() { // Add a special callback generator `this.parallel()` that groups stuff. next.parallel = function () { - var i = counter; - counter++; + var index = 1 + counter++; + pending++; function check() { - if (counter === 0) { + if (pending === 0) { // When they're all done, call the callback next.apply(null, results); } @@ -76,13 +76,13 @@ function Step() { process.nextTick(check); // Ensures that check is called at least once return function () { - counter--; + pending--; // Compress the error from any result to the first argument if (arguments[0]) { results[0] = arguments[0]; } // Send the other results as arguments - results[i + 1] = arguments[1]; + results[index] = arguments[1]; if (!lock) { check(); } }; }; @@ -91,11 +91,12 @@ function Step() { next.group = function () { var localCallback = next.parallel(); var counter = 0; + var pending = 0; var result = []; var error = undefined; function check() { - if (counter === 0) { + if (pending === 0) { // When group is done, call the callback localCallback(error, result); } @@ -104,16 +105,16 @@ function Step() { // Generates a callback for the group return function () { - var i = counter; - counter++; + var index = counter++; + pending++; return function () { - counter--; + pending--; // Compress the error from any result to the first argument if (arguments[0]) { error = arguments[0]; } // Send the other results as arguments - result[i] = arguments[1]; + result[index] = arguments[1]; if (!lock) { check(); } }; }; diff --git a/test/groupTest.js b/test/groupTest.js index 81798cc..c1124ab 100644 --- a/test/groupTest.js +++ b/test/groupTest.js @@ -45,4 +45,58 @@ Step( fulfill('five'); assert.deepEqual(results, []); } +); + +// Test lock functionality with N sized groups +expect("test3: 1"); +expect("test3: 1,2,3"); +expect("test3: 2"); +Step( + function() { + return 1; + }, + function makeGroup(err, num) { + if(err) throw err; + fulfill("test3: " + num); + var group = this.group(); + + setTimeout((function(callback) { return function() { callback(null, 1); } })(group()), 100); + group()(null, 2); + setTimeout((function(callback) { return function() { callback(null, 3); } })(group()), 0); + }, + function groupResults(err, results) { + if(err) throw err; + fulfill("test3: " + results); + return 2 + }, + function terminate(err, num) { + if(err) throw err; + fulfill("test3: " + num); + } +); + +// Test lock functionality with zero sized groups +expect("test4: 1"); +expect("test4: empty array"); +expect("test4: group of zero terminated"); +expect("test4: 2"); +Step( + function() { + return 1; + }, + function makeGroup(err, num) { + if(err) throw err; + fulfill("test4: " + num); + this.group(); + }, + function groupResults(err, results) { + if(err) throw err; + if(results.length === 0) { fulfill("test4: empty array"); } + fulfill('test4: group of zero terminated'); + return 2 + }, + function terminate(err, num) { + if(err) throw err; + fulfill("test4: " + num); + } ); \ No newline at end of file diff --git a/test/parallelTest.js b/test/parallelTest.js index c579e96..6a78853 100644 --- a/test/parallelTest.js +++ b/test/parallelTest.js @@ -20,3 +20,30 @@ Step( assert.equal(etcText, users, "Users should come second"); } ); + +// Test lock functionality with N sized groups +expect("test2: 1"); +expect("test2: 1,2,3"); +expect("test2: 2"); +Step( + function() { + return 1; + }, + function makeParallelCalls(err, num) { + if(err) throw err; + fulfill("test2: " + num); + + setTimeout((function(callback) { return function() { callback(null, 1); } })(this.parallel()), 100); + this.parallel()(null, 2); + setTimeout((function(callback) { return function() { callback(null, 3); } })(this.parallel()), 0); + }, + function parallelResults(err, one, two, three) { + if(err) throw err; + fulfill("test2: " + [one, two, three]); + return 2 + }, + function terminate(err, num) { + if(err) throw err; + fulfill("test2: " + num); + } +) \ No newline at end of file