@irrelon/emitter 3.0.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +1 -1
- package/.eslintignore +3 -1
- package/.eslintrc.json +17 -56
- package/dist/Emitter.d.ts +205 -0
- package/dist/Emitter.js +339 -777
- package/package.json +54 -50
- package/readme.md +44 -11
- package/src/Emitter.ts +672 -0
- package/tests/Emitter.test.js +277 -110
- package/tsconfig.json +29 -0
- package/src/Emitter.js +0 -884
package/src/Emitter.ts
ADDED
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
/*
|
|
2
|
+
The MIT License (MIT)
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2014 Irrelon Software Limited
|
|
5
|
+
http://www.irrelon.com
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice, url and this permission notice shall be included in
|
|
15
|
+
all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
23
|
+
THE SOFTWARE.
|
|
24
|
+
|
|
25
|
+
Source: https://github.com/irrelon/emitter
|
|
26
|
+
|
|
27
|
+
Changelog:
|
|
28
|
+
Version 5.0.0:
|
|
29
|
+
Added typescript types, switched from mocha to jest for test
|
|
30
|
+
running.
|
|
31
|
+
Breaking change. When using makeEmitter, prototypeMode is now
|
|
32
|
+
false by default. Simply pass makeEmitter(SomeClass.prototype)
|
|
33
|
+
instead of makeEmitter(SomeClass) to modify a prototype.
|
|
34
|
+
Version 4.0.1:
|
|
35
|
+
Updated library to use new ES6 functionality making Overload()
|
|
36
|
+
less useful so it can be removed as a dependency.
|
|
37
|
+
Version 4.0.0:
|
|
38
|
+
Breaking change. Library now has named exports `Emitter` and
|
|
39
|
+
`makeEmitter`. `Emitter` is an ES6 class that can be extended
|
|
40
|
+
and `makeEmitter` is a function that takes an object or class
|
|
41
|
+
and does what `Emitter(someObject)` used to do. This change is
|
|
42
|
+
to support being able to extend `Emitter` as a base class.
|
|
43
|
+
Version 3.1.0:
|
|
44
|
+
Changed order of execution so that listeners that are listening
|
|
45
|
+
against a specific ID get called before the general catch-all
|
|
46
|
+
listeners. Renamed package to @irrelon/emitter.
|
|
47
|
+
Version 2.0.11:
|
|
48
|
+
Added cancelStatic method to allow cancelling a static event
|
|
49
|
+
Version 2.0.7:
|
|
50
|
+
Fixed UMD module support
|
|
51
|
+
Version 2.0.6:
|
|
52
|
+
Added UMD module support
|
|
53
|
+
Version 2.0.5:
|
|
54
|
+
Added bower version number
|
|
55
|
+
Version 2.0.4:
|
|
56
|
+
Allow instantiation as independent instance, updated unit tests
|
|
57
|
+
Version 2.0.3:
|
|
58
|
+
Documentation updates, published to bower
|
|
59
|
+
Version 2.0.2:
|
|
60
|
+
Documentation updates
|
|
61
|
+
Version 2.0.1:
|
|
62
|
+
Bug fix in this._emitters usage
|
|
63
|
+
Version 2.0.0:
|
|
64
|
+
Big update to bring in line with latest developments in other projects. Event emitter can
|
|
65
|
+
now use deferEmit(), emitId(), emitStatic(), emitStaticId(), willEmit(), willEmitId().
|
|
66
|
+
Version 1.1.9:
|
|
67
|
+
Updated changelog correctly
|
|
68
|
+
Version 1.1.8:
|
|
69
|
+
Removed tons of dependencies wrongly included in main dependencies, have moved to devDependencies section of package.json
|
|
70
|
+
Version 1.1.0:
|
|
71
|
+
Added support for overloaded methods
|
|
72
|
+
Added support for events with ids
|
|
73
|
+
Version 1.0.2:
|
|
74
|
+
Removed AMD support, added browserify support
|
|
75
|
+
Added package.json
|
|
76
|
+
Added once() method
|
|
77
|
+
Added hasListener() method
|
|
78
|
+
Published to NPM as irrelon-emitter
|
|
79
|
+
Version 1.0.1:
|
|
80
|
+
Added ability to extend any object with eventing capability
|
|
81
|
+
Added AMD / Require.js support
|
|
82
|
+
Added Node.js support
|
|
83
|
+
Version 1.0.0:
|
|
84
|
+
First commit
|
|
85
|
+
*/
|
|
86
|
+
export interface InternalEventListenersType {
|
|
87
|
+
[eventName: string|symbol]: InternalEventListenersByIdType
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface InternalEventListenersByIdType {
|
|
91
|
+
[idName: string|symbol]: Array<EventListenerType>
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export type EventListenerType = (...rest: any[]) => any;
|
|
95
|
+
|
|
96
|
+
export interface InternalEventEmittersType {
|
|
97
|
+
[eventName: string|symbol]: Array<EventEmitterType>
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface EventEmitterType {
|
|
101
|
+
id: string|symbol
|
|
102
|
+
args: any
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @class Emitter
|
|
107
|
+
* @constructor
|
|
108
|
+
*/
|
|
109
|
+
export class Emitter {
|
|
110
|
+
private _emitting: boolean;
|
|
111
|
+
private _noEmitDefer: boolean;
|
|
112
|
+
private _listeners: InternalEventListenersType
|
|
113
|
+
private _emitters: InternalEventEmittersType
|
|
114
|
+
private _eventRemovalQueue: Array<(...rest: any[]) => any>;
|
|
115
|
+
private _deferTimeout: any;
|
|
116
|
+
|
|
117
|
+
constructor (obj?: any) {
|
|
118
|
+
this._noEmitDefer = false;
|
|
119
|
+
this._emitting = false;
|
|
120
|
+
this._listeners = {};
|
|
121
|
+
this._emitters = {};
|
|
122
|
+
this._eventRemovalQueue = [];
|
|
123
|
+
|
|
124
|
+
if (obj !== undefined) {
|
|
125
|
+
return makeEmitter(obj);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Attach an event listener to the passed event only if the passed
|
|
131
|
+
* id matches the document id for the event being fired.
|
|
132
|
+
* @memberOf Emitter
|
|
133
|
+
* @method on
|
|
134
|
+
* @param {String} event The name of the event to listen for.
|
|
135
|
+
* @param {*} id The document id to match against.
|
|
136
|
+
* @param {Function} listener The method to call when the event is fired.
|
|
137
|
+
* @returns {Emitter} The emitter instance.
|
|
138
|
+
*/
|
|
139
|
+
_on (event: string|symbol, id: string|symbol, listener: (...rest: any[]) => any) {
|
|
140
|
+
const generateEmitterTimeout = (emitter: EventEmitterType) => {
|
|
141
|
+
setTimeout(() => {
|
|
142
|
+
listener.apply(this, emitter.args);
|
|
143
|
+
}, 1);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
this._listeners = this._listeners || {};
|
|
147
|
+
this._listeners[event] = this._listeners[event] || {};
|
|
148
|
+
this._listeners[event][id] = this._listeners[event][id] || [];
|
|
149
|
+
this._listeners[event][id].push(listener);
|
|
150
|
+
|
|
151
|
+
// Check for any static emitters, and fire the event if any exist
|
|
152
|
+
if (!this._emitters || !this._emitters[event] || !this._emitters[event].length) return this;
|
|
153
|
+
|
|
154
|
+
// Emit events for each emitter
|
|
155
|
+
this._emitters[event].forEach((emitter: EventEmitterType) => {
|
|
156
|
+
if (id === "*" || emitter.id === id) {
|
|
157
|
+
// Call the listener out of process so that any code that expects a listener
|
|
158
|
+
// to be called at some point in the future rather than immediately on registration
|
|
159
|
+
// will not fail
|
|
160
|
+
generateEmitterTimeout(emitter);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Attach an event listener to the passed event only if the passed
|
|
169
|
+
* id matches the document id for the event being fired.
|
|
170
|
+
* @memberOf Emitter
|
|
171
|
+
* @method once
|
|
172
|
+
* @param {String} event The name of the event to listen for.
|
|
173
|
+
* @param {*} id The document id to match against.
|
|
174
|
+
* @param {Function} listener The method to call when the event is fired.
|
|
175
|
+
* @returns {Emitter} The emitter instance.
|
|
176
|
+
*/
|
|
177
|
+
_once (event:string|symbol, id:string|symbol, listener: EventListenerType) {
|
|
178
|
+
let fired = false;
|
|
179
|
+
|
|
180
|
+
const internalCallback = (...rest: any[]) => {
|
|
181
|
+
if (fired) return;
|
|
182
|
+
|
|
183
|
+
fired = true;
|
|
184
|
+
this.off(event, id, internalCallback);
|
|
185
|
+
listener(...rest);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
return this.on(event, id, internalCallback);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Cancels an event listener based on an event name, id and listener function.
|
|
193
|
+
* @memberOf Emitter
|
|
194
|
+
* @method off
|
|
195
|
+
* @param {String} event The event to cancel listener for.
|
|
196
|
+
* @param {String} id The ID of the event to cancel listening for.
|
|
197
|
+
* @param {Function} listener The event listener function used in the on()
|
|
198
|
+
* or once() call to cancel.
|
|
199
|
+
* @returns {Emitter} The emitter instance.
|
|
200
|
+
*/
|
|
201
|
+
_off (event: string|symbol, id: string|symbol, listener?: EventListenerType) {
|
|
202
|
+
if (this._emitting) {
|
|
203
|
+
this._eventRemovalQueue = this._eventRemovalQueue || [];
|
|
204
|
+
this._eventRemovalQueue.push(() => {
|
|
205
|
+
this.off(event, id, listener);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (!this._listeners || !this._listeners[event] || !this._listeners[event][id]) return this;
|
|
212
|
+
|
|
213
|
+
if (id && !listener) {
|
|
214
|
+
if (id === "*") {
|
|
215
|
+
delete this._listeners[event];
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// No listener provided, delete all listeners
|
|
220
|
+
delete this._listeners[event][id];
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (!listener) return this;
|
|
225
|
+
|
|
226
|
+
const arr = this._listeners[event][id] || [];
|
|
227
|
+
const index = arr.indexOf(listener);
|
|
228
|
+
|
|
229
|
+
if (index > -1) {
|
|
230
|
+
arr.splice(index, 1);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Attach an event listener to the passed event only if the passed
|
|
236
|
+
* id matches the document id for the event being fired.
|
|
237
|
+
* @memberOf Emitter
|
|
238
|
+
* @method on
|
|
239
|
+
* @param {String} event The name of the event to listen for.
|
|
240
|
+
* @param {*} id The document id to match against.
|
|
241
|
+
* @param {Function} listener The method to call when the event is fired.
|
|
242
|
+
* @returns {Emitter} The emitter instance.
|
|
243
|
+
*/
|
|
244
|
+
on (event: string|symbol, id: string|symbol, listener: EventListenerType): any;
|
|
245
|
+
on (event: string|symbol, listener: (...rest: any[]) => any): any;
|
|
246
|
+
on (event: string|symbol, ...rest: any[]) {
|
|
247
|
+
const restTypes = rest.map((arg) => typeof arg);
|
|
248
|
+
|
|
249
|
+
if (restTypes[0] === "function") {
|
|
250
|
+
return this._on.call(this, event, "*", rest[0]);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return this._on.call(this, event, rest[0], rest[1]);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Attach an event listener to the passed event only if the passed
|
|
258
|
+
* id matches the document id for the event being fired.
|
|
259
|
+
* @memberOf Emitter
|
|
260
|
+
* @method once
|
|
261
|
+
* @param {String} event The name of the event to listen for.
|
|
262
|
+
* @param {*} id The document id to match against.
|
|
263
|
+
* @param {Function} listener The method to call when the event is fired.
|
|
264
|
+
* @returns {Emitter} The emitter instance.
|
|
265
|
+
*/
|
|
266
|
+
once (event: string, id: string|symbol, listener: EventListenerType): any;
|
|
267
|
+
once (event: string, listener: (...rest: any[]) => any): any;
|
|
268
|
+
once (event: string, ...rest: any[]) {
|
|
269
|
+
const restTypes = rest.map((arg) => typeof arg);
|
|
270
|
+
|
|
271
|
+
if (restTypes[0] === "function") {
|
|
272
|
+
return this._once.call(this, event, "*", rest[0]);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return this._once.call(this, event, rest[0], rest[1]);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Attach an event listener to the passed event only if the passed
|
|
280
|
+
* id matches the document id for the event being fired.
|
|
281
|
+
* @memberOf Emitter
|
|
282
|
+
* @method once
|
|
283
|
+
* @param {String} event The name of the event to listen for.
|
|
284
|
+
* @param {*} id The document id to match against.
|
|
285
|
+
* @param {Function} listener The method to call when the event is fired.
|
|
286
|
+
* @returns {Emitter} The emitter instance.
|
|
287
|
+
*/
|
|
288
|
+
one (event: string, id: string|symbol, listener: EventListenerType): any;
|
|
289
|
+
one (event: string, listener: (...rest: any[]) => any): any;
|
|
290
|
+
one (event: string, ...rest: any[]) {
|
|
291
|
+
const restTypes = rest.map((arg) => typeof arg);
|
|
292
|
+
|
|
293
|
+
if (restTypes[0] === "function") {
|
|
294
|
+
this.off(event);
|
|
295
|
+
return this._on.call(this, event, "*", rest[0]);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
this.off(event, rest[0]);
|
|
299
|
+
return this._on.call(this, event, rest[0], rest[1]);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Cancels an event listener based on an event name, id and listener function.
|
|
304
|
+
* @memberOf Emitter
|
|
305
|
+
* @method off
|
|
306
|
+
* @param event The event to cancel listener for.
|
|
307
|
+
* @param id The ID of the event to cancel listening for.
|
|
308
|
+
* @param listener The event listener function used in the on()
|
|
309
|
+
* or once() call to cancel.
|
|
310
|
+
* @returns {Emitter} The emitter instance.
|
|
311
|
+
*/
|
|
312
|
+
off (event: string|symbol, id: string|symbol, listener?: EventListenerType): any;
|
|
313
|
+
off (event: string|symbol, listener: (...rest: any[]) => any): any;
|
|
314
|
+
off (event: string|symbol): any;
|
|
315
|
+
off (event: string|symbol, ...rest: any[]) {
|
|
316
|
+
if (rest.length === 0) {
|
|
317
|
+
// Only event was provided
|
|
318
|
+
return this._off.call(this, event, "*");
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const restTypes = rest.map((arg) => typeof arg);
|
|
322
|
+
|
|
323
|
+
if (restTypes[0] === "function") {
|
|
324
|
+
return this._off.call(this, event, "*", rest[0]);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return this._off.call(this, event, rest[0], rest[1]);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Handles emitting events, is an internal method not called directly.
|
|
332
|
+
* @param {String} event The name of the event to emit.
|
|
333
|
+
* @param {*} data The data to emit with the event.
|
|
334
|
+
* @returns {Emitter} The emitter instance.
|
|
335
|
+
* @private
|
|
336
|
+
*/
|
|
337
|
+
emit (event: string|symbol, ...data: any[]) {
|
|
338
|
+
const id = "*";
|
|
339
|
+
this._listeners = this._listeners || {};
|
|
340
|
+
this._emitting = true;
|
|
341
|
+
|
|
342
|
+
if (this._listeners[event] && this._listeners[event][id]) {
|
|
343
|
+
// Handle global emit
|
|
344
|
+
const arr = this._listeners[event][id];
|
|
345
|
+
const arrCount = arr.length;
|
|
346
|
+
|
|
347
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
348
|
+
// Check we have a function to execute
|
|
349
|
+
const tmpFunc = arr[arrIndex];
|
|
350
|
+
|
|
351
|
+
if (typeof tmpFunc === "function") {
|
|
352
|
+
tmpFunc.call(this, ...data);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
this._emitting = false;
|
|
358
|
+
this._processRemovalQueue();
|
|
359
|
+
|
|
360
|
+
return this;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
emitId (event: string|symbol, id: string|symbol, ...data: any[]) {
|
|
364
|
+
this._listeners = this._listeners || {};
|
|
365
|
+
this._emitting = true;
|
|
366
|
+
|
|
367
|
+
if (!this._listeners || !this._listeners[event]) {
|
|
368
|
+
this._emitting = false;
|
|
369
|
+
this._processRemovalQueue();
|
|
370
|
+
|
|
371
|
+
return this;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Handle id emit
|
|
375
|
+
if (this._listeners[event][id]) {
|
|
376
|
+
const arr = this._listeners[event][id];
|
|
377
|
+
const arrCount = arr.length;
|
|
378
|
+
|
|
379
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
380
|
+
// Check we have a function to execute
|
|
381
|
+
const tmpFunc = arr[arrIndex];
|
|
382
|
+
|
|
383
|
+
if (typeof tmpFunc === "function") {
|
|
384
|
+
tmpFunc.call(this, ...data);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Handle global emit
|
|
390
|
+
if (this._listeners[event]["*"]) {
|
|
391
|
+
const arr = this._listeners[event]["*"];
|
|
392
|
+
const arrCount = arr.length;
|
|
393
|
+
|
|
394
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
395
|
+
// Check we have a function to execute
|
|
396
|
+
const tmpFunc = arr[arrIndex];
|
|
397
|
+
|
|
398
|
+
if (typeof tmpFunc === "function") {
|
|
399
|
+
tmpFunc.call(this, ...data);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
this._emitting = false;
|
|
405
|
+
this._processRemovalQueue();
|
|
406
|
+
|
|
407
|
+
return this;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Creates a static event emitter that is emitted whenever an
|
|
412
|
+
* event listener is created with the same name.
|
|
413
|
+
* @param {String} event The name of the event to emit.
|
|
414
|
+
* @param {*} data The data to emit with the event.
|
|
415
|
+
* @returns {Emitter} The emitter instance.
|
|
416
|
+
*/
|
|
417
|
+
emitStatic (event: string|symbol, ...data: any[]) {
|
|
418
|
+
this._emitters = this._emitters || {};
|
|
419
|
+
this._emitters[event] = this._emitters[event] || [];
|
|
420
|
+
this._emitters[event].push({
|
|
421
|
+
"id": "*",
|
|
422
|
+
"args": data
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
this.emit(event, ...data);
|
|
426
|
+
|
|
427
|
+
this._processRemovalQueue();
|
|
428
|
+
|
|
429
|
+
return this;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Creates a static event emitter that is emitted whenever an
|
|
434
|
+
* event listener is created with the same name and id.
|
|
435
|
+
* @param {String} event The name of the event to emit.
|
|
436
|
+
* @param {String} id The id of the event to emit.
|
|
437
|
+
* @param {*} data The data to emit with the event.
|
|
438
|
+
* @returns {Emitter} The emitter instance.
|
|
439
|
+
*/
|
|
440
|
+
emitStaticId (event: string|symbol, id: string|symbol, ...data: any[]) {
|
|
441
|
+
if (!id) throw new Error("Missing id from emitStaticId call!");
|
|
442
|
+
|
|
443
|
+
this._emitters = this._emitters || {};
|
|
444
|
+
this._emitters[event] = this._emitters[event] || [];
|
|
445
|
+
this._emitters[event].push({
|
|
446
|
+
id,
|
|
447
|
+
"args": data
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
this.emitId(event, id, ...data);
|
|
451
|
+
|
|
452
|
+
return this;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Handles removing emitters, is an internal method not called directly.
|
|
457
|
+
* @param {String} event The event to remove static emitter for.
|
|
458
|
+
* @returns {Emitter} The emitter instance.
|
|
459
|
+
* @private
|
|
460
|
+
*/
|
|
461
|
+
cancelStatic (event: string|symbol) {
|
|
462
|
+
this._emitters = this._emitters || {};
|
|
463
|
+
this._emitters[event] = [];
|
|
464
|
+
|
|
465
|
+
return this;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Checks if an event has any event listeners or not.
|
|
470
|
+
* @memberOf Emitter
|
|
471
|
+
* @method willEmit
|
|
472
|
+
* @param {String} event The name of the event to check for.
|
|
473
|
+
* @returns {boolean} True if one or more event listeners are registered for
|
|
474
|
+
* the event. False if none are found.
|
|
475
|
+
*/
|
|
476
|
+
willEmit (event: string|symbol) {
|
|
477
|
+
const id = "*";
|
|
478
|
+
|
|
479
|
+
if (!this._listeners || !this._listeners[event]) {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const arr = this._listeners[event][id];
|
|
484
|
+
const arrCount = arr.length;
|
|
485
|
+
|
|
486
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
487
|
+
// Check we have a function to execute
|
|
488
|
+
const tmpFunc = arr[arrIndex];
|
|
489
|
+
|
|
490
|
+
if (typeof tmpFunc === "function") {
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return false;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Checks if an event has any event listeners or not based on the passed id.
|
|
500
|
+
* @memberOf Emitter
|
|
501
|
+
* @method willEmitId
|
|
502
|
+
* @param {String} event The name of the event to check for.
|
|
503
|
+
* @param {String} id The event ID to check for.
|
|
504
|
+
* @returns {boolean} True if one or more event listeners are registered for
|
|
505
|
+
* the event. False if none are found.
|
|
506
|
+
*/
|
|
507
|
+
willEmitId (event: string|symbol, id: string|symbol) {
|
|
508
|
+
if (!this._listeners || !this._listeners[event]) {
|
|
509
|
+
return false;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// Handle id emit
|
|
513
|
+
if (this._listeners[event][id]) {
|
|
514
|
+
const arr = this._listeners[event][id];
|
|
515
|
+
const arrCount = arr.length;
|
|
516
|
+
|
|
517
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
518
|
+
// Check we have a function to execute
|
|
519
|
+
const tmpFunc = arr[arrIndex];
|
|
520
|
+
|
|
521
|
+
if (typeof tmpFunc === "function") {
|
|
522
|
+
return true;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// Handle global emit
|
|
528
|
+
if (this._listeners[event]["*"]) {
|
|
529
|
+
const arr = this._listeners[event]["*"];
|
|
530
|
+
const arrCount = arr.length;
|
|
531
|
+
|
|
532
|
+
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
533
|
+
// Check we have a function to execute
|
|
534
|
+
const tmpFunc = arr[arrIndex];
|
|
535
|
+
|
|
536
|
+
if (typeof tmpFunc === "function") {
|
|
537
|
+
return true;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
return false;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Queues an event to be fired. This has automatic de-bouncing so that any
|
|
547
|
+
* events of the same type that occur within 100 milliseconds of a previous
|
|
548
|
+
* one will all be wrapped into a single emit rather than emitting tons of
|
|
549
|
+
* events for the same eventName. Only the data from the last de-bounced event
|
|
550
|
+
* will be emitted.
|
|
551
|
+
* @memberOf Emitter
|
|
552
|
+
* @method deferEmit
|
|
553
|
+
* @param {String} eventName The name of the event to emit.
|
|
554
|
+
* @param {*=} data Optional data to emit with the event.
|
|
555
|
+
* @returns {Emitter} The emitter instance.
|
|
556
|
+
*/
|
|
557
|
+
deferEmit (eventName: string|symbol, ...data: any[]) {
|
|
558
|
+
if (this._noEmitDefer) {
|
|
559
|
+
this.emit(eventName, ...data);
|
|
560
|
+
return this;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// Check for an existing timeout
|
|
564
|
+
this._deferTimeout = this._deferTimeout || {};
|
|
565
|
+
|
|
566
|
+
if (this._deferTimeout[eventName]) {
|
|
567
|
+
clearTimeout(this._deferTimeout[eventName]);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// Set a timeout
|
|
571
|
+
this._deferTimeout[eventName] = setTimeout(() => {
|
|
572
|
+
this.emit(eventName, ...data);
|
|
573
|
+
}, 100);
|
|
574
|
+
|
|
575
|
+
return this;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* If events are cleared with the off() method while the event emitter is
|
|
580
|
+
* actively processing any events then the off() calls get added to a
|
|
581
|
+
* queue to be executed after the event emitter is finished. This stops
|
|
582
|
+
* errors that might occur by potentially modifying the event queue while
|
|
583
|
+
* the emitter is running through them. This method is called after the
|
|
584
|
+
* event emitter is finished processing.
|
|
585
|
+
* @private
|
|
586
|
+
* @returns {undefined}
|
|
587
|
+
*/
|
|
588
|
+
_processRemovalQueue () {
|
|
589
|
+
if (!this._eventRemovalQueue || !this._eventRemovalQueue.length) {
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// Execute each removal call
|
|
594
|
+
for (let i = 0; i < this._eventRemovalQueue.length; i++) {
|
|
595
|
+
this._eventRemovalQueue[i]();
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
// Clear the removal queue
|
|
599
|
+
this._eventRemovalQueue = [];
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Makes the passed class or object into an emitter by modifying either the
|
|
605
|
+
* prototype or the actual object to include event emitter methods.
|
|
606
|
+
* @param obj The object / function / class to add event methods to.
|
|
607
|
+
* If none is provided a new object will be created. This allows you to use
|
|
608
|
+
* new Emitter() to generate an event emitter that is not tied to any other
|
|
609
|
+
* object or class.
|
|
610
|
+
* @param prototypeMode Defaults to true. Set to true to add emitter
|
|
611
|
+
* methods to the the passed object"s prototype property e.g. obj.prototype.on
|
|
612
|
+
* = emitter.on. Set to false to add emitter methods the object directly e.g.
|
|
613
|
+
* obj.on = emitter.on.
|
|
614
|
+
* @returns {Object} The newly augmented object.
|
|
615
|
+
*/
|
|
616
|
+
type MakeEmitter = {
|
|
617
|
+
(obj: Record<string, unknown>|boolean|undefined, prototypeMode: boolean): any,
|
|
618
|
+
(obj: boolean): any,
|
|
619
|
+
(obj: undefined): any
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
export const makeEmitter: MakeEmitter = (obj: Record<string, unknown>|boolean|undefined, prototypeMode = false) => {
|
|
623
|
+
let operateOnObject;
|
|
624
|
+
|
|
625
|
+
if (obj === undefined && prototypeMode === undefined) {
|
|
626
|
+
obj = {};
|
|
627
|
+
prototypeMode = false;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
if (typeof obj === "boolean" && prototypeMode === undefined) {
|
|
631
|
+
prototypeMode = obj;
|
|
632
|
+
obj = {};
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
if (prototypeMode === undefined) {
|
|
636
|
+
prototypeMode = false;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
if (typeof obj !== "object" && typeof obj !== "function") {
|
|
640
|
+
throw new Error("Cannot operate on passed value, it is not an object, function or class!");
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
if (prototypeMode) {
|
|
644
|
+
if (obj.prototype === undefined) {
|
|
645
|
+
throw new Error("Cannot modify prototype of passed object, it has no prototype property!");
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
operateOnObject = obj.prototype as Record<string, unknown>;
|
|
649
|
+
} else {
|
|
650
|
+
operateOnObject = obj;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// Convert the object to have eventing capability
|
|
654
|
+
operateOnObject.on = Emitter.prototype.on;
|
|
655
|
+
operateOnObject.off = Emitter.prototype.off;
|
|
656
|
+
operateOnObject.one = Emitter.prototype.one;
|
|
657
|
+
operateOnObject.once = Emitter.prototype.once;
|
|
658
|
+
operateOnObject.emit = Emitter.prototype.emit;
|
|
659
|
+
operateOnObject.emitId = Emitter.prototype.emitId;
|
|
660
|
+
operateOnObject.emitStatic = Emitter.prototype.emitStatic;
|
|
661
|
+
operateOnObject.emitStaticId = Emitter.prototype.emitStaticId;
|
|
662
|
+
operateOnObject.cancelStatic = Emitter.prototype.cancelStatic;
|
|
663
|
+
operateOnObject.deferEmit = Emitter.prototype.deferEmit;
|
|
664
|
+
operateOnObject.willEmit = Emitter.prototype.willEmit;
|
|
665
|
+
operateOnObject.willEmitId = Emitter.prototype.willEmitId;
|
|
666
|
+
operateOnObject._processRemovalQueue = Emitter.prototype._processRemovalQueue;
|
|
667
|
+
operateOnObject._on = Emitter.prototype._on;
|
|
668
|
+
operateOnObject._off = Emitter.prototype._off;
|
|
669
|
+
operateOnObject._once = Emitter.prototype._once;
|
|
670
|
+
|
|
671
|
+
return obj;
|
|
672
|
+
};
|