Skip to content

Commit

Permalink
Rename from callbackLocation to gate_location. Update examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamura-to committed Jun 14, 2012
1 parent a8b41c9 commit e8fe5d9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 38 deletions.
6 changes: 3 additions & 3 deletions examples/countDown.js
Expand Up @@ -2,15 +2,15 @@ var gate = require('../index');
var fs = require('fs');

var files = ['file1', 'file2'];
var latch = gate.latch(files.length);
latch.await(function (err, results) {
var g = gate.create(files.length);
g.await(function (err, results) {
if (err) throw err;
console.log(results[0]); // { name: 'file1', data: 'FILE1' }
console.log(results[1]); // { name: 'file2', data: 'FILE2' }
});

process.nextTick(function () {
files.forEach(function (file) {
fs.readFile(file, 'utf8', latch({name: file, data: 1}));
fs.readFile(file, 'utf8', g.latch({name: file, data: 1}));
});
});
8 changes: 4 additions & 4 deletions examples/errorCheckSkipping.js
@@ -1,11 +1,11 @@
var gate = require('../index');
var fs = require('fs');

var latch = gate.latch();
fs.readFile('non-existent1', 'utf8', latch({err: 0, data: 1}, true));
fs.readFile('non-existent2', 'utf8', latch({err: 0, data: 1}, true));
var g = gate.create();
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));

latch.await(function (err, results) {
g.await(function (err, results) {
results.forEach(function (result) {
if (result.err) {
console.log(result.err);
Expand Down
12 changes: 6 additions & 6 deletions examples/errorHandling.js
@@ -1,14 +1,14 @@
var gate = require('../index');
var fs = require('fs');

var latch = gate.latch();
fs.readFile('file1', 'utf8', latch({name: 'file1', data: 1}));
fs.readFile('non-existent', 'utf8', latch({name: 'non-existent', data: 1}));
var g = gate.create();
fs.readFile('file1', 'utf8', g.latch({name: 'file1', data: 1}));
fs.readFile('non-existent', 'utf8', g.latch({name: 'non-existent', data: 1}));

latch.await(function (err, results) {
g.await(function (err, results) {
if (err) {
console.log(err + ', callbackLocation: ' + err.callbackLocation);
console.log(err + ', gate_location: ' + err.gate_location);
} else {
console.log(results);
}
});
});
6 changes: 3 additions & 3 deletions examples/loop.js
@@ -1,12 +1,12 @@
var gate = require('../index');
var fs = require('fs');

var latch = gate.latch();
var g = gate.create();
['file1', 'file2'].forEach(function (file) {
fs.readFile(file, 'utf8', latch({name: file, data: 1}));
fs.readFile(file, 'utf8', g.latch({name: file, data: 1}));
});

latch.await(function (err, results) {
g.await(function (err, results) {
if (err) throw err;
console.log(results[0]); // { name: 'file1', data: 'FILE1' }
console.log(results[1]); // { name: 'file2', data: 'FILE2' }
Expand Down
10 changes: 5 additions & 5 deletions examples/mapping.js
Expand Up @@ -2,18 +2,18 @@ var gate = require('../index');
var fs = require('fs');
var exec = require('child_process').exec;

var latch = gate.latch();
var g = gate.create();

// single mapping: arguments[1] in the callback will be result
fs.readFile('file1', 'utf8', latch(1));
fs.readFile('file1', 'utf8', g.latch(1));

// multiple mapping: object including arguments[1] and argments[2] in the callback will be result
exec('cat *.js bad_file | wc -l', latch({stdout: 1, stderr: 2}));
exec('cat *.js bad_file | wc -l', g.latch({stdout: 1, stderr: 2}));

// all mapping: all arguments in the callback will be result
fs.readFile('file2', 'utf8', latch());
fs.readFile('file2', 'utf8', g.latch());

latch.await(function (err, results) {
g.await(function (err, results) {
if (err !== null) {
console.log('exec error: ' + err);
}
Expand Down
10 changes: 5 additions & 5 deletions examples/setTimeout.js
@@ -1,11 +1,11 @@
var gate = require('../index');

var latch = gate.latch();
setTimeout(latch({val: 'a'}), 30);
setTimeout(latch({val: 'b'}), 20);
setTimeout(latch({val: 'c'}), 10);
var g = gate.create();
setTimeout(g.latch({val: 'a'}), 30);
setTimeout(g.latch({val: 'b'}), 20);
setTimeout(g.latch({val: 'c'}), 10);

latch.await(function (err, results) {
g.await(function (err, results) {
if (err) throw err;
console.log(results);
});
8 changes: 4 additions & 4 deletions examples/simple.js
@@ -1,11 +1,11 @@
var gate = require('../index');
var fs = require('fs');

var latch = gate.latch();
fs.readFile('file1', 'utf8', latch({name: 'file1', data: 1}));
fs.readFile('file2', 'utf8', latch({name: 'file2', data: 1}));
var g = gate.create();
fs.readFile('file1', 'utf8', g.latch({name: 'file1', data: 1}));
fs.readFile('file2', 'utf8', g.latch({name: 'file2', data: 1}));

latch.await(function (err, results) {
g.await(function (err, results) {
if (err) throw err;
console.log(results[0]); // { name: 'file1', data: 'FILE1' }
console.log(results[1]); // { name: 'file2', data: 'FILE2' }
Expand Down
13 changes: 7 additions & 6 deletions lib/gate.js
Expand Up @@ -12,12 +12,13 @@ function create(count) {

function Gate(count) {
this._async = new Async(count);
Object.defineProperty(this, "count", {
get: function () { return this._async.count; },
enumerable: true
});
}

Object.defineProperty(Gate.prototype, "count", {
get: function count() { return this._async.count; },
enumerable: true
});

Gate.prototype.latch = function latch(mapping, skipErrorCheck) {
return this._async.makeCallback(latch, mapping, skipErrorCheck);
};
Expand Down Expand Up @@ -70,8 +71,8 @@ Async.prototype.makeCallback = function makeCallback(caller, mapping, skipErrorC
var next = self.next;
self.pending--;
if (!self.canceled) {
if (error && typeof error === 'object' && !('callbackLocation' in error)) {
error.callbackLocation = location;
if (error && typeof error === 'object' && !('gate_location' in error)) {
error.gate_location = location;
}
if (error && !skipErrorCheck) {
self.canceled = true;
Expand Down
4 changes: 2 additions & 2 deletions test/gate.js
Expand Up @@ -78,8 +78,8 @@ describe('latch', function() {
});
g.await(function (err) {
assert.strictEqual('ERROR', err.message);
assert(err.callbackLocation);
done();
assert(err.gate_location);
done();
});
});

Expand Down

0 comments on commit e8fe5d9

Please sign in to comment.