Skip to content

Commit

Permalink
Support named result
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamura-to committed Jun 15, 2012
1 parent e8fe5d9 commit 52b5305
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
11 changes: 6 additions & 5 deletions examples/errorCheckSkipping.js
Expand Up @@ -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);
}
});
20 changes: 15 additions & 5 deletions lib/gate.js
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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.');
Expand All @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions test/gate.js
Expand Up @@ -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'}));
Expand Down

0 comments on commit 52b5305

Please sign in to comment.