Comparing version 0.0.8 to 0.0.9
@@ -14,3 +14,3 @@ var async = require('async'), | ||
// Attach async methods. | ||
['series', 'parallel'].forEach(function(method) { | ||
['series', 'parallel', 'waterfall'].forEach(function(method) { | ||
eventEmitter[method] = function () { | ||
@@ -21,3 +21,3 @@ var emitter = this, | ||
callback = args.pop(), | ||
tasks = mapHandlers(emitter, name, args); | ||
tasks = mapHandlers(method, emitter, name, args); | ||
@@ -66,2 +66,3 @@ async[method](tasks, callback); | ||
function asyncApply (thisArg, fn, args, done) { | ||
if (!Array.isArray(args)) args = [args]; | ||
if (fn.length <= args.length) { | ||
@@ -71,10 +72,14 @@ done(null, fn.apply(thisArg, args)); | ||
else { | ||
fn.apply(thisArg, args.slice(0).concat([done])); | ||
fn.apply(thisArg, args.slice().concat(done)); | ||
} | ||
} | ||
function mapHandlers (emitter, name, args) { | ||
return emitter.listeners(name).map(function (listener) { | ||
function mapHandlers (method, emitter, name, args) { | ||
return emitter.listeners(name).map(function (listener, idx) { | ||
if (method === 'waterfall' && idx > 0) { | ||
// For waterfall, args only need to be bound to the first task. | ||
return asyncApply.bind(emitter, emitter, listener); | ||
} | ||
return asyncApply.bind(emitter, emitter, listener, args); | ||
}); | ||
} |
{ | ||
"name": "eventflow", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Flow control for your event emitters", | ||
@@ -5,0 +5,0 @@ "main": "eventflow.js", |
@@ -153,3 +153,23 @@ EventFlow | ||
Waterfall | ||
--------- | ||
The waterfall method allows listeners to modify a variable in a series. The | ||
first listener receives an initial value, and each subsequent listener modifies | ||
the return of the last listener: | ||
```js | ||
emitter.on('foo', function(n) { | ||
// sync task | ||
return n + 1; | ||
}); | ||
emitter.on('foo', function(n, callback) { | ||
// async task | ||
cb(null, n * 3); | ||
}); | ||
emitter.waterfall('foo', 2, function(err, n) { | ||
// n = 9 | ||
}); | ||
``` | ||
- - - | ||
@@ -156,0 +176,0 @@ |
@@ -231,1 +231,44 @@ var eventflow = require('../'), | ||
describe('waterfall', function() { | ||
var emitter; | ||
beforeEach(function() { | ||
emitter = new EventEmitter(); | ||
}); | ||
it('should pass a value between handlers', function (done) { | ||
emitter.on('foo', function (n) { | ||
return n + 1; | ||
}); | ||
emitter.on('foo', function (n, cb) { | ||
cb(null, n * 5); | ||
}); | ||
emitter.on('foo', function (n) { | ||
return n - 3; | ||
}); | ||
emitter.waterfall('foo', 0, function (err, n) { | ||
assert.equal(n, 2); | ||
done(); | ||
}); | ||
}); | ||
it('should support optional use of `error`', function (done) { | ||
emitter.on('drink', function (n) { | ||
return n + 1; | ||
}); | ||
emitter.on('drink', function (n, cb) { | ||
cb('oh no!'); | ||
}); | ||
emitter.on('drink', function (n) { | ||
return n - 3; | ||
}); | ||
emitter.waterfall('drink', 0, function (err, n) { | ||
assert.equal(n, undefined); | ||
assert.equal(err, 'oh no!'); | ||
done(); | ||
}); | ||
}); | ||
}); |
15886
335
201