Skip to content
Browse files

Add `this.next`

  • Loading branch information...
1 parent b1e97d7 commit e11112a11b55c25a6bc05ecf6f16b67928320a9d @hokaccha committed Feb 13, 2013
Showing with 78 additions and 26 deletions.
  1. +39 −20 lib/chain-tiny.js
  2. +13 −0 test/basic.js
  3. +9 −1 test/each.js
  4. +17 −5 test/paralle.js
View
59 lib/chain-tiny.js
@@ -72,7 +72,7 @@ Chain.prototype.end = function(fn) {
}
else {
args.push(next);
- self.stack.shift().apply(null, args);
+ self.stack.shift().apply({ next: next }, args);
}
})();
};
@@ -89,21 +89,28 @@ Chain.prototype.end = function(fn) {
Chain.prototype.each = function(fn) {
var self = this;
- return self.chain(function(obj, next) {
+ return self.chain(function(obj, origNext) {
var _chain = Chain();
var isArray = Array.isArray(obj);
var results = isArray ? [] : {};
Object.keys(obj).forEach(function(key) {
var val = obj[key];
_chain.chain(function(_next) {
- fn.call(null, key, val, function(err, result) {
- isArray ? results.push(result) : results[key] = result;
+ function next(err, result) {
+ if (isArray) {
+ results.push(result);
+ }
+ else {
+ results[key] = result;
+ }
_next(err);
- });
+ }
+
+ fn.call({ next: next }, key, val, next);
});
});
_chain.end(function(err) {
- next(err, results);
+ origNext(err, results);
});
});
};
@@ -120,21 +127,30 @@ Chain.prototype.each = function(fn) {
Chain.prototype.eachParallel = function(fn) {
var self = this;
- return self.chain(function(obj, next) {
- var _chain = Chain();
+ return self.chain(function(obj, origNext) {
var isArray = Array.isArray(obj);
var q = isArray ? [] : {};
+
Object.keys(obj).forEach(function(key) {
var val = obj[key];
- var _fn = function(_next) {
- fn.call(null, key, val, function(err, result) {
+
+ function _fn(_next) {
+ function next(err, result) {
_next(err, result);
- });
- };
- isArray ? q.push(_fn) : q[key] = _fn;
+ }
+
+ fn.call({ next: next }, key, val, next);
+ }
+
+ if (isArray) {
+ q.push(_fn);
+ }
+ else {
+ q[key] = _fn;
+ }
});
- Chain.parallel(q).end(next);
+ Chain.parallel(q).end(origNext);
});
};
@@ -151,26 +167,29 @@ Chain.prototype.parallel = function(obj) {
return self.chain(function() {
var args = Array.prototype.slice.call(arguments);
- var next = args.pop();
+ var origNext = args.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];
process.nextTick(function() {
- fn.apply(null, args.concat(function _next(err, result) {
+ var fn = obj[key];
+
+ function next(err, result) {
if (err) {
- next(err);
+ origNext(err);
}
else {
results[key] = result;
count++;
if (count === keys.length) {
- next(null, results);
+ origNext(null, results);
}
}
- }));
+ }
+
+ fn.apply({ next: next }, args.concat(next));
});
});
});
View
13 test/basic.js
@@ -59,4 +59,17 @@ module.exports = nodeunit.testCase({
t.done();
});
},
+ 'this.next': function(t) {
+ chain(function(next) {
+ t.equal(next, this.next);
+ this.next(null, 'foo');
+ })
+ .chain(function(foo, next) {
+ t.equal(next, this.next);
+ next();
+ })
+ .end(function() {
+ t.done();
+ });
+ }
});
View
10 test/each.js
@@ -8,6 +8,7 @@ module.exports = nodeunit.testCase({
next(null, { foo: 'bar', hoge: 'fuga' });
})
.each(function(key, val, next) {
+ t.equal(this.next, next);
setTimeout(function() {
next(null, key + '/' + val);
}, 100);
@@ -26,6 +27,7 @@ module.exports = nodeunit.testCase({
next(null, ['foo', 'bar']);
})
.each(function(key, val, next) {
+ t.equal(this.next, next);
setTimeout(function() {
next(null, key + '/' + val);
}, 100);
@@ -44,6 +46,7 @@ module.exports = nodeunit.testCase({
next(null, { foo: 'bar', hoge: 'fuga'});
})
.eachParallel(function(key, val, next) {
+ t.equal(this.next, next);
setTimeout(function() {
next(null, key + '/' + val);
}, 100);
@@ -62,6 +65,7 @@ module.exports = nodeunit.testCase({
next(null, ['foo', 'bar']);
})
.eachParallel(function(key, val, next) {
+ t.equal(this.next, next);
setTimeout(function() {
next(null, key + '/' + val);
}, 100);
@@ -78,6 +82,7 @@ module.exports = nodeunit.testCase({
'Chain.each() object': function(t) {
var start = Date.now();
chain.each({ foo: 'bar', hoge: 'fuga' }, function(key, val, next) {
+ t.equal(this.next, next);
setTimeout(function() {
next(null, key + '/' + val);
}, 100);
@@ -93,6 +98,7 @@ module.exports = nodeunit.testCase({
'Chain.each() array': function(t) {
var start = Date.now();
chain.each(['foo', 'bar'], function(key, val, next) {
+ t.equal(this.next, next);
setTimeout(function() {
next(null, key + '/' + val);
}, 100);
@@ -109,6 +115,7 @@ module.exports = nodeunit.testCase({
'Chain.eachParallel() object': function(t) {
var start = Date.now();
chain.eachParallel({ foo: 'bar', hoge: 'fuga'}, function(key, val, next) {
+ t.equal(this.next, next);
setTimeout(function() {
next(null, key + '/' + val);
}, 100);
@@ -124,6 +131,7 @@ module.exports = nodeunit.testCase({
'Chain.eachParallel() array': function(t) {
var start = Date.now();
chain.eachParallel(['foo', 'bar'], function(key, val, next) {
+ t.equal(this.next, next);
setTimeout(function() {
next(null, key + '/' + val);
}, 100);
@@ -136,5 +144,5 @@ module.exports = nodeunit.testCase({
next();
})
.end(t.done);
- },
+ }
});
View
22 test/paralle.js
@@ -20,11 +20,15 @@ module.exports = nodeunit.testCase({
t.equal(val, 'foo');
r.push(2);
next(null, 2);
- }, 1)
+ }, 1);
+ },
+ function(val, next) {
+ t.equal(this.next, next);
+ next(null, 3);
}
])
.chain(function(results, next) {
- t.deepEqual(results, [1, 2]);
+ t.deepEqual(results, [1, 2, 3]);
t.deepEqual(r, [2, 1]);
next();
})
@@ -44,10 +48,14 @@ module.exports = nodeunit.testCase({
r.push(2);
next(null, 2);
}, 1)
+ },
+ baz: function(next) {
+ t.equal(this.next, next);
+ next(null, 3);
}
})
.chain(function(results, next) {
- t.deepEqual(results, { foo: 1, bar: 2 });
+ t.deepEqual(results, { foo: 1, bar: 2, baz: 3 });
t.deepEqual(r, [2, 1]);
next();
})
@@ -71,11 +79,15 @@ module.exports = nodeunit.testCase({
t.equal(val, 'foo');
r.push(2);
next(null, 2);
- }, 1)
+ }, 1);
+ },
+ baz: function(val, next) {
+ t.equal(this.next, next);
+ next(null, 3);
}
})
.chain(function(results, next) {
- t.deepEqual(results, { foo: 1, bar: 2 });
+ t.deepEqual(results, { foo: 1, bar: 2, baz: 3 });
t.deepEqual(r, [2, 1]);
next();
})

0 comments on commit e11112a

Please sign in to comment.
Something went wrong with that request. Please try again.