@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/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
- none = "!+_NONE",
111
- cancel = "!+_CANCEL"
110
+ none = "!+_NONE",
111
+ cancel = "!+_CANCEL"
112
112
  }
113
113
 
114
114
  export type EventListenerCallback<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> = (
115
- ...args: Parameters<EventCallbackFunctionSignature>
115
+ ...args: Parameters<EventCallbackFunctionSignature>
116
116
  ) => ReturnType<EventCallbackFunctionSignature>;
117
117
 
118
118
  export interface EventStaticEmitterObject<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> {
119
- id: string;
120
- args: Parameters<EventCallbackFunctionSignature>;
119
+ id: string;
120
+ args: Parameters<EventCallbackFunctionSignature>;
121
121
  }
122
122
 
123
123
  export interface EmitterAnyEventsInterface {
124
- [key: string]: AnyFunction;
124
+ [key: string]: AnyFunction;
125
125
  }
126
126
 
127
127
  export type EmitterEventsInterface<T> = {
128
- [K in keyof T]: T[K];
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 EmitterEventsInterface<EventsInterface> = EmitterAnyEventsInterface> {
140
+ export class Emitter<EventsInterface extends EmitterEventsWithStar<EventsInterface> = EmitterAnyEventsInterface> {
135
141
  _eventsEmitting: boolean = false;
136
142
  _eventRemovalQueue: any[] = [];
137
143
  _eventListeners?: {
138
- [EventName in keyof EventsInterface]?: Record<string, EventListenerCallback<EventsInterface[EventName]>[]>;
139
- } = {};
144
+ [EventName in keyof EventsInterface]?: Record<string, EventListenerCallback<EventsInterface[EventName]>[]>;
145
+ } = {};
140
146
  _eventStaticEmitters: {
141
- [EventName in keyof EventsInterface]?: EventStaticEmitterObject<EventsInterface[EventName]>[];
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
- * Attach an event listener to the passed event only if the passed
148
- * id matches the id for the event being fired.
149
- * @param eventName The name of the event to listen for.
150
- * @param id The id to match against.
151
- * @param listener The method to call when the event is fired.
152
- * @returns The emitter instance.
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
- !this._eventStaticEmitters[eventName] ||
174
- !this._eventStaticEmitters[eventName]!.length
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
- * Attach an event listener to the passed event only if the passed
195
- * id matches the document id for the event being fired.
196
- * @param eventName The name of the event to listen for.
197
- * @param id The id to match against.
198
- * @param listener The method to call when the event is fired.
199
- * @returns The emitter instance.
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
- * Cancels an event listener based on an event name, id and listener function.
226
- * @param eventName The event to cancel listener for.
227
- * @param id The ID of the event to cancel listening for.
228
- * @param listener The event listener function used in the on()
229
- * or once() call to cancel.
230
- * @returns The emitter instance.
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
- * Attach an event listener to the passed event only if the passed
279
- * id matches the document id for the event being fired.
280
- * @param eventName The name of the event to listen for.
281
- * @param id The id to match against.
282
- * @param listener The method to call when the event is fired.
283
- * @returns The emitter instance.
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
- eventName: EventName,
287
- id: string,
288
- listener: EventListenerCallback<EventsInterface[EventName]>
289
- ): this;
300
+ eventName: EventName,
301
+ id: string,
302
+ listener: EventListenerCallback<EventsInterface[EventName]>
303
+ ): this;
290
304
  on<EventName extends keyof EventsInterface>(
291
- eventName: EventName,
292
- listener: EventListenerCallback<EventsInterface[EventName]>
293
- ): this;
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
- * Attach an event listener to the passed event only if the passed
308
- * id matches the document id for the event being fired.
309
- * @param eventName The name of the event to listen for.
310
- * @param id The id to match against.
311
- * @param listener
312
- * @returns The emitter instance.
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
- eventName: EventName,
316
- id: string,
317
- listener: EventListenerCallback<EventsInterface[EventName]>
318
- ): this;
329
+ eventName: EventName,
330
+ id: string,
331
+ listener: EventListenerCallback<EventsInterface[EventName]>
332
+ ): this;
319
333
  once<EventName extends keyof EventsInterface>(
320
- eventName: EventName,
321
- listener: EventListenerCallback<EventsInterface[EventName]>
322
- ): this;
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
- * Overwrites any previous event listeners so that if the event fires, only
335
- * the listener you pass will be called. If you pass an id along with the
336
- * listener, only listeners for the id will be overwritten. This is similar
337
- * to calling `.off(eventName)` then `on(eventName, listener)` since
338
- * `off(eventName)` will cancel any previous event listeners for the passed
339
- * event name.
340
- * @param eventName The name of the event to listen for.
341
- * @param id The id to match against.
342
- * @param listener The method to call when the event is fired.
343
- * @returns The emitter instance.
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
- eventName: EventName,
347
- id: string,
348
- listener: EventListenerCallback<EventsInterface[EventName]>
349
- ): this;
360
+ eventName: EventName,
361
+ id: string,
362
+ listener: EventListenerCallback<EventsInterface[EventName]>
363
+ ): this;
350
364
  overwrite<EventName extends keyof EventsInterface>(
351
- eventName: EventName,
352
- listener: EventListenerCallback<EventsInterface[EventName]>
353
- ): this;
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
- * Cancels an event listener based on an event name, id and listener function.
368
- * @param eventName The event to cancel listener for.
369
- * @param id The ID of the event to cancel listening for.
370
- * @param listener The event listener function used in the on()
371
- * or once() call to cancel.
372
- * @returns The emitter instance.
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
- eventName: EventName,
376
- id: string,
377
- listener?: EventListenerCallback<EventsInterface[EventName]>
378
- ): this;
397
+ eventName: EventName,
398
+ id: string,
399
+ listener?: EventListenerCallback<EventsInterface[EventName]>
400
+ ): this;
379
401
  off<EventName extends keyof EventsInterface>(
380
- eventName: EventName,
381
- listener?: EventListenerCallback<EventsInterface[EventName]>
382
- ): this;
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
- * Emit an event by name.
432
- * @param eventName The name of the event to emit.
433
- * @param {...any} data The arguments to send to any listening methods.
434
- * If you are sending multiple arguments, separate them with a comma so
435
- * that they are received by the function as separate arguments.
436
- * @return {number}
437
- * @example #Emit an Event
438
- * // Emit the event named "hello"
439
- * myEntity.emit('hello');
440
- * @example #Emit an Event With Data Object
441
- * // Emit the event named "hello"
442
- * myEntity.emit('hello', {moo: true});
443
- * @example #Emit an Event With Multiple Data Values
444
- * // Emit the event named "hello"
445
- * myEntity.emit('hello', {moo: true}, 'someString');
446
- * @example #Listen for Event Data
447
- * // Set a listener to listen for the data (multiple values emitted
448
- * // from an event are passed as function arguments)
449
- * myEntity.on('hello', function (arg1, arg2) {
450
- * console.log(arg1, arg2);
451
- * }
452
- *
453
- * // Emit the event named "hello"
454
- * myEntity.emit('hello', 'data1', 'data2');
455
- *
456
- * // The console output is:
457
- * // data1, data2
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
- // Handle id emit
479
- if (this._eventListeners[eventName]![id]) {
480
- const arr = this._eventListeners[eventName]![id];
481
- emitterResult = emitterResult.concat(this._emitToArrayOfListeners<EventName>(arr, data));
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
- // Handle global emit if the id passed wasn't already a global (*) id
485
- if (id !== "*" && this._eventListeners[eventName]!["*"]) {
486
- const arr = this._eventListeners[eventName]!["*"];
487
- emitterResult = emitterResult.concat(this._emitToArrayOfListeners<EventName>(arr, data));
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
- * Creates a persistent emitter record that will fire a listener if
498
- * one is added for this event after the emitStatic() call has been
499
- * made.
500
- * @param eventName The name of the event to emit.
501
- * @param {...any} data Optional arguments to emit with the event.
502
- * @returns The emitter instance.
503
- * @private
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 global emit
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
- this._eventStaticEmitters[eventName]!.push({
533
- id: "*",
534
- args: data
535
- });
659
+ this._eventStaticEmitters[eventName]!.push({
660
+ id: "*",
661
+ args: data
662
+ });
536
663
 
537
- this._processRemovalQueue();
664
+ this._processRemovalQueue();
538
665
 
539
- return this;
666
+ return this;
540
667
  }
541
668
 
542
669
  /**
543
- * Creates a persistent emitter record that will fire a listener if
544
- * one is added for this event after the emitStatic() call has been
545
- * made.
546
- * @param eventName The name of the event to emit.
547
- * @param id The id of the event to emit.
548
- * @param {...any} data Optional arguments to emit with the event.
549
- * @returns The emitter instance.
550
- * @private
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 global emit
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
- this._eventStaticEmitters[eventName]!.push({
599
- id,
600
- args: data
601
- });
725
+ this._eventStaticEmitters[eventName]!.push({
726
+ id,
727
+ args: data
728
+ });
602
729
 
603
- this._processRemovalQueue();
730
+ this._processRemovalQueue();
604
731
 
605
- return this;
732
+ return this;
606
733
  }
607
734
 
608
735
  /**
609
- * Handles removing emitters, is an internal method not called directly.
610
- * @param eventName The event to remove static emitter for.
611
- * @returns The emitter instance.
612
- * @private
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
- * Checks if an event has any event listeners or not.
623
- * @param eventName The name of the event to check for.
624
- * @returns True if one or more event listeners are registered for
625
- * the event. False if none are found.
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
- * Checks if an event has any event listeners or not based on the passed id.
651
- * @param eventName The name of the event to check for.
652
- * @param id The event ID to check for.
653
- * @returns True if one or more event listeners are registered for
654
- * the event. False if none are found.
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 global emit
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
- * Queues an event to be fired. This has automatic de-bouncing so that any
696
- * events of the same type that occur within 100 milliseconds of a previous
697
- * one will all be wrapped into a single emit rather than emitting tons of
698
- * events for lots of chained inserts etc. Only the data from the last
699
- * de-bounced event will be emitted.
700
- * @param eventName The name of the event to emit.
701
- * @param {...any} data Optional arguments to emit with the event.
702
- * @returns The emitter instance.
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
- * Returns true if the result from an emit() call passed as the first
729
- * argument contains a cancellation flag.
730
- * @param resultArr
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
- * If events are cleared with the off() method while the event emitter is
738
- * actively processing any events then the off() calls get added to a
739
- * queue to be executed after the event emitter is finished. This stops
740
- * errors that might occur by potentially mutating the event listener array
741
- * while the emitter is running through them. This method is called after
742
- * the event emitter is finished processing.
743
- * @private
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;