Permalink
Browse files

Create nested gate instance automatically

  • Loading branch information...
1 parent 8329861 commit 9880c0403193b46e5102fbe84dd42f598a8bb84e @nakamura-to committed Jul 6, 2012
Showing with 29 additions and 10 deletions.
  1. +9 −10 lib/gate.js
  2. +20 −0 test/gate_test.js
View
@@ -7,16 +7,15 @@ var assert = require('assert');
var noop = function noop() {};
function create(options) {
- options = options || {};
- options.count = typeof options.count === 'number' ? options.count : -1;
- options.failFast = options.failFast !== false;
- options.domain = options.domain || null;
return new Gate(options);
}
function Gate(options) {
- this.options = options;
- this._async = new Async(options.count, options.domain, options.failFast);
+ options = options || {};
+ var count = typeof options.count === 'number' ? options.count : -1;
+ var failFast = options.failFast !== false;
+ var domain = options.domain || null;
+ this._async = new Async(count, domain, failFast);
}
Object.defineProperty(Gate.prototype, "count", {
@@ -60,9 +59,9 @@ function Async(count, domain, failFast) {
Async.prototype.await = function await(callback) {
var next = this.domain ? this.domain.intercept(callback) : callback;
if (this.error) {
- next(this.error);
+ next(this.error, null, new Gate({domain: this.domain}));
} else if (this.pending === 0 && this.count <= 0) {
- next(null, this.results);
+ next(null, this.results, new Gate({domain: this.domain}));
} else {
this.next = next;
}
@@ -90,7 +89,7 @@ Async.prototype.makeCallback = function makeCallback(caller, name, mapping) {
}
if (next) {
self.next = noop;
- next(error);
+ next(error, null, new Gate({domain: self.domain}));
} else {
self.error = error;
}
@@ -103,7 +102,7 @@ Async.prototype.makeCallback = function makeCallback(caller, name, mapping) {
}
if (self.pending === 0 && self.count <= 0 && next) {
self.next = noop;
- next(null, self.results);
+ next(null, self.results, new Gate({domain: self.domain}));
}
}
}
View
@@ -189,4 +189,24 @@ describe('latch', function() {
});
*/
+ it('should nest gate', function (done) {
+ var g = gate.create();
+ setTimeout(g.latch({val: 'a'}), 0);
+ setTimeout(g.latch({val: 'b'}), 0);
+ g.await(function (err, results, g) {
+ if (err) throw err;
+ assert.deepEqual({val: 'a'}, results[0]);
+ assert.deepEqual({val: 'b'}, results[1]);
+ setTimeout(g.latch({val: 'c'}), 0);
+ setTimeout(g.latch({val: 'd'}), 0);
+ g.await(function (err, results) {
+ if (err) throw err;
+ assert.deepEqual({val: 'c'}, results[0]);
+ assert.deepEqual({val: 'd'}, results[1]);
+ done();
+ });
+ });
+ });
+
+
});

0 comments on commit 9880c04

Please sign in to comment.