Permalink
Browse files

Support named result

  • Loading branch information...
1 parent e8fe5d9 commit 52b53055fcd5b1a5ad698c1b04807cb250f1a317 @nakamura-to committed Jun 15, 2012
Showing with 58 additions and 10 deletions.
  1. +6 −5 examples/errorCheckSkipping.js
  2. +15 −5 lib/gate.js
  3. +37 −0 test/gate.js
@@ -6,9 +6,10 @@ fs.readFile('non-existent1', 'utf8', g.latch({err: 0, data: 1}, true));
fs.readFile('no n-existent2', 'utf8', g.latch({err: 0, data: 1}, true));
g.await(function (err, results) {
- results.forEach(function (result) {
- if (result.err) {
- console.log(result.err);
- }
- });
+ if (results[0].err) {
+ console.log(results[0].err);
+ }
+ if (results[1].err) {
+ console.log(results[1].err);
+ }
});
View
@@ -19,8 +19,13 @@ Object.defineProperty(Gate.prototype, "count", {
enumerable: true
});
-Gate.prototype.latch = function latch(mapping, skipErrorCheck) {
- return this._async.makeCallback(latch, mapping, skipErrorCheck);
+Gate.prototype.latch = function latch(name, mapping, skipErrorCheck) {
+ if (typeof name !== "string") {
+ skipErrorCheck = mapping;
+ mapping = name;
+ name = null;
+ }
+ return this._async.makeCallback(latch, name, mapping, skipErrorCheck);
};
Gate.prototype.val = function val(value) {
@@ -43,7 +48,7 @@ function Async(count) {
this.canceled = false;
this.next = null;
this.error = null;
- this.results = [];
+ this.results = {};
}
Async.prototype.await = function await(callback) {
@@ -56,7 +61,7 @@ Async.prototype.await = function await(callback) {
}
};
-Async.prototype.makeCallback = function makeCallback(caller, mapping, skipErrorCheck) {
+Async.prototype.makeCallback = function makeCallback(caller, name, mapping, skipErrorCheck) {
var type = typeof mapping;
assert(type !== 'undefined' || type !== 'number' || type !== 'object',
'An argument `mapping` must be a number or an object, if specified.');
@@ -83,7 +88,12 @@ Async.prototype.makeCallback = function makeCallback(caller, mapping, skipErrorC
self.error = error;
}
} else {
- self.results[index] = mapArguments(mapping, arguments);
+ var result = mapArguments(mapping, arguments);
+ if (name === null) {
+ self.results[index] = result;
+ } else {
+ self.results[name] = result;
+ }
if (self.pending === 0 && self.count <= 0 && next) {
self.next = noop;
next(null, self.results);
View
@@ -2,6 +2,43 @@ var gate = require('../lib/gate.js');
var assert = require('assert');
describe('latch', function() {
+
+ it('should latch with index', function (done) {
+ var g = gate.create();
+ process.nextTick(g.latch({val: 'a'}));
+ process.nextTick(g.latch({val: 'b'}));
+ g.await(function (err, results) {
+ if (err) throw err;
+ assert.deepEqual({val: 'a'}, results[0]);
+ assert.deepEqual({val: 'b'}, results[1]);
+ done();
+ });
+ });
+
+ it('should latch with name', function (done) {
+ var g = gate.create();
+ process.nextTick(g.latch('hoge', {val: 'a'}));
+ process.nextTick(g.latch('foo', {val: 'b'}));
+ g.await(function (err, results) {
+ if (err) throw err;
+ assert.deepEqual({val: 'a'}, results.hoge);
+ assert.deepEqual({val: 'b'}, results.foo);
+ done();
+ });
+ });
+
+ it('should latch with index and name', function (done) {
+ var g = gate.create();
+ process.nextTick(g.latch('hoge', {val: 'a'}));
+ process.nextTick(g.latch({val: 'b'}));
+ g.await(function (err, results) {
+ if (err) throw err;
+ assert.deepEqual({val: 'a'}, results.hoge);
+ assert.deepEqual({val: 'b'}, results[1]);
+ done();
+ });
+ });
+
it('should await async calls', function (done) {
var g = gate.create();
process.nextTick(g.latch({val: 'a'}));

0 comments on commit 52b5305

Please sign in to comment.