Skip to content

Commit

Permalink
Fixed a bug with empty array of functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkwolfe committed Sep 9, 2010
1 parent c8250b5 commit 05f992e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -56,8 +56,8 @@ use the function callback as the Node callback. The above example could be rewri

### Your function results

The results of your function calls is available in second parameter of the group or chain
callback. The results is an array of the values submitted by your functions in the `done`
The results of your function calls are available in second parameter of the group or chain
callback. The results array of the values submitted by your functions in the `done`
callback, in the same order that the functions are declared.

If the `done` method is called with a `null` parameter or no parameter, then `null` or
Expand Down
32 changes: 18 additions & 14 deletions lib/groupie.js
@@ -1,14 +1,6 @@

var sys = require('sys');

exports.bind = function(scope) {
var _function = this;

return function() {
return _function.apply(scope, arguments);
}
}

/**
* Executes a group of functions concurrently, invoking a callback when all have completed
* or when an error occurs. Errors occur when an executed function throws an unhandled
Expand All @@ -27,10 +19,12 @@ exports.bind = function(scope) {
exports.group = function () {
var args = Array.prototype.slice.call(arguments);
var cb = args.pop();
var fxns = args.length > 0 && Array.isArray(args[0]) ? args.shift() : [];
var open = !fxns.length;
var fxns = args.length > 0 && Array.isArray(args[0]) ? args.shift() : null;
var open = fxns === null;
var finalized = !open;

fxns = fxns || [];

var items_left_to_execute = fxns.length;
var results = [];
var errOccurred = false;
Expand All @@ -43,17 +37,23 @@ exports.group = function () {
}
results[i] = result;
items_left_to_execute--;
if (finalized && !items_left_to_execute)
cb(null, results)
if (finalized && !items_left_to_execute) {
cb(null, results);
}
};

try {
fxn(done);
} catch (err) {
sys.puts("ERR: " + err);
done(err, results);
}
};


if (fxns.length === 0 && !open)
cb(null, []);

for ( var i = 0; i < fxns.length; i++) {
callGroupFunction(i, fxns[i]);
}
Expand Down Expand Up @@ -81,9 +81,11 @@ exports.group = function () {
exports.chain = function () {
var args = Array.prototype.slice.call(arguments);
var cb = args.pop();
var fxns = args.length > 0 && Array.isArray(args[0]) ? args.shift() : [];
var open = !fxns.length;
var fxns = args.length > 0 && Array.isArray(args[0]) ? args.shift() : null;
var open = fxns === null;
var finalized = !open;

fxns = fxns || [];

var pos = 0;
var results = [];
Expand Down Expand Up @@ -113,6 +115,8 @@ exports.chain = function () {

if (fxns.length > 0)
callNext();
else if (!open)
cb(null, []);

if (open)
return {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{ "name" : "groupie"
, "description" : "A simple flow control library for node.js for executing multiple functions as a group or in a chain, calling back when all functions have finished."
, "version" : "0.1.0"
, "version" : "0.1.1"
, "homepage" : "http://github.com/alexkwolfe/groupie"
, "author" : "Alex Wolfe <alexkwolfe@gmail.com>"
, "contributors" : []
Expand Down
8 changes: 8 additions & 0 deletions test/unit/chain_test.js
Expand Up @@ -31,6 +31,14 @@ exports.testResults = function(assert) {
});
};

exports.testNoFunctions = function(assert) {
groupie.chain([], function(err, colors) {
assert.ok(!err);
assert.same([], colors);
assert.done();
});
};

exports.testNullResultsAreNotDiscarded = function(assert) {
assert.expect(1);

Expand Down
8 changes: 8 additions & 0 deletions test/unit/group_test.js
Expand Up @@ -35,6 +35,14 @@ exports.testResults = function(assert) {
});
};

exports.testNoFunctions = function(assert) {
groupie.group([], function(err, colors) {
assert.ok(!err);
assert.same([], colors);
assert.done();
});
};

exports.testNullResultsAreNotDiscarded = function(assert) {
assert.expect(1);

Expand Down

0 comments on commit 05f992e

Please sign in to comment.