This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
78 lines (64 loc) · 1.47 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* dependencies.
*/
var Emitter = require('events').EventEmitter
, proto = Emitter.prototype;
/**
* expsoe `mixin`
*
* @param {Object} obj
*/
module.exports = function (obj) {
// mixin
for (var k in proto) {
obj[k] = proto[k];
}
// events getter.
obj.__defineGetter__('_events', function () {
return this.__events || (this.__events = {});
});
// events setter.
obj.__defineSetter__('_events', function (val) {
this.__events = val;
});
/**
* Remove all listeners for `event`.
*
* if the method is executed without
* arguments it will remove all listeners,
* otherwise you can supply `event` or
* `event` with `fn` for more specific stuff.
*
* example:
*
* obj.on('foo', console.log)._events;
* // > { foo: fn, }
* obj.on('foo', console.dir)._events;
* // > { foo: [fn, fn] }
* obj.off('foo', console.log)._events;
* // > { foo: [fn] }
* obj.off('foo');
* // > {}
* obj.off();
* // > {}
*
* @param {String} event
* @param {Function} fn
* @return {self}
*/
obj.off = function (event, fn) {
switch (arguments.length) {
case 2:
this.removeListener(event, fn);
return this;
case 1:
this.removeAllListeners(event);
return this;
case 0:
this.removeAllListeners();
return this;
}
};
// all done
return obj;
};