-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
59 lines (48 loc) · 1.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**!
* co-event-wrap - index.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <[email protected]> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var co = require('co');
module.exports = wrap;
function wrap(emitter) {
var on = emitter.on;
var once = emitter.once;
var removeListener = emitter.removeListener;
emitter.on = emitter.addListener = function (type, listener) {
var wrapped = listener;
if (listener.constructor.name === 'GeneratorFunction') {
wrapped = wrapListener(listener);
listener.__coEventWrapListener = wrapped;
}
on.call(emitter, type, wrapped);
};
emitter.removeListener = function (type, listener) {
if (listener && listener.__coEventWrapListener) {
listener = listener.__coEventWrapListener;
}
removeListener.call(emitter, type, listener);
};
emitter.once = function (type, listener) {
if (listener.constructor.name === 'GeneratorFunction') {
listener = wrapListener(listener);
}
once.call(emitter, type, listener);
};
return emitter;
}
function wrapListener(listener) {
return function () {
var wrapped = co.wrap(listener);
wrapped.apply(null, arguments).then(noop);
};
}
function noop() {}