@irrelon/emitter 6.0.0 → 6.1.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/dist/cjs/src/Emitter.d.ts +64 -5
- package/dist/cjs/src/Emitter.js +113 -18
- package/dist/cjs/tests/Emitter.test.js +66 -13
- package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/esm/src/Emitter.d.ts +64 -5
- package/dist/esm/src/Emitter.js +111 -16
- package/dist/esm/tests/Emitter.test.js +66 -13
- package/dist/esm/tsconfig-esm.tsbuildinfo +1 -1
- package/eslint.config.mjs +42 -0
- package/jest.config.js +7 -0
- package/package.json +14 -25
- package/readme.md +20 -3
- package/src/Emitter.ts +330 -201
- package/tests/Emitter.test.ts +244 -163
- package/.editorconfig +0 -11
- package/fixPaths.cjs +0 -183
- /package/{.eslintrc.json → eslint.json} +0 -0
package/src/Emitter.ts
CHANGED
|
@@ -107,50 +107,56 @@ export type AnyFunction<ReturnType = any> = (...args: any[]) => ReturnType;
|
|
|
107
107
|
export type AnyConstructor = new (...args: any[]) => any;
|
|
108
108
|
|
|
109
109
|
export enum ListenerReturnFlag {
|
|
110
|
-
|
|
111
|
-
|
|
110
|
+
none = "!+_NONE",
|
|
111
|
+
cancel = "!+_CANCEL"
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
export type EventListenerCallback<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> = (
|
|
115
|
-
|
|
115
|
+
...args: Parameters<EventCallbackFunctionSignature>
|
|
116
116
|
) => ReturnType<EventCallbackFunctionSignature>;
|
|
117
117
|
|
|
118
118
|
export interface EventStaticEmitterObject<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> {
|
|
119
|
-
|
|
120
|
-
|
|
119
|
+
id: string;
|
|
120
|
+
args: Parameters<EventCallbackFunctionSignature>;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
export interface EmitterAnyEventsInterface {
|
|
124
|
-
|
|
124
|
+
[key: string]: AnyFunction;
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
export type EmitterEventsInterface<T> = {
|
|
128
|
-
|
|
128
|
+
[K in keyof T]: T[K];
|
|
129
129
|
};
|
|
130
130
|
|
|
131
|
+
interface WithStar {
|
|
132
|
+
"*"?: (eventName: string, ...args: any[]) => void;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export type EmitterEventsWithStar<T> = EmitterEventsInterface<T> & WithStar;
|
|
136
|
+
|
|
131
137
|
/**
|
|
132
138
|
* Creates a new class with the capability to emit events.
|
|
133
139
|
*/
|
|
134
|
-
export class Emitter<EventsInterface extends
|
|
140
|
+
export class Emitter<EventsInterface extends EmitterEventsWithStar<EventsInterface> = EmitterAnyEventsInterface> {
|
|
135
141
|
_eventsEmitting: boolean = false;
|
|
136
142
|
_eventRemovalQueue: any[] = [];
|
|
137
143
|
_eventListeners?: {
|
|
138
|
-
|
|
139
|
-
|
|
144
|
+
[EventName in keyof EventsInterface]?: Record<string, EventListenerCallback<EventsInterface[EventName]>[]>;
|
|
145
|
+
} = {};
|
|
140
146
|
_eventStaticEmitters: {
|
|
141
|
-
|
|
142
|
-
|
|
147
|
+
[EventName in keyof EventsInterface]?: EventStaticEmitterObject<EventsInterface[EventName]>[];
|
|
148
|
+
} = {};
|
|
143
149
|
_eventsAllowDefer: boolean = false;
|
|
144
150
|
_eventsDeferTimeouts: Record<any, number> = {};
|
|
145
151
|
|
|
146
152
|
/**
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
153
|
+
* Attach an event listener to the passed event only if the passed
|
|
154
|
+
* id matches the id for the event being fired.
|
|
155
|
+
* @param eventName The name of the event to listen for.
|
|
156
|
+
* @param id The id to match against.
|
|
157
|
+
* @param listener The method to call when the event is fired.
|
|
158
|
+
* @returns The emitter instance.
|
|
159
|
+
*/
|
|
154
160
|
_on<EventName extends keyof EventsInterface> (
|
|
155
161
|
eventName: EventName,
|
|
156
162
|
id: string,
|
|
@@ -170,8 +176,8 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
170
176
|
// Check for any static emitters, and fire the event if any exist
|
|
171
177
|
if (
|
|
172
178
|
!this._eventStaticEmitters ||
|
|
173
|
-
|
|
174
|
-
|
|
179
|
+
!this._eventStaticEmitters[eventName] ||
|
|
180
|
+
!this._eventStaticEmitters[eventName]!.length
|
|
175
181
|
)
|
|
176
182
|
return this;
|
|
177
183
|
|
|
@@ -191,13 +197,13 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
191
197
|
}
|
|
192
198
|
|
|
193
199
|
/**
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
200
|
+
* Attach an event listener to the passed event only if the passed
|
|
201
|
+
* id matches the document id for the event being fired.
|
|
202
|
+
* @param eventName The name of the event to listen for.
|
|
203
|
+
* @param id The id to match against.
|
|
204
|
+
* @param listener The method to call when the event is fired.
|
|
205
|
+
* @returns The emitter instance.
|
|
206
|
+
*/
|
|
201
207
|
_once<EventName extends keyof EventsInterface> (
|
|
202
208
|
eventName: EventName,
|
|
203
209
|
id: string,
|
|
@@ -222,13 +228,13 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
222
228
|
}
|
|
223
229
|
|
|
224
230
|
/**
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
231
|
+
* Cancels an event listener based on an event name, id and listener function.
|
|
232
|
+
* @param eventName The event to cancel listener for.
|
|
233
|
+
* @param id The ID of the event to cancel listening for.
|
|
234
|
+
* @param listener The event listener function used in the on()
|
|
235
|
+
* or once() call to cancel.
|
|
236
|
+
* @returns The emitter instance.
|
|
237
|
+
*/
|
|
232
238
|
_off<EventName extends keyof EventsInterface> (
|
|
233
239
|
eventName: EventName,
|
|
234
240
|
id: string,
|
|
@@ -275,22 +281,30 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
275
281
|
}
|
|
276
282
|
|
|
277
283
|
/**
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
284
|
+
* Creates an event listener that listens for any and all events
|
|
285
|
+
* emitted.
|
|
286
|
+
*/
|
|
287
|
+
onAny (listener: EventListenerCallback) {
|
|
288
|
+
this.on("*", listener);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Attach an event listener to the passed event only if the passed
|
|
293
|
+
* id matches the document id for the event being fired.
|
|
294
|
+
* @param eventName The name of the event to listen for.
|
|
295
|
+
* @param id The id to match against.
|
|
296
|
+
* @param listener The method to call when the event is fired.
|
|
297
|
+
* @returns The emitter instance.
|
|
298
|
+
*/
|
|
285
299
|
on<EventName extends keyof EventsInterface>(
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
300
|
+
eventName: EventName,
|
|
301
|
+
id: string,
|
|
302
|
+
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
303
|
+
): this;
|
|
290
304
|
on<EventName extends keyof EventsInterface>(
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
305
|
+
eventName: EventName,
|
|
306
|
+
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
307
|
+
): this;
|
|
294
308
|
on<EventName extends keyof EventsInterface> (
|
|
295
309
|
eventName: EventName,
|
|
296
310
|
idOrListener: string | EventListenerCallback<EventsInterface[EventName]>,
|
|
@@ -304,22 +318,22 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
304
318
|
}
|
|
305
319
|
|
|
306
320
|
/**
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
321
|
+
* Attach an event listener to the passed event only if the passed
|
|
322
|
+
* id matches the document id for the event being fired.
|
|
323
|
+
* @param eventName The name of the event to listen for.
|
|
324
|
+
* @param id The id to match against.
|
|
325
|
+
* @param listener
|
|
326
|
+
* @returns The emitter instance.
|
|
327
|
+
*/
|
|
314
328
|
once<EventName extends keyof EventsInterface>(
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
329
|
+
eventName: EventName,
|
|
330
|
+
id: string,
|
|
331
|
+
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
332
|
+
): this;
|
|
319
333
|
once<EventName extends keyof EventsInterface>(
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
334
|
+
eventName: EventName,
|
|
335
|
+
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
336
|
+
): this;
|
|
323
337
|
once<EventName extends keyof EventsInterface> (eventName: EventName, ...rest: any[]): this {
|
|
324
338
|
const restTypes = rest.map((arg) => typeof arg);
|
|
325
339
|
|
|
@@ -331,26 +345,26 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
331
345
|
}
|
|
332
346
|
|
|
333
347
|
/**
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
348
|
+
* Overwrites any previous event listeners so that if the event fires, only
|
|
349
|
+
* the listener you pass will be called. If you pass an id along with the
|
|
350
|
+
* listener, only listeners for the id will be overwritten. This is similar
|
|
351
|
+
* to calling `.off(eventName)` then `on(eventName, listener)` since
|
|
352
|
+
* `off(eventName)` will cancel any previous event listeners for the passed
|
|
353
|
+
* event name.
|
|
354
|
+
* @param eventName The name of the event to listen for.
|
|
355
|
+
* @param id The id to match against.
|
|
356
|
+
* @param listener The method to call when the event is fired.
|
|
357
|
+
* @returns The emitter instance.
|
|
358
|
+
*/
|
|
345
359
|
overwrite<EventName extends keyof EventsInterface>(
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
360
|
+
eventName: EventName,
|
|
361
|
+
id: string,
|
|
362
|
+
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
363
|
+
): this;
|
|
350
364
|
overwrite<EventName extends keyof EventsInterface>(
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
365
|
+
eventName: EventName,
|
|
366
|
+
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
367
|
+
): this;
|
|
354
368
|
overwrite<EventName extends keyof EventsInterface> (eventName: EventName, ...rest: any[]) {
|
|
355
369
|
const restTypes = rest.map((arg) => typeof arg);
|
|
356
370
|
|
|
@@ -364,22 +378,30 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
364
378
|
}
|
|
365
379
|
|
|
366
380
|
/**
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
381
|
+
* Removes an event listener that listens for any and all events
|
|
382
|
+
* emitted.
|
|
383
|
+
*/
|
|
384
|
+
offAny (listener: EventListenerCallback) {
|
|
385
|
+
this.off("*", listener);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Cancels an event listener based on an event name, id and listener function.
|
|
390
|
+
* @param eventName The event to cancel listener for.
|
|
391
|
+
* @param id The ID of the event to cancel listening for.
|
|
392
|
+
* @param listener The event listener function used in the on()
|
|
393
|
+
* or once() call to cancel.
|
|
394
|
+
* @returns The emitter instance.
|
|
395
|
+
*/
|
|
374
396
|
off<EventName extends keyof EventsInterface>(
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
397
|
+
eventName: EventName,
|
|
398
|
+
id: string,
|
|
399
|
+
listener?: EventListenerCallback<EventsInterface[EventName]>
|
|
400
|
+
): this;
|
|
379
401
|
off<EventName extends keyof EventsInterface>(
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
402
|
+
eventName: EventName,
|
|
403
|
+
listener?: EventListenerCallback<EventsInterface[EventName]>
|
|
404
|
+
): this;
|
|
383
405
|
off<EventName extends keyof EventsInterface>(eventName: EventName): this;
|
|
384
406
|
off<EventName extends keyof EventsInterface> (eventName: EventName, ...rest: any[]) {
|
|
385
407
|
if (rest.length === 0) {
|
|
@@ -400,6 +422,13 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
400
422
|
return this._off(eventName, rest[0], rest[1]);
|
|
401
423
|
}
|
|
402
424
|
|
|
425
|
+
_emitToSingleListener<EventName extends keyof EventsInterface> (
|
|
426
|
+
listener: EventListenerCallback<EventsInterface[EventName]>,
|
|
427
|
+
data: Parameters<EventsInterface[EventName]>
|
|
428
|
+
): ReturnType<EventsInterface[EventName]> | ListenerReturnFlag {
|
|
429
|
+
return listener(...data);
|
|
430
|
+
}
|
|
431
|
+
|
|
403
432
|
_emitToArrayOfListeners<EventName extends keyof EventsInterface> (
|
|
404
433
|
arr: EventListenerCallback<EventsInterface[EventName]>[],
|
|
405
434
|
data: Parameters<EventsInterface[EventName]>
|
|
@@ -428,34 +457,120 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
428
457
|
}
|
|
429
458
|
|
|
430
459
|
/**
|
|
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
|
-
|
|
460
|
+
* Call an event listener by name. The `rpc()` function differs from `emit()` in
|
|
461
|
+
* that it will only call and return data from the first event listener for the
|
|
462
|
+
* event rather than all event listeners. If you have more than one event listener
|
|
463
|
+
* assigned to the event, all others except the first will be ignored and will
|
|
464
|
+
* not receive the event.
|
|
465
|
+
* @param eventName The name of the event to call the listener for.
|
|
466
|
+
* @param {...any} data The arguments to send to the listening method.
|
|
467
|
+
* If you are sending multiple arguments, separate them with a comma so
|
|
468
|
+
* that they are received by the function as separate arguments.
|
|
469
|
+
* @return {number}
|
|
470
|
+
* @example #Call the Event Listener
|
|
471
|
+
* // Emit the event named "hello"
|
|
472
|
+
* myEntity.emit('hello');
|
|
473
|
+
* @example #Call the Event Listener With Data Object
|
|
474
|
+
* // Emit the event named "hello"
|
|
475
|
+
* myEntity.emit('hello', {moo: true});
|
|
476
|
+
* @example #Call the Event Listener With Multiple Data Values
|
|
477
|
+
* // Emit the event named "hello"
|
|
478
|
+
* myEntity.emit('hello', {moo: true}, 'someString');
|
|
479
|
+
* @example #Register the Listener for Event Data
|
|
480
|
+
* // Set a listener to listen for the data (multiple values emitted
|
|
481
|
+
* // from an event are passed as function arguments)
|
|
482
|
+
* myEntity.on('hello', function (arg1, arg2) {
|
|
483
|
+
* console.log("Arguments:", arg1, arg2);
|
|
484
|
+
* return "foo";
|
|
485
|
+
* }
|
|
486
|
+
*
|
|
487
|
+
* // Emit the event named "hello"
|
|
488
|
+
* const result = myEntity.emit('hello', 'data1', 'data2');
|
|
489
|
+
* console.log("Result:", result);
|
|
490
|
+
*
|
|
491
|
+
* // The console output is:
|
|
492
|
+
* // Arguments: data1 data2
|
|
493
|
+
* // Result: "foo"
|
|
494
|
+
*/
|
|
495
|
+
rpc<EventName extends keyof EventsInterface> (
|
|
496
|
+
eventName: EventName,
|
|
497
|
+
...data: Parameters<EventsInterface[EventName]>
|
|
498
|
+
): ReturnType<EventsInterface[EventName]> | ListenerReturnFlag {
|
|
499
|
+
return this.rpcId<EventName>(eventName, "*", ...data);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
rpcId<EventName extends keyof EventsInterface> (
|
|
503
|
+
eventName: EventName,
|
|
504
|
+
id: string,
|
|
505
|
+
...data: Parameters<EventsInterface[EventName]>
|
|
506
|
+
): ReturnType<EventsInterface[EventName]> | ListenerReturnFlag {
|
|
507
|
+
if (!this._eventListeners || !this._eventListeners[eventName]) {
|
|
508
|
+
throw new Error("Cannot make an rpc call without at least one event listener and none were found");
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
let emitterResult: ReturnType<EventsInterface[EventName]> | ListenerReturnFlag;
|
|
512
|
+
this._eventsEmitting = true;
|
|
513
|
+
|
|
514
|
+
if (this._eventListeners[eventName]![id]) {
|
|
515
|
+
// Handle id emit
|
|
516
|
+
const arr = this._eventListeners[eventName]![id];
|
|
517
|
+
emitterResult = this._emitToSingleListener<EventName>(arr[0], data);
|
|
518
|
+
} else if (id !== "*" && this._eventListeners[eventName]!["*"]) {
|
|
519
|
+
// Handle wildcard emit if the id passed wasn't already a wildcard (*) id
|
|
520
|
+
const arr = this._eventListeners[eventName]!["*"];
|
|
521
|
+
emitterResult = this._emitToSingleListener<EventName>(arr[0], data);
|
|
522
|
+
} else {
|
|
523
|
+
if (id === "*") {
|
|
524
|
+
throw new Error(
|
|
525
|
+
`Cannot make an rpc call to event "${eventName as string}" without at least one event listener and none were found, register a listener via on("${eventName as string}", () => { ...some listener code })`
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
throw new Error(
|
|
530
|
+
`Cannot make an rpc call to event "${eventName as string}" and id "${id}" without at least one event listener and none were found, register a listener via on("${eventName as string}", "${id}", () => { ...some listener code })`
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
this._eventsEmitting = false;
|
|
535
|
+
this._processRemovalQueue();
|
|
536
|
+
|
|
537
|
+
return emitterResult;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Emit an event by name.
|
|
542
|
+
* @param eventName The name of the event to emit.
|
|
543
|
+
* @param {...any} data The arguments to send to any listening methods.
|
|
544
|
+
* If you are sending multiple arguments, separate them with a comma so
|
|
545
|
+
* that they are received by the function as separate arguments.
|
|
546
|
+
* @return {number}
|
|
547
|
+
* @example #Emit an Event
|
|
548
|
+
* // Emit the event named "hello"
|
|
549
|
+
* myEntity.emit('hello');
|
|
550
|
+
* @example #Emit an Event With Data Object
|
|
551
|
+
* // Emit the event named "hello"
|
|
552
|
+
* myEntity.emit('hello', {moo: true});
|
|
553
|
+
* @example #Emit an Event With Multiple Data Values
|
|
554
|
+
* // Emit the event named "hello"
|
|
555
|
+
* myEntity.emit('hello', {moo: true}, 'someString');
|
|
556
|
+
* @example #Listen for Event Data
|
|
557
|
+
* // Set a listener to listen for the data (multiple values emitted
|
|
558
|
+
* // from an event are passed as function arguments)
|
|
559
|
+
* myEntity.on('hello', function (arg1, arg2) {
|
|
560
|
+
* console.log("Arguments:", arg1, arg2);
|
|
561
|
+
* return "foo";
|
|
562
|
+
* }
|
|
563
|
+
*
|
|
564
|
+
* // Emit the event named "hello"
|
|
565
|
+
* // The result is returned as an array of all return values
|
|
566
|
+
* // from all the listeners for the event
|
|
567
|
+
* const result = myEntity.emit('hello', 'data1', 'data2');
|
|
568
|
+
* console.log("Result:", result);
|
|
569
|
+
*
|
|
570
|
+
* // The console output is:
|
|
571
|
+
* // Arguments: data1 data2
|
|
572
|
+
* // Result: ["foo"]
|
|
573
|
+
*/
|
|
459
574
|
emit<EventName extends keyof EventsInterface> (
|
|
460
575
|
eventName: EventName,
|
|
461
576
|
...data: Parameters<EventsInterface[EventName]>
|
|
@@ -468,23 +583,35 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
468
583
|
id: string,
|
|
469
584
|
...data: Parameters<EventsInterface[EventName]>
|
|
470
585
|
): (ReturnType<EventsInterface[EventName]> | ListenerReturnFlag)[] {
|
|
471
|
-
if (!this._eventListeners || !this._eventListeners[eventName]) {
|
|
586
|
+
if (!this._eventListeners || (!this._eventListeners[eventName] && !this._eventListeners["*"])) {
|
|
472
587
|
return [];
|
|
473
588
|
}
|
|
474
589
|
|
|
475
590
|
let emitterResult: (ReturnType<EventsInterface[EventName]> | ListenerReturnFlag)[] = [];
|
|
476
591
|
this._eventsEmitting = true;
|
|
477
592
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
593
|
+
if (this._eventListeners[eventName]) {
|
|
594
|
+
// Handle id emit
|
|
595
|
+
if (this._eventListeners[eventName]![id]) {
|
|
596
|
+
const arr = this._eventListeners[eventName]![id];
|
|
597
|
+
emitterResult = emitterResult.concat(this._emitToArrayOfListeners<EventName>(arr, data));
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// Handle id wildcard emit if the id passed wasn't already a wildcard (*) id
|
|
601
|
+
if (id !== "*" && this._eventListeners[eventName]!["*"]) {
|
|
602
|
+
const arr = this._eventListeners[eventName]!["*"];
|
|
603
|
+
emitterResult = emitterResult.concat(this._emitToArrayOfListeners<EventName>(arr, data));
|
|
604
|
+
}
|
|
482
605
|
}
|
|
483
606
|
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
607
|
+
if (this._eventListeners["*"]) {
|
|
608
|
+
const arr = this._eventListeners["*"]!["*"];
|
|
609
|
+
emitterResult = emitterResult.concat(
|
|
610
|
+
this._emitToArrayOfListeners<EventName>(
|
|
611
|
+
arr as EventListenerCallback<EventsInterface[EventName]>[],
|
|
612
|
+
[eventName, ...data] as Parameters<EventsInterface[EventName]>
|
|
613
|
+
)
|
|
614
|
+
);
|
|
488
615
|
}
|
|
489
616
|
|
|
490
617
|
this._eventsEmitting = false;
|
|
@@ -494,14 +621,14 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
494
621
|
}
|
|
495
622
|
|
|
496
623
|
/**
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
624
|
+
* Creates a persistent emitter record that will fire a listener if
|
|
625
|
+
* one is added for this event after the emitStatic() call has been
|
|
626
|
+
* made.
|
|
627
|
+
* @param eventName The name of the event to emit.
|
|
628
|
+
* @param {...any} data Optional arguments to emit with the event.
|
|
629
|
+
* @returns The emitter instance.
|
|
630
|
+
* @private
|
|
631
|
+
*/
|
|
505
632
|
emitStatic<EventName extends keyof EventsInterface> (
|
|
506
633
|
eventName: EventName,
|
|
507
634
|
...data: Parameters<EventsInterface[EventName]>
|
|
@@ -511,7 +638,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
511
638
|
this._eventsEmitting = true;
|
|
512
639
|
|
|
513
640
|
if (this._eventListeners[eventName] && this._eventListeners[eventName]![id]) {
|
|
514
|
-
// Handle
|
|
641
|
+
// Handle id wildcard emit
|
|
515
642
|
const arr = this._eventListeners[eventName]![id];
|
|
516
643
|
const arrCount = arr.length;
|
|
517
644
|
|
|
@@ -529,26 +656,26 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
529
656
|
|
|
530
657
|
this._eventStaticEmitters = this._eventStaticEmitters || {};
|
|
531
658
|
this._eventStaticEmitters[eventName] = this._eventStaticEmitters[eventName] || [];
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
659
|
+
this._eventStaticEmitters[eventName]!.push({
|
|
660
|
+
id: "*",
|
|
661
|
+
args: data
|
|
662
|
+
});
|
|
536
663
|
|
|
537
|
-
|
|
664
|
+
this._processRemovalQueue();
|
|
538
665
|
|
|
539
|
-
|
|
666
|
+
return this;
|
|
540
667
|
}
|
|
541
668
|
|
|
542
669
|
/**
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
670
|
+
* Creates a persistent emitter record that will fire a listener if
|
|
671
|
+
* one is added for this event after the emitStatic() call has been
|
|
672
|
+
* made.
|
|
673
|
+
* @param eventName The name of the event to emit.
|
|
674
|
+
* @param id The id of the event to emit.
|
|
675
|
+
* @param {...any} data Optional arguments to emit with the event.
|
|
676
|
+
* @returns The emitter instance.
|
|
677
|
+
* @private
|
|
678
|
+
*/
|
|
552
679
|
emitStaticId<EventName extends keyof EventsInterface> (
|
|
553
680
|
eventName: EventName,
|
|
554
681
|
id: string,
|
|
@@ -575,7 +702,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
575
702
|
}
|
|
576
703
|
}
|
|
577
704
|
|
|
578
|
-
// Handle
|
|
705
|
+
// Handle wildcard emit
|
|
579
706
|
if (this._eventListeners[eventName]!["*"]) {
|
|
580
707
|
const arr = this._eventListeners[eventName]!["*"];
|
|
581
708
|
const arrCount = arr.length;
|
|
@@ -595,22 +722,22 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
595
722
|
|
|
596
723
|
this._eventStaticEmitters = this._eventStaticEmitters || {};
|
|
597
724
|
this._eventStaticEmitters[eventName] = this._eventStaticEmitters[eventName] || [];
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
725
|
+
this._eventStaticEmitters[eventName]!.push({
|
|
726
|
+
id,
|
|
727
|
+
args: data
|
|
728
|
+
});
|
|
602
729
|
|
|
603
|
-
|
|
730
|
+
this._processRemovalQueue();
|
|
604
731
|
|
|
605
|
-
|
|
732
|
+
return this;
|
|
606
733
|
}
|
|
607
734
|
|
|
608
735
|
/**
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
736
|
+
* Handles removing emitters, is an internal method not called directly.
|
|
737
|
+
* @param eventName The event to remove static emitter for.
|
|
738
|
+
* @returns The emitter instance.
|
|
739
|
+
* @private
|
|
740
|
+
*/
|
|
614
741
|
cancelStatic<EventName extends keyof EventsInterface> (eventName: EventName) {
|
|
615
742
|
this._eventStaticEmitters = this._eventStaticEmitters || {};
|
|
616
743
|
this._eventStaticEmitters[eventName] = [];
|
|
@@ -619,11 +746,11 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
619
746
|
}
|
|
620
747
|
|
|
621
748
|
/**
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
749
|
+
* Checks if an event has any event listeners or not.
|
|
750
|
+
* @param eventName The name of the event to check for.
|
|
751
|
+
* @returns True if one or more event listeners are registered for
|
|
752
|
+
* the event. False if none are found.
|
|
753
|
+
*/
|
|
627
754
|
willEmit<EventName extends keyof EventsInterface> (eventName: EventName) {
|
|
628
755
|
const id = "*";
|
|
629
756
|
|
|
@@ -647,12 +774,12 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
647
774
|
}
|
|
648
775
|
|
|
649
776
|
/**
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
777
|
+
* Checks if an event has any event listeners or not based on the passed id.
|
|
778
|
+
* @param eventName The name of the event to check for.
|
|
779
|
+
* @param id The event ID to check for.
|
|
780
|
+
* @returns True if one or more event listeners are registered for
|
|
781
|
+
* the event. False if none are found.
|
|
782
|
+
*/
|
|
656
783
|
willEmitId<EventName extends keyof EventsInterface> (eventName: EventName, id: string) {
|
|
657
784
|
if (!this._eventListeners || !this._eventListeners[eventName]) {
|
|
658
785
|
return false;
|
|
@@ -673,7 +800,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
673
800
|
}
|
|
674
801
|
}
|
|
675
802
|
|
|
676
|
-
// Handle
|
|
803
|
+
// Handle wildcard emit
|
|
677
804
|
if (this._eventListeners[eventName]!["*"]) {
|
|
678
805
|
const arr = this._eventListeners[eventName]!["*"];
|
|
679
806
|
const arrCount = arr.length;
|
|
@@ -692,15 +819,15 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
692
819
|
}
|
|
693
820
|
|
|
694
821
|
/**
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
822
|
+
* Queues an event to be fired. This has automatic de-bouncing so that any
|
|
823
|
+
* events of the same type that occur within 100 milliseconds of a previous
|
|
824
|
+
* one will all be wrapped into a single emit rather than emitting tons of
|
|
825
|
+
* events for lots of chained inserts etc. Only the data from the last
|
|
826
|
+
* de-bounced event will be emitted.
|
|
827
|
+
* @param eventName The name of the event to emit.
|
|
828
|
+
* @param {...any} data Optional arguments to emit with the event.
|
|
829
|
+
* @returns The emitter instance.
|
|
830
|
+
*/
|
|
704
831
|
deferEmit<EventName extends keyof EventsInterface> (
|
|
705
832
|
eventName: EventName,
|
|
706
833
|
...data: Parameters<EventsInterface[EventName]>
|
|
@@ -725,23 +852,23 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
725
852
|
}
|
|
726
853
|
|
|
727
854
|
/**
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
855
|
+
* Returns true if the result from an emit() call passed as the first
|
|
856
|
+
* argument contains a cancellation flag.
|
|
857
|
+
* @param resultArr
|
|
858
|
+
*/
|
|
732
859
|
didCancel (resultArr: (unknown | ListenerReturnFlag)[]) {
|
|
733
860
|
return resultArr.findIndex((result) => result === ListenerReturnFlag.cancel) > -1;
|
|
734
861
|
}
|
|
735
862
|
|
|
736
863
|
/**
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
864
|
+
* If events are cleared with the off() method while the event emitter is
|
|
865
|
+
* actively processing any events then the off() calls get added to a
|
|
866
|
+
* queue to be executed after the event emitter is finished. This stops
|
|
867
|
+
* errors that might occur by potentially mutating the event listener array
|
|
868
|
+
* while the emitter is running through them. This method is called after
|
|
869
|
+
* the event emitter is finished processing.
|
|
870
|
+
* @private
|
|
871
|
+
*/
|
|
745
872
|
_processRemovalQueue () {
|
|
746
873
|
if (!this._eventRemovalQueue || !this._eventRemovalQueue.length) {
|
|
747
874
|
return;
|
|
@@ -795,7 +922,9 @@ export function makeEmitter (obj: any, prototypeMode?: any) {
|
|
|
795
922
|
|
|
796
923
|
// Convert the object prototype to have eventing capability
|
|
797
924
|
operateOnObject.on = Emitter.prototype.on;
|
|
925
|
+
operateOnObject.onAny = Emitter.prototype.onAny;
|
|
798
926
|
operateOnObject.off = Emitter.prototype.off;
|
|
927
|
+
operateOnObject.offAny = Emitter.prototype.offAny;
|
|
799
928
|
operateOnObject.once = Emitter.prototype.once;
|
|
800
929
|
operateOnObject.emit = Emitter.prototype.emit;
|
|
801
930
|
operateOnObject.emitId = Emitter.prototype.emitId;
|