thunkify-wrap
Advanced tools
Comparing version 0.0.3 to 0.0.4
n.n.n / 2014-03-05 | ||
================== | ||
* add event in readme | ||
* add test for event | ||
* add event support | ||
n.n.n / 2014-02-28 | ||
@@ -3,0 +10,0 @@ ================== |
55
index.js
@@ -7,2 +7,3 @@ /**! | ||
'use strict'; | ||
var EventEmitter = require('events').EventEmitter; | ||
@@ -26,2 +27,6 @@ /** | ||
if (input instanceof EventEmitter) { | ||
return eventToThunk(input, ctx); | ||
} | ||
// thunkify object | ||
@@ -59,3 +64,2 @@ if (type === 'object') { | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
@@ -91,1 +95,50 @@ | ||
} | ||
/** | ||
* wrap a event object to a thunk | ||
* yield to wait the event emit `end`, `close`, `finish` or others | ||
* @param {Event} e | ||
* @param {Array} globalEvents | ||
* @return {Function} | ||
*/ | ||
function eventToThunk(e, globalEvents) { | ||
globalEvents = globalEvents || ['end']; | ||
return function (endEvents) { | ||
var called = false; | ||
endEvents = endEvents || globalEvents; | ||
if (!Array.isArray(endEvents)) { | ||
endEvents = [endEvents]; | ||
} | ||
return function (done) { | ||
// clean | ||
function _done(err, data) { | ||
e.removeListener('error', error); | ||
endEvents.forEach(function (name) { | ||
e.removeListener(name, end); | ||
}); | ||
done(err, data); | ||
} | ||
function error(err) { | ||
if (called) { | ||
return; | ||
} | ||
called = true; | ||
done(err); | ||
} | ||
function end(data) { | ||
if (called) { | ||
return; | ||
} | ||
called = true; | ||
done(null, data); | ||
} | ||
e.once('error', error); | ||
endEvents.forEach(function (name) { | ||
e.once(name, end); | ||
}); | ||
}; | ||
}; | ||
} |
{ | ||
"name": "thunkify-wrap", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"repository": "dead-horse/node-thunkify-wrap", | ||
@@ -5,0 +5,0 @@ "description": "Turn callbacks, arrays, generators, generator functions, and promises into a thunk", |
@@ -35,2 +35,17 @@ | ||
## event support | ||
you can pass an event object, give end event name list, wrap event to thunk like this | ||
``` | ||
var e = new EventEmitter(); | ||
var end = thunkify(e, 'finish'); | ||
yield end(); | ||
or | ||
yield.end(['close', 'end']); // will cover `finish` event | ||
``` | ||
when specified events emitted, this generator will go on. see more in the source code. | ||
## ctx | ||
@@ -37,0 +52,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13501
15
389
105