-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventEmitter-compatible.js
490 lines (461 loc) · 16.6 KB
/
EventEmitter-compatible.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*!
* Event Emitter (the best you'll get ^^)
* author: Stefan Benicke
* version: 1.2.0
* url: https://github.com/opusonline/EventEmitter.js
* license: MIT
* features:
* - on: multiple events, event namespaces, multiple listeners, listeners with context
* - off: all, multiple events, event namespaces, multiple listeners, listeners with context
* - emit: multiple events, event namespaces
* - listeners: multiple events, namespaces, all at once
* - newListener/removeListener events
* - chainable
* - includes inherit method and noConflict
* inspired by:
* - https://nodejs.org/api/events.html
* - http://radio.uxder.com/ (radiojs)
* - https://github.com/Olical/EventEmitter
* - https://github.com/asyncly/EventEmitter2
* - https://github.com/primus/eventemitter3
*/
;
(function (global, undefined) {
'use strict';
var nativeBind;
var existingEventEmitter = global.EventEmitter;
function EE(listener, namespaces, once) {
var context;
if (Array.isArray(listener)) {
this.listener = listener[0];
context = listener[1];
} else {
this.listener = listener;
}
if (this.listener.hasOwnProperty('boundThis')) {
context = this.listener.boundThis; // bound functions are prefered against apply context
}
this.context = context;
this.namespaces = namespaces;
this.once = once || false;
}
function EventEmitter() {
}
EventEmitter.noConflict = function noConflict() {
global.EventEmitter = existingEventEmitter;
return EventEmitter;
};
// adapted from https://github.com/nodejs/node-v0.x-archive/blob/master/lib/util.js#L634
EventEmitter.inherits = function inherits(ctor) {
ctor.super_ = EventEmitter;
ctor.prototype = createObject(EventEmitter.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
EventEmitter.prototype.listeners = function listeners(event) {
var i, n, namespaces, result = [];
if (typeof this._events === 'undefined') {
return result;
}
if (arguments.length === 0 || typeof event === 'undefined') {
for (event in this._events) {
if (this._events.hasOwnProperty(event)) {
for (i = 0, n = this._events[event].length; i < n; i++) {
result.push(this._events[event][i]);
}
}
}
return result;
} else if (arguments.length > 1) {
for (i = 0, n = arguments.length; i < n; i++) {
arrayExtend(result, this.listeners.call(this, arguments[i]));
}
return result;
}
if (stringIncludes(event, '.')) {
namespaces = event.split('.');
event = namespaces.splice(0, 1)[0];
}
if (event === '') {
for (event in this._events) {
if (this._events.hasOwnProperty(event)) {
for (i = 0, n = this._events[event].length; i < n; i++) {
if (allInArray(namespaces, this._events[event][i].namespaces)) {
result.push(this._events[event][i]);
}
}
}
}
} else {
if (this._events.hasOwnProperty(event) === false) {
return result;
}
for (i = 0, n = this._events[event].length; i < n; i++) {
if (allInArray(namespaces, this._events[event][i].namespaces)) {
result.push(this._events[event][i]);
}
}
}
return result;
};
EventEmitter.prototype.emit = function emit(event/*, ...args */) {
var args, once, i, n, namespaces;
if (typeof this._events === 'undefined' || arguments.length < 1 || typeof event === 'undefined') {
return this;
}
if (isArray(event)) {
for (i = 0, n = event.length; i < n; i++) {
arguments[0] = event[i];
this.emit.apply(this, arguments);
}
return this;
}
if (stringIncludes(event, '.')) {
namespaces = event.split('.');
event = namespaces.splice(0, 1)[0];
}
if (this._events.hasOwnProperty(event) === false) {
return this;
}
if (arguments.length > 1) {
args = Array.prototype.slice.call(arguments, 1);
}
for (i = 0, n = this._events[event].length; i < n; i++) {
if (allInArray(namespaces, this._events[event][i].namespaces)) {
this._events[event][i].listener.apply(this._events[event][i].context, args);
if (this._events[event][i].once) {
once = true;
}
}
}
if (once) {
removeEvent.call(this, event, namespaces, onceCheckFunction);
}
return this;
};
EventEmitter.prototype.on = function on(event/*, ...listener */) {
var i, n;
if (arguments.length < 2 || typeof event === 'undefined') {
return this;
}
if (isArray(event)) {
for (i = 0, n = event.length; i < n; i++) {
arguments[0] = event[i];
this.on.apply(this, arguments);
}
return this;
}
for (i = 1, n = arguments.length; i < n; i++) {
storeListener.call(this, event, arguments[i]);
}
return this;
};
EventEmitter.prototype.once = function once(event/*, ...listener */) {
var i, n;
if (arguments.length < 2 || typeof event === 'undefined') {
return this;
}
if (isArray(event)) {
for (i = 0, n = event.length; i < n; i++) {
arguments[0] = event[i];
this.once.apply(this, arguments);
}
return this;
}
for (i = 1, n = arguments.length; i < n; i++) {
storeListener.call(this, event, arguments[i], true);
}
return this;
};
function storeListener(event, listener, once) {
var ee;
var namespaces = event.split('.');
event = namespaces.splice(0, 1)[0];
ee = new EE(listener, namespaces, once);
if (typeof this._events === 'undefined') {
this._events = {};
}
if (this._events.hasOwnProperty(event) === false) {
this._events[event] = [ee];
} else {
this._events[event].push(ee);
}
if (this._events.hasOwnProperty('newListener')) {
this.emit('newListener', event, ee);
}
}
EventEmitter.prototype.off = function off(event/*, ...listener */) {
var i, n, namespaces;
if (typeof this._events === 'undefined') {
return this;
}
if (arguments.length === 0) {
if (this._events.hasOwnProperty('removeListener')) {
removeAndNotifyEvent.call(this);
} else {
delete this._events;
}
} else { // arguments.length > 0
if (typeof event === 'undefined') {
return this;
}
if (isArray(event)) {
for (i = 0, n = event.length; i < n; i++) {
arguments[0] = event[i];
this.off.apply(this, arguments);
}
return this;
}
if (stringIncludes(event, '.')) {
namespaces = event.split('.');
event = namespaces.splice(0, 1)[0];
}
if (arguments.length === 1) {
if (isArray(namespaces)) {
if (event === '') {
for (event in this._events) {
if (this._events.hasOwnProperty(event)) {
removeEvent.call(this, event, namespaces, argOneCheckFunction);
}
}
} else {
if (this._events.hasOwnProperty(event) === false) {
return this;
}
removeEvent.call(this, event, namespaces, argOneCheckFunction);
}
} else {
if (this._events.hasOwnProperty(event) === false) {
return this;
}
if (this._events.hasOwnProperty('removeListener')) {
removeAndNotifyEvent.call(this, event);
} else {
delete this._events[event];
}
}
} else { // arguments.length > 1
if (event === '') {
for (event in this._events) {
if (this._events.hasOwnProperty(event)) {
for (i = 1, n = arguments.length; i < n; i++) {
removeEvent.call(this, event, namespaces, argManyCheckFunction, arguments[i]);
}
}
}
} else {
if (this._events.hasOwnProperty(event) === false) {
return this;
}
for (i = 1, n = arguments.length; i < n; i++) {
removeEvent.call(this, event, namespaces, argManyCheckFunction, arguments[i]);
}
}
}
}
return this;
};
function removeEvent(event, namespaces, checkFunction, listener) {
var i, n, listeners;
for (i = 0, n = this._events[event].length; i < n; i++) {
if (checkFunction(this._events[event][i], namespaces, listener)) {
if (this._events.hasOwnProperty('removeListener')) {
this.emit('removeListener', event, this._events[event][i]);
}
delete this._events[event][i];
} else {
if (!listeners) {
listeners = [];
}
listeners.push(this._events[event][i]);
}
}
if (isArray(listeners) && listeners.length > 0) {
this._events[event] = listeners;
} else {
delete this._events[event];
}
}
function removeAndNotifyEvent(event) {
var i, n;
if (typeof event === 'undefined') {
for (event in this._events) {
if (this._events.hasOwnProperty(event)) {
removeAndNotifyEvent.call(this, event);
}
}
return;
}
for (i = 0, n = this._events[event].length; i < n; i++) {
this.emit('removeListener', event, this._events[event][i]);
delete this._events[event][i];
}
delete this._events[event];
}
function argOneCheckFunction(ee, namespaces) {
return allInArray(namespaces, ee.namespaces);
}
function argManyCheckFunction(ee, namespaces, listener) {
return ((isArray(listener) && ee.listener === listener[0] && ee.context === listener[1]) || (ee.listener === listener)) && allInArray(namespaces, ee.namespaces);
}
function onceCheckFunction(ee, namespaces) {
return ee.once === true && allInArray(namespaces, ee.namespaces);
}
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
EventEmitter.prototype.removeListener = EventEmitter.prototype.off;
EventEmitter.prototype.removeAllListeners = EventEmitter.prototype.off;
/* helper */
function allInArray(source, target) {
if (!isArray(source)) {
return true; // important for emit, same as source.length === 0
}
if (source.length === 0) {
return true;
} else if (target.length === 0) {
return false;
}
return arrayEvery(source, function (value) {
return arrayIndexOf(target, value) > -1;
});
}
// adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
function isArray(array) {
if (Array.isArray) {
return Array.isArray(array);
}
return Object.prototype.toString.call(array) === '[object Array]';
}
// adapted from https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
function arrayIndexOf(array, searchElement, fromIndex) {
var k, O, len, n;
if (Array.prototype.indexOf) {
return Array.prototype.indexOf.call(array, searchElement, fromIndex);
}
if (array == null) {
throw new TypeError('array is null or not defined');
}
O = Object(array);
len = O.length >>> 0;
if (len === 0) {
return -1;
}
n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
if (n >= len) {
return -1;
}
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (k in O && O[k] === searchElement) {
return k;
}
k++;
}
return -1;
}
// adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
function arrayEvery(array, callbackfn, thisArg) {
var T, k, O, len, kValue, testResult;
if (Array.prototype.every) {
return Array.prototype.every.call(array, callbackfn, thisArg);
}
if (array == null) {
throw new TypeError('array is null or not defined');
}
O = Object(array);
len = O.length >>> 0;
if (typeof callbackfn !== 'function') {
throw new TypeError();
}
if (arguments.length > 2) {
T = thisArg;
}
k = 0;
while (k < len) {
if (k in O) {
kValue = O[k];
testResult = callbackfn.call(T, kValue, k, O);
if (!testResult) {
return false;
}
}
k++;
}
return true;
}
// adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
function stringIncludes(string, search, from) {
if (String.prototype.includes) {
return String.prototype.includes.call(string, search, from);
}
return String.prototype.indexOf.call(string, search, from) !== -1;
}
// adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
var createObject = (function() {
if (Object.create) {
return Object.create;
}
function Temp() {
}
var hasOwn = Object.prototype.hasOwnProperty;
return function (O) {
if (typeof O != 'object') {
throw TypeError('Object prototype may only be an Object or null');
}
Temp.prototype = O;
var obj = new Temp();
Temp.prototype = null;
if (arguments.length > 1) {
var Properties = Object(arguments[1]);
for (var prop in Properties) {
if (hasOwn.call(Properties, prop)) {
obj[prop] = Properties[prop];
}
}
}
return obj;
};
})();
function arrayExtend(source, target) {
var i, n;
if (!isArray(source)) {
return source;
}
if (isArray(target)) {
for (i = 0, n = target.length; i < n; i++) {
source.push(target[i]);
}
} else {
source.push(target);
}
return source;
}
// shim to reach bound context
if (Function.prototype.bind) {
nativeBind = Function.prototype.bind;
Function.prototype.bind = function(oThis) {
var boundFn = nativeBind.apply(this, arguments);
boundFn.boundThis = oThis;
boundFn.boundArgs = Array.prototype.slice.call(arguments, 1);
boundFn.targetFunction = this;
return boundFn;
};
}
/* end helper */
if (typeof define === 'function' && define.amd) {
define(function () {
return EventEmitter;
});
} else if (typeof module !== 'undefined') {
module.exports = EventEmitter;
} else {
global.EventEmitter = EventEmitter;
}
})(this);