Browse files

Add parallel, forEachParallel

  • Loading branch information...
1 parent d3bc9e4 commit 6e8d5562dc219f1f2c0c4170b5917c0fa16f0dcf @hokaccha committed Oct 17, 2011
Showing with 152 additions and 0 deletions.
  1. +80 −0 lib/chain-tiny.js
  2. +72 −0 test/basic.js
View
80 lib/chain-tiny.js
@@ -96,6 +96,72 @@ Chain.prototype.forEach = function(fn) {
};
/**
+ * Parallel exec functions.
+ *
+ * @param {Object} obj
+ * @return {Object} chain
+ * @api public
+ */
+Chain.prototype.parallel = function(obj) {
+ var self = this;
+
+ return self.chain(function() {
+ var next = Array.prototype.slice.call(arguments).pop();
+ var count = 0;
+ var isArray = Array.isArray(obj);
+ var results = isArray ? [] : {};
+ var keys = Object.keys(obj);
+ keys.forEach(function(key) {
+ var fn = obj[key];
+ setTimeout(function() {
+ fn.call(null, function _next(err, result) {
+ if (err) {
+ next(err);
+ }
+ else {
+ results[key] = result;
+ count++;
+ if (count === keys.length) {
+ next(null, results);
+ }
+ }
+ });
+ }, 0);
+ });
+ });
+};
+
+/**
+ * Iterator function to each item in an array parallel. Array recieved before next function args.
+ *
+ * @param {Function} fn
+ * @return {Object} chain
+ * @api public
+ */
+Chain.prototype.forEachParallel = function(fn) {
+ var self = this;
+
+ return self.chain(function(ary, next) {
+ var _chain = Chain();
+ var q = [];
+ ary.forEach(function(val, i) {
+ q.push(function(_next) {
+ fn.call(null, i, val, function(err, result) {
+ _next(err, result);
+ });
+ });
+ });
+
+ Chain.parallel(q).end(next);
+ });
+};
+
+Chain.parallel = function(ary) {
+ return Chain().parallel(ary);
+
+};
+
+/**
* Iterator function to each item in an array.
*
* @param {Array} ary
@@ -109,4 +175,18 @@ Chain.forEach = function(ary, fn) {
}).forEach(fn);
};
+/**
+ * Parallel iterator function to each item in an array.
+ *
+ * @param {Array} ary
+ * @param {Function} fn
+ * @return {Object} chain
+ * @api public
+ */
+Chain.forEachParallel = function(ary, fn) {
+ return Chain(function(next) {
+ next(null, ary);
+ }).forEachParallel(fn);
+};
+
module.exports = Chain;
View
72 test/basic.js
@@ -94,5 +94,77 @@ module.exports = nodeunit.testCase({
t.equal(results[1], 'barbaz');
t.done();
});
+ },
+ 'parallel': function(t) {
+ var r = [];
+ chain(function(next) {
+ next(null, 'foo');
+ })
+ .parallel([
+ function(next) {
+ setTimeout(function() {
+ r.push(1);
+ next(null, 1);
+ }, 100);
+ },
+ function(next) {
+ setTimeout(function() {
+ r.push(2);
+ next(null, 2);
+ }, 1)
+ }
+ ])
+ .chain(function(results, next) {
+ t.deepEqual(results, [1, 2]);
+ t.deepEqual(r, [2, 1]);
+ next();
+ })
+ .end(t.done);
+ },
+ 'parallel obj': function(t) {
+ var r = [];
+ chain.parallel({
+ foo: function(next) {
+ setTimeout(function() {
+ r.push(1);
+ next(null, 1);
+ }, 100);
+ },
+ bar: function(next) {
+ setTimeout(function() {
+ r.push(2);
+ next(null, 2);
+ }, 1)
+ }
+ })
+ .chain(function(results, next) {
+ t.deepEqual(results, { foo: 1, bar: 2 });
+ t.deepEqual(r, [2, 1]);
+ next();
+ })
+ .end(t.done);
+ },
+ 'forEachParallel': function(t) {
+ chain(function(next) {
+ next(null, ['foo', 'bar']);
+ })
+ .forEachParallel(function(key, val, next) {
+ next(null, key + val);
+ })
+ .end(function(err, result) {
+ t.ok(!err);
+ t.deepEqual(result, ['0foo', '1bar']);
+ t.done();
+ });
+ },
+ 'Chain.forEachParallel': function(t) {
+ chain.forEachParallel(['foo', 'bar'], function(key, val, next) {
+ next(null, key + val);
+ })
+ .end(function(err, result) {
+ t.ok(!err);
+ t.deepEqual(result, ['0foo', '1bar']);
+ t.done();
+ });
}
});

0 comments on commit 6e8d556

Please sign in to comment.