Permalink
Browse files

Improve all API

  • Loading branch information...
1 parent 982fc54 commit a8b41c927b07dfff0dcf01006aada481ca7eb862 @nakamura-to committed Jun 12, 2012
Showing with 69 additions and 62 deletions.
  1. +24 −17 lib/gate.js
  2. +1 −1 package.json
  3. +44 −44 test/gate.js
View
@@ -1,29 +1,36 @@
'use strict';
-exports.latch = latch;
+exports.create = create;
var util = require('util');
var assert = require('assert');
var noop = function noop() {};
-function latch(count) {
- var async = new Async(count);
- var _latch = function makeCallback(mapping, skipErrorCheck) {
- return async.makeCallback(makeCallback, mapping, skipErrorCheck);
- };
- _latch.val = function val(value) {
- return new Val(value);
- };
- _latch.count = function count() {
- return async.count;
- };
- _latch.await = function await(callback) {
- async.await(callback);
- async.await = noop;
- };
- return _latch;
+function create(count) {
+ return new Gate(count);
+}
+
+function Gate(count) {
+ this._async = new Async(count);
+ Object.defineProperty(this, "count", {
+ get: function () { return this._async.count; },
+ enumerable: true
+ });
}
+Gate.prototype.latch = function latch(mapping, skipErrorCheck) {
+ return this._async.makeCallback(latch, mapping, skipErrorCheck);
+};
+
+Gate.prototype.val = function val(value) {
+ return new Val(value);
+};
+
+Gate.prototype.await = function await(callback) {
+ this._async.await(callback);
+ this._async.await = noop;
+};
+
function Val(value) {
this.value = value;
}
View
@@ -10,5 +10,5 @@
"devDependencies": {
"mocha": "~0.11.0"
},
- "version" : "0.1.0"
+ "version" : "0.2.0dev"
}
View
@@ -3,119 +3,119 @@ var assert = require('assert');
describe('latch', function() {
it('should await async calls', function (done) {
- var latch = gate.latch();
- process.nextTick(latch({val: 'a'}));
- process.nextTick(latch({val: 'b'}));
- latch.await(function (err, results) {
+ 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'}, {val: 'b'}], results);
done();
- });
+ });
});
it('should await async calls with a given count', function (done) {
- var latch = gate.latch(3);
- latch.await(function (err, results) {
+ var g = gate.create(3);
+ g.await(function (err, results) {
if (err) throw err;
assert.deepEqual([{val: 'a'}, {val: 'b'}, {val: 'c'}], results);
done();
});
- process.nextTick(latch({val: 'a'}));
- assert.strictEqual(2, latch.count());
- process.nextTick(latch({val: 'b'}));
- assert.strictEqual(1, latch.count());
- process.nextTick(latch({val: 'c'}));
- assert.strictEqual(0, latch.count());
- process.nextTick(latch({val: 'd'}));
- assert.strictEqual(0, latch.count());
+ process.nextTick(g.latch({val: 'a'}));
+ assert.strictEqual(2, g.count);
+ process.nextTick(g.latch({val: 'b'}));
+ assert.strictEqual(1, g.count);
+ process.nextTick(g.latch({val: 'c'}));
+ assert.strictEqual(0, g.count);
+ process.nextTick(g.latch({val: 'd'}));
+ assert.strictEqual(0, g.count);
});
it('should await sync calls', function (done) {
- var latch = gate.latch();
- latch({val: 'a'})();
- latch({val: 'b'})();
- latch.await(function (err, results) {
+ var g = gate.create();
+ g.latch({val: 'a'})();
+ g.latch({val: 'b'})();
+ g.await(function (err, results) {
if (err) throw err;
assert.deepEqual([{val: 'a'}, {val: 'b'}], results);
done();
});
});
it('should await sync calls with a given count', function (done) {
- var latch = gate.latch(3);
- latch.await(function (err, results) {
+ var g = gate.create(3);
+ g.await(function (err, results) {
if (err) throw err;
assert.deepEqual([{val: 'a'}, {val: 'b'}, {val: 'c'}], results);
done();
});
- latch({val: 'a'})();
- assert.strictEqual(2, latch.count());
- latch({val: 'b'})();
- assert.strictEqual(1, latch.count());
- latch({val: 'c'})();
- assert.strictEqual(0, latch.count());
- latch({val: 'd'})();
- assert.strictEqual(0, latch.count());
+ g.latch({val: 'a'})();
+ assert.strictEqual(2, g.count);
+ g.latch({val: 'b'})();
+ assert.strictEqual(1, g.count);
+ g.latch({val: 'c'})();
+ assert.strictEqual(0, g.count);
+ g.latch({val: 'd'})();
+ assert.strictEqual(0, g.count);
});
it('should handle error', function (done) {
- var latch = gate.latch();
- var callback = latch(1);
+ var g = gate.create();
+ var callback = g.latch(1);
process.nextTick(function () {
callback('ERROR');
});
- latch.await(function (err) {
+ g.await(function (err) {
assert.strictEqual('ERROR', err);
done();
});
});
it('should handle error object', function (done) {
- var latch = gate.latch();
- var callback = latch(1);
+ var g = gate.create();
+ var callback = g.latch(1);
process.nextTick(function () {
callback(new Error('ERROR'));
});
- latch.await(function (err) {
+ g.await(function (err) {
assert.strictEqual('ERROR', err.message);
assert(err.callbackLocation);
done();
});
});
it('should skip error check', function (done) {
- var latch = gate.latch();
- var callback = latch(0, true);
+ var g = gate.create();
+ var callback = g.latch(0, true);
process.nextTick(function () {
callback('ERROR');
});
- latch.await(function (err, results) {
+ g.await(function (err, results) {
if (err) throw err;
assert.strictEqual('ERROR', results[0]);
done();
});
});
it('should map values', function (done) {
- var latch = gate.latch();
- var callback = latch({name: 'aaa', age: latch.val(20), arg1: 1, arg2: 2});
+ var g = gate.create();
+ var callback = g.latch({name: 'aaa', age: g.val(20), arg1: 1, arg2: 2});
process.nextTick(function () {
callback(0, 'bbb', 100);
});
- latch.await(function (err, results) {
+ g.await(function (err, results) {
if (err) throw err;
assert.deepEqual([{name: 'aaa', age: 20, arg1: 'bbb', arg2: 100}], results);
done();
});
});
it('should map all values', function (done) {
- var latch = gate.latch();
- var callback = latch();
+ var g = gate.create();
+ var callback = g.latch();
process.nextTick(function () {
callback(null, 'bbb', 100);
});
- latch.await(function (err, results) {
+ g.await(function (err, results) {
if (err) throw err;
assert.deepEqual([[null, 'bbb', 100]], results);
done();

0 comments on commit a8b41c9

Please sign in to comment.