-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
184 lines (159 loc) · 3.9 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* jshint esnext: true, noyield: true */
function Emitter(obj) {
this._listeners = {};
this._styles = {};
if(obj) mixin(obj);
}
/**
* Mixes the co-emitter into the object
*
* @param {Object} obj
* @return undefined
* @api private
*/
function mixin(obj) {
Emitter.call(obj);
for(var key in Emitter.prototype) {
obj[key] = Emitter.prototype[key];
}
}
/**
* Register a generator for the given event
*
* @param {String} event
* @param {Function*} generator
* @return {Emitter}
* @api public
*/
Emitter.prototype.on = function() {
var args = Array.prototype.slice.call(arguments),
event = args.shift();
var listeners = this._listeners[event] || [],
add = function(listener) { this.on(event, listener); }.bind(this);
this._listeners[event] = listeners;
if(Array.isArray(args[0])) {
args[0].forEach(add);
} else if(args.length > 1) {
args.forEach(add);
} else {
var listener = args[0],
existingStyle = this._styles[event],
style = this._styles[event] = isGenerator(listener) ? 'generator' : 'function';
if(existingStyle && style !== existingStyle) {
throw new Error('Cannot mix generator and function listeners');
} else {
this._styles[event] = style;
listeners.push(listener);
}
}
return this;
};
/**
* Register a generator for the given event that runs just once
*
* @param {String} event
* @param {Function*} generator
* @return {Emitter}
* @api public
*/
Emitter.prototype.once = function(event, listener) {
var self = this, remover;
if(isGenerator(listener)) {
remover = function*() {
var args = Array.prototype.slice.call(arguments);
yield listener.apply(this, args);
self.off(event, remover);
};
} else {
remover = function() {
var args = Array.prototype.slice.call(arguments);
listener.apply(this, args);
self.off(event, remover);
};
}
this.on(event, remover);
};
/**
* Runs all of the listeners for a given event
*
* @param {String} event
* @param {[args]} arguments
* @return {Emitter}
* @api public
*/
Emitter.prototype.emit = function() {
var args = Array.prototype.slice.call(arguments),
event = args.shift(),
listeners = this.listeners(event),
style = this._styles[event];
if(style == 'function') {
for(var i = 0; i < listeners.length; ++i) {
listeners[i].apply(this, args);
}
return undefined;
} else {
return function*() {
for(var i = 0; i < listeners.length; ++i) {
if(Array.isArray(args)) {
args = (yield listeners[i].apply(this, args)) || args;
} else {
args = (yield listeners[i].call(this, args)) || args;
}
}
return args.length == 1 ? args[0] : args;
}.call(this);
}
};
/**
* Removes Listener
*
* @param {String} event
* @param {Function*} listener
* @return {undefined}
* @api public
*/
Emitter.prototype.off = function(event, listener) {
if(!event) {
this._listeners = {};
this._styles = {};
} else if(!listener) {
delete this._listeners[event];
delete this._styles[event];
} else {
var index = this._listeners[event].indexOf(listener);
if(~index) this._listeners[event].splice(index, 1);
}
};
/**
* Removes All listeners
*
* @return {undefined}
* @api public
*/
Emitter.prototype.removeAllListeners = function() {
this.off();
};
/**
* Checks whether a listener exists for a given event
*
* @param {String} event
* @return {Boolean}
* @api public
*/
Emitter.prototype.hasListeners = function(event) {
return this.listeners(event).length > 0;
};
/**
* Returns an array of all listeners for a given event
*
* @param {String} event
* @return {Array}
* @api public
*/
Emitter.prototype.listeners = function(event) {
return this._listeners[event] ? this._listeners[event] : [];
};
module.exports = Emitter;
function isGenerator(fn) {
return /^function\s*\*/.test(fn.toString());
};