@irrelon/emitter 6.0.1 → 6.2.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 +4 -7
- package/dist/cjs/src/Emitter.d.ts +51 -14
- package/dist/cjs/src/Emitter.js +83 -29
- package/dist/cjs/tests/Emitter.test.js +77 -30
- package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/esm/src/Emitter.d.ts +51 -14
- package/dist/esm/src/Emitter.js +83 -26
- package/dist/esm/tests/Emitter.test.js +77 -30
- package/dist/esm/tsconfig-esm.tsbuildinfo +1 -1
- package/doc/Emitter.html +451 -1551
- package/doc/Emitter.js.html +601 -850
- package/doc/index.html +1 -1
- package/doc/scripts/linenumber.js +9 -9
- package/doc/styles/jsdoc-default.css +2 -2
- package/eslint.config.mjs +42 -0
- package/jest.config.js +7 -0
- package/package.json +18 -27
- package/readme.md +10 -1
- package/src/Emitter.d.ts +290 -0
- package/src/Emitter.js +600 -0
- package/src/Emitter.ts +167 -161
- package/tests/Emitter.test.d.ts +1 -0
- package/tests/Emitter.test.js +472 -0
- package/tests/Emitter.test.ts +143 -64
- package/tsconfig.json +8 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/.prettierrc.yml +0 -26
- package/doc/deferEmit.html +0 -258
- package/doc/emitStatic.html +0 -444
- package/doc/emitStaticId.html +0 -685
- package/doc/global.html +0 -991
- package/doc/global.html#emit +0 -442
- package/doc/off.html +0 -857
- package/doc/on.html +0 -453
- package/doc/once.html +0 -453
- package/doc/willEmit.html +0 -234
- package/doc/willEmitId.html +0 -257
- /package/{.eslintrc.json → eslint.json} +0 -0
package/src/Emitter.ts
CHANGED
|
@@ -25,9 +25,31 @@
|
|
|
25
25
|
Source: https://github.com/irrelon/emitter
|
|
26
26
|
|
|
27
27
|
Changelog:
|
|
28
|
-
Version 6.
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
Version 6.2.0:
|
|
29
|
+
Added ability to emit events via both our emitter and via
|
|
30
|
+
`window.dispatchEvent()` so that applications that don't have
|
|
31
|
+
access to an event emitter instance can still listen for events
|
|
32
|
+
when emitted by our emitter. This option can be switched on or
|
|
33
|
+
off via the `dispatchWindowEventsEnabled()` function. The default
|
|
34
|
+
is `false` (off).
|
|
35
|
+
|
|
36
|
+
Added the ability to set a prefix to events which are emitted
|
|
37
|
+
via the window.dispatchEvent() method. For example, if you set
|
|
38
|
+
the prefix to "myCompany.myApp.myEmitterName" then emit an event
|
|
39
|
+
via emit() called "myEvent", the dispatched event name on the
|
|
40
|
+
window object will be "myCompany.myApp.myEmitterName.myEvent".
|
|
41
|
+
Event names using standard Emitter functions are unaffected.
|
|
42
|
+
For instance, you would still listen for `myEvent` via
|
|
43
|
+
`emitter.on("myEvent", ...)` and emit it via
|
|
44
|
+
`emitter.emit("myEvent", ...)`.
|
|
45
|
+
Version 6.1.0:
|
|
46
|
+
Added `onAny()` and `offAny()` methods which are analogous to
|
|
47
|
+
`on("*")` and `off("*")`.
|
|
48
|
+
Version 6.0.0:
|
|
49
|
+
Updated to allow event listeners to return data to the caller of
|
|
50
|
+
the emit() function, e.g.
|
|
51
|
+
|
|
52
|
+
`const results = emitter.emit("myEventName");`
|
|
31
53
|
|
|
32
54
|
The results are an array of all return values from any listener
|
|
33
55
|
functions that were called by the event "myEventName". This is
|
|
@@ -37,9 +59,10 @@
|
|
|
37
59
|
Listeners that return `ListenerReturnFlag.cancel` will cancel
|
|
38
60
|
the event call to any further listeners for that one event
|
|
39
61
|
emit.
|
|
40
|
-
Version 5.0.3 and 5.0.4:
|
|
41
|
-
to
|
|
42
|
-
listeners and emitting
|
|
62
|
+
Version 5.0.3 and 5.0.4:
|
|
63
|
+
Updated to allow specifying an events type to the Emitter to
|
|
64
|
+
allow complete type safety when registering listeners and emitting
|
|
65
|
+
events.
|
|
43
66
|
|
|
44
67
|
Updated the build and package to split between delivery of CJS
|
|
45
68
|
and ESM module types so that this package can be eiter imported
|
|
@@ -99,7 +122,7 @@
|
|
|
99
122
|
Version 1.0.1:
|
|
100
123
|
Added ability to extend any object with eventing capability
|
|
101
124
|
Added AMD / Require.js support
|
|
102
|
-
|
|
125
|
+
Added Node.js support
|
|
103
126
|
Version 1.0.0:
|
|
104
127
|
First commit
|
|
105
128
|
*/
|
|
@@ -111,9 +134,7 @@ export enum ListenerReturnFlag {
|
|
|
111
134
|
cancel = "!+_CANCEL"
|
|
112
135
|
}
|
|
113
136
|
|
|
114
|
-
export type EventListenerCallback<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> = (
|
|
115
|
-
...args: Parameters<EventCallbackFunctionSignature>
|
|
116
|
-
) => ReturnType<EventCallbackFunctionSignature>;
|
|
137
|
+
export type EventListenerCallback<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> = (...args: Parameters<EventCallbackFunctionSignature>) => ReturnType<EventCallbackFunctionSignature>;
|
|
117
138
|
|
|
118
139
|
export interface EventStaticEmitterObject<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> {
|
|
119
140
|
id: string;
|
|
@@ -128,10 +149,16 @@ export type EmitterEventsInterface<T> = {
|
|
|
128
149
|
[K in keyof T]: T[K];
|
|
129
150
|
};
|
|
130
151
|
|
|
152
|
+
export interface WithStar {
|
|
153
|
+
"*"?: (eventName: string, ...args: any[]) => void;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export type EmitterEventsWithStar<T> = EmitterEventsInterface<T> & WithStar;
|
|
157
|
+
|
|
131
158
|
/**
|
|
132
|
-
* Creates a new class with the
|
|
159
|
+
* Creates a new class with the ability to emit events.
|
|
133
160
|
*/
|
|
134
|
-
export class Emitter<EventsInterface extends
|
|
161
|
+
export class Emitter<EventsInterface extends EmitterEventsWithStar<EventsInterface> = EmitterAnyEventsInterface> {
|
|
135
162
|
_eventsEmitting: boolean = false;
|
|
136
163
|
_eventRemovalQueue: any[] = [];
|
|
137
164
|
_eventListeners?: {
|
|
@@ -142,6 +169,17 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
142
169
|
} = {};
|
|
143
170
|
_eventsAllowDefer: boolean = false;
|
|
144
171
|
_eventsDeferTimeouts: Record<any, number> = {};
|
|
172
|
+
_dispatchWindowEventsEnabled: boolean = false;
|
|
173
|
+
_dispatchPrefix?: string;
|
|
174
|
+
|
|
175
|
+
constructor (dispatchPrefix?: string, shouldDispatchWindowEvents: boolean = false) {
|
|
176
|
+
this._dispatchPrefix = dispatchPrefix;
|
|
177
|
+
this._dispatchWindowEventsEnabled = shouldDispatchWindowEvents;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
dispatchWindowEventsEnabled (shouldDispatchWindowEvents: boolean) {
|
|
181
|
+
this._dispatchWindowEventsEnabled = shouldDispatchWindowEvents;
|
|
182
|
+
}
|
|
145
183
|
|
|
146
184
|
/**
|
|
147
185
|
* Attach an event listener to the passed event only if the passed
|
|
@@ -151,11 +189,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
151
189
|
* @param listener The method to call when the event is fired.
|
|
152
190
|
* @returns The emitter instance.
|
|
153
191
|
*/
|
|
154
|
-
_on<EventName extends keyof EventsInterface>(
|
|
155
|
-
eventName: EventName,
|
|
156
|
-
id: string,
|
|
157
|
-
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
158
|
-
) {
|
|
192
|
+
_on<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>) {
|
|
159
193
|
const generateTimeout = (emitter: EventStaticEmitterObject<EventsInterface[EventName]>) => {
|
|
160
194
|
setTimeout(() => {
|
|
161
195
|
listener(...emitter.args);
|
|
@@ -168,12 +202,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
168
202
|
nonNullableEventListeners[id].push(listener);
|
|
169
203
|
|
|
170
204
|
// Check for any static emitters, and fire the event if any exist
|
|
171
|
-
if (
|
|
172
|
-
!this._eventStaticEmitters ||
|
|
173
|
-
!this._eventStaticEmitters[eventName] ||
|
|
174
|
-
!this._eventStaticEmitters[eventName]!.length
|
|
175
|
-
)
|
|
176
|
-
return this;
|
|
205
|
+
if (!this._eventStaticEmitters || !this._eventStaticEmitters[eventName] || !this._eventStaticEmitters[eventName]!.length) return this;
|
|
177
206
|
|
|
178
207
|
// Emit events for each emitter
|
|
179
208
|
for (let i = 0; i < this._eventStaticEmitters[eventName]!.length; i++) {
|
|
@@ -198,17 +227,11 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
198
227
|
* @param listener The method to call when the event is fired.
|
|
199
228
|
* @returns The emitter instance.
|
|
200
229
|
*/
|
|
201
|
-
_once<EventName extends keyof EventsInterface>(
|
|
202
|
-
eventName: EventName,
|
|
203
|
-
id: string,
|
|
204
|
-
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
205
|
-
) {
|
|
230
|
+
_once<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>) {
|
|
206
231
|
let fired = false;
|
|
207
232
|
let originalReturnData: ReturnType<EventsInterface[EventName]>;
|
|
208
233
|
|
|
209
|
-
const internalCallback: EventListenerCallback<EventsInterface[EventName]> = (
|
|
210
|
-
...args: Parameters<EventsInterface[EventName]>
|
|
211
|
-
): ReturnType<EventsInterface[EventName]> => {
|
|
234
|
+
const internalCallback: EventListenerCallback<EventsInterface[EventName]> = (...args: Parameters<EventsInterface[EventName]>): ReturnType<EventsInterface[EventName]> => {
|
|
212
235
|
if (fired) return originalReturnData;
|
|
213
236
|
|
|
214
237
|
fired = true;
|
|
@@ -229,14 +252,9 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
229
252
|
* or once() call to cancel.
|
|
230
253
|
* @returns The emitter instance.
|
|
231
254
|
*/
|
|
232
|
-
_off<EventName extends keyof EventsInterface>(
|
|
233
|
-
eventName: EventName,
|
|
234
|
-
id: string,
|
|
235
|
-
listener?: EventListenerCallback<EventsInterface[EventName]>
|
|
236
|
-
): this {
|
|
255
|
+
_off<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener?: EventListenerCallback<EventsInterface[EventName]>): this {
|
|
237
256
|
// If the event name doesn't have any listeners, exit early
|
|
238
|
-
if (!this._eventListeners || !this._eventListeners[eventName] || !this._eventListeners[eventName]![id])
|
|
239
|
-
return this;
|
|
257
|
+
if (!this._eventListeners || !this._eventListeners[eventName] || !this._eventListeners[eventName]![id]) return this;
|
|
240
258
|
|
|
241
259
|
// If we are emitting events at the moment, don't remove this listener
|
|
242
260
|
// until the process has completed, so we queue for removal instead
|
|
@@ -274,6 +292,14 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
274
292
|
return this;
|
|
275
293
|
}
|
|
276
294
|
|
|
295
|
+
/**
|
|
296
|
+
* Creates an event listener that listens for any and all events
|
|
297
|
+
* emitted.
|
|
298
|
+
*/
|
|
299
|
+
onAny (listener: EventListenerCallback) {
|
|
300
|
+
this.on("*", listener);
|
|
301
|
+
}
|
|
302
|
+
|
|
277
303
|
/**
|
|
278
304
|
* Attach an event listener to the passed event only if the passed
|
|
279
305
|
* id matches the document id for the event being fired.
|
|
@@ -282,20 +308,9 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
282
308
|
* @param listener The method to call when the event is fired.
|
|
283
309
|
* @returns The emitter instance.
|
|
284
310
|
*/
|
|
285
|
-
on<EventName extends keyof EventsInterface>(
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
289
|
-
): this;
|
|
290
|
-
on<EventName extends keyof EventsInterface>(
|
|
291
|
-
eventName: EventName,
|
|
292
|
-
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
293
|
-
): this;
|
|
294
|
-
on<EventName extends keyof EventsInterface>(
|
|
295
|
-
eventName: EventName,
|
|
296
|
-
idOrListener: string | EventListenerCallback<EventsInterface[EventName]>,
|
|
297
|
-
listener?: EventListenerCallback<EventsInterface[EventName]>
|
|
298
|
-
): this {
|
|
311
|
+
on<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
312
|
+
on<EventName extends keyof EventsInterface> (eventName: EventName, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
313
|
+
on<EventName extends keyof EventsInterface> (eventName: EventName, idOrListener: string | EventListenerCallback<EventsInterface[EventName]>, listener?: EventListenerCallback<EventsInterface[EventName]>): this {
|
|
299
314
|
if (typeof idOrListener === "string") {
|
|
300
315
|
return this._on(eventName, idOrListener, listener as EventListenerCallback<EventsInterface[EventName]>);
|
|
301
316
|
}
|
|
@@ -311,16 +326,9 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
311
326
|
* @param listener
|
|
312
327
|
* @returns The emitter instance.
|
|
313
328
|
*/
|
|
314
|
-
once<EventName extends keyof EventsInterface>(
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
318
|
-
): this;
|
|
319
|
-
once<EventName extends keyof EventsInterface>(
|
|
320
|
-
eventName: EventName,
|
|
321
|
-
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
322
|
-
): this;
|
|
323
|
-
once<EventName extends keyof EventsInterface>(eventName: EventName, ...rest: any[]): this {
|
|
329
|
+
once<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
330
|
+
once<EventName extends keyof EventsInterface> (eventName: EventName, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
331
|
+
once<EventName extends keyof EventsInterface> (eventName: EventName, ...rest: any[]): this {
|
|
324
332
|
const restTypes = rest.map((arg) => typeof arg);
|
|
325
333
|
|
|
326
334
|
if (restTypes[0] === "function") {
|
|
@@ -342,16 +350,9 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
342
350
|
* @param listener The method to call when the event is fired.
|
|
343
351
|
* @returns The emitter instance.
|
|
344
352
|
*/
|
|
345
|
-
overwrite<EventName extends keyof EventsInterface>(
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
349
|
-
): this;
|
|
350
|
-
overwrite<EventName extends keyof EventsInterface>(
|
|
351
|
-
eventName: EventName,
|
|
352
|
-
listener: EventListenerCallback<EventsInterface[EventName]>
|
|
353
|
-
): this;
|
|
354
|
-
overwrite<EventName extends keyof EventsInterface>(eventName: EventName, ...rest: any[]) {
|
|
353
|
+
overwrite<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
354
|
+
overwrite<EventName extends keyof EventsInterface> (eventName: EventName, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
355
|
+
overwrite<EventName extends keyof EventsInterface> (eventName: EventName, ...rest: any[]) {
|
|
355
356
|
const restTypes = rest.map((arg) => typeof arg);
|
|
356
357
|
|
|
357
358
|
if (restTypes[0] === "function") {
|
|
@@ -363,6 +364,14 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
363
364
|
return this._on(eventName, rest[0], rest[1]);
|
|
364
365
|
}
|
|
365
366
|
|
|
367
|
+
/**
|
|
368
|
+
* Removes an event listener that listens for any and all events
|
|
369
|
+
* emitted.
|
|
370
|
+
*/
|
|
371
|
+
offAny (listener: EventListenerCallback) {
|
|
372
|
+
this.off("*", listener);
|
|
373
|
+
}
|
|
374
|
+
|
|
366
375
|
/**
|
|
367
376
|
* Cancels an event listener based on an event name, id and listener function.
|
|
368
377
|
* @param eventName The event to cancel listener for.
|
|
@@ -371,17 +380,10 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
371
380
|
* or once() call to cancel.
|
|
372
381
|
* @returns The emitter instance.
|
|
373
382
|
*/
|
|
374
|
-
off<EventName extends keyof EventsInterface>(
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
): this;
|
|
379
|
-
off<EventName extends keyof EventsInterface>(
|
|
380
|
-
eventName: EventName,
|
|
381
|
-
listener?: EventListenerCallback<EventsInterface[EventName]>
|
|
382
|
-
): this;
|
|
383
|
-
off<EventName extends keyof EventsInterface>(eventName: EventName): this;
|
|
384
|
-
off<EventName extends keyof EventsInterface>(eventName: EventName, ...rest: any[]) {
|
|
383
|
+
off<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener?: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
384
|
+
off<EventName extends keyof EventsInterface> (eventName: EventName, listener?: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
385
|
+
off<EventName extends keyof EventsInterface> (eventName: EventName): this;
|
|
386
|
+
off<EventName extends keyof EventsInterface> (eventName: EventName, ...rest: any[]) {
|
|
385
387
|
if (rest.length === 0) {
|
|
386
388
|
// Only event was provided, use * as the id to mean "any without"
|
|
387
389
|
// a specific id
|
|
@@ -400,14 +402,14 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
400
402
|
return this._off(eventName, rest[0], rest[1]);
|
|
401
403
|
}
|
|
402
404
|
|
|
403
|
-
_emitToSingleListener<EventName extends keyof EventsInterface>(
|
|
405
|
+
_emitToSingleListener<EventName extends keyof EventsInterface> (
|
|
404
406
|
listener: EventListenerCallback<EventsInterface[EventName]>,
|
|
405
407
|
data: Parameters<EventsInterface[EventName]>
|
|
406
408
|
): ReturnType<EventsInterface[EventName]> | ListenerReturnFlag {
|
|
407
409
|
return listener(...data);
|
|
408
410
|
}
|
|
409
411
|
|
|
410
|
-
_emitToArrayOfListeners<EventName extends keyof EventsInterface>(
|
|
412
|
+
_emitToArrayOfListeners<EventName extends keyof EventsInterface> (
|
|
411
413
|
arr: EventListenerCallback<EventsInterface[EventName]>[],
|
|
412
414
|
data: Parameters<EventsInterface[EventName]>
|
|
413
415
|
): (ReturnType<EventsInterface[EventName]> | ListenerReturnFlag)[] {
|
|
@@ -434,6 +436,23 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
434
436
|
return resultArr;
|
|
435
437
|
}
|
|
436
438
|
|
|
439
|
+
/**
|
|
440
|
+
* Dispatches an event originating in our emitter to the browser's
|
|
441
|
+
* window object.
|
|
442
|
+
* @param eventName The name of the event to emit. This gets prefixed with the
|
|
443
|
+
* dispatch prefix if one is set.
|
|
444
|
+
* @param data
|
|
445
|
+
*/
|
|
446
|
+
_emitToWindowDispatch<EventName extends keyof EventsInterface> (eventName: EventName, data: any) {
|
|
447
|
+
if (typeof window === "undefined") return;
|
|
448
|
+
if (!this._dispatchWindowEventsEnabled) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const finalEventName = (this._dispatchPrefix ? `${this._dispatchPrefix}.${eventName as string}` : eventName) as string;
|
|
453
|
+
window.dispatchEvent(new CustomEvent(finalEventName, { detail: data }));
|
|
454
|
+
}
|
|
455
|
+
|
|
437
456
|
/**
|
|
438
457
|
* Call an event listener by name. The `rpc()` function differs from `emit()` in
|
|
439
458
|
* that it will only call and return data from the first event listener for the
|
|
@@ -464,24 +483,17 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
464
483
|
*
|
|
465
484
|
* // Emit the event named "hello"
|
|
466
485
|
* const result = myEntity.emit('hello', 'data1', 'data2');
|
|
467
|
-
*
|
|
486
|
+
* console.log("Result:", result);
|
|
468
487
|
*
|
|
469
488
|
* // The console output is:
|
|
470
|
-
* //
|
|
489
|
+
* // Arguments: data1 data2
|
|
471
490
|
* // Result: "foo"
|
|
472
491
|
*/
|
|
473
|
-
rpc<EventName extends keyof EventsInterface>(
|
|
474
|
-
eventName: EventName,
|
|
475
|
-
...data: Parameters<EventsInterface[EventName]>
|
|
476
|
-
): ReturnType<EventsInterface[EventName]> | ListenerReturnFlag {
|
|
492
|
+
rpc<EventName extends keyof EventsInterface> (eventName: EventName, ...data: Parameters<EventsInterface[EventName]>): ReturnType<EventsInterface[EventName]> | ListenerReturnFlag {
|
|
477
493
|
return this.rpcId<EventName>(eventName, "*", ...data);
|
|
478
494
|
}
|
|
479
495
|
|
|
480
|
-
rpcId<EventName extends keyof EventsInterface>(
|
|
481
|
-
eventName: EventName,
|
|
482
|
-
id: string,
|
|
483
|
-
...data: Parameters<EventsInterface[EventName]>
|
|
484
|
-
): ReturnType<EventsInterface[EventName]> | ListenerReturnFlag {
|
|
496
|
+
rpcId<EventName extends keyof EventsInterface> (eventName: EventName, id: string, ...data: Parameters<EventsInterface[EventName]>): ReturnType<EventsInterface[EventName]> | ListenerReturnFlag {
|
|
485
497
|
if (!this._eventListeners || !this._eventListeners[eventName]) {
|
|
486
498
|
throw new Error("Cannot make an rpc call without at least one event listener and none were found");
|
|
487
499
|
}
|
|
@@ -494,26 +506,18 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
494
506
|
const arr = this._eventListeners[eventName]![id];
|
|
495
507
|
emitterResult = this._emitToSingleListener<EventName>(arr[0], data);
|
|
496
508
|
} else if (id !== "*" && this._eventListeners[eventName]!["*"]) {
|
|
497
|
-
// Handle
|
|
509
|
+
// Handle wildcard emit if the id passed wasn't already a wildcard (*) id
|
|
498
510
|
const arr = this._eventListeners[eventName]!["*"];
|
|
499
511
|
emitterResult = this._emitToSingleListener<EventName>(arr[0], data);
|
|
500
512
|
} else {
|
|
501
513
|
if (id === "*") {
|
|
502
514
|
throw new Error(
|
|
503
|
-
`Cannot make an rpc call to event "${
|
|
504
|
-
eventName as string
|
|
505
|
-
}" without at least one event listener and none were found, register a listener via on("${
|
|
506
|
-
eventName as string
|
|
507
|
-
}", () => { ...some listener code })`
|
|
515
|
+
`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 })`
|
|
508
516
|
);
|
|
509
517
|
}
|
|
510
518
|
|
|
511
519
|
throw new Error(
|
|
512
|
-
`Cannot make an rpc call to event "${
|
|
513
|
-
eventName as string
|
|
514
|
-
}" and id "${id}" without at least one event listener and none were found, register a listener via on("${
|
|
515
|
-
eventName as string
|
|
516
|
-
}", "${id}", () => { ...some listener code })`
|
|
520
|
+
`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 })`
|
|
517
521
|
);
|
|
518
522
|
}
|
|
519
523
|
|
|
@@ -529,7 +533,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
529
533
|
* @param {...any} data The arguments to send to any listening methods.
|
|
530
534
|
* If you are sending multiple arguments, separate them with a comma so
|
|
531
535
|
* that they are received by the function as separate arguments.
|
|
532
|
-
* @return {
|
|
536
|
+
* @return {any[]} An array of all return values from all the listeners for the event.
|
|
533
537
|
* @example #Emit an Event
|
|
534
538
|
* // Emit the event named "hello"
|
|
535
539
|
* myEntity.emit('hello');
|
|
@@ -542,7 +546,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
542
546
|
* @example #Listen for Event Data
|
|
543
547
|
* // Set a listener to listen for the data (multiple values emitted
|
|
544
548
|
* // from an event are passed as function arguments)
|
|
545
|
-
*
|
|
549
|
+
* myEntity.on('hello', function (arg1, arg2) {
|
|
546
550
|
* console.log("Arguments:", arg1, arg2);
|
|
547
551
|
* return "foo";
|
|
548
552
|
* }
|
|
@@ -551,41 +555,51 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
551
555
|
* // The result is returned as an array of all return values
|
|
552
556
|
* // from all the listeners for the event
|
|
553
557
|
* const result = myEntity.emit('hello', 'data1', 'data2');
|
|
554
|
-
*
|
|
558
|
+
* console.log("Result:", result);
|
|
555
559
|
*
|
|
556
560
|
* // The console output is:
|
|
557
|
-
* //
|
|
561
|
+
* // Arguments: data1 data2
|
|
558
562
|
* // Result: ["foo"]
|
|
559
563
|
*/
|
|
560
|
-
emit<EventName extends keyof EventsInterface>(
|
|
561
|
-
eventName
|
|
562
|
-
...data: Parameters<EventsInterface[EventName]>
|
|
563
|
-
): (ReturnType<EventsInterface[EventName]> | ListenerReturnFlag)[] {
|
|
564
|
+
emit<EventName extends keyof EventsInterface> (eventName: EventName, ...data: Parameters<EventsInterface[EventName]>): (ReturnType<EventsInterface[EventName]> | ListenerReturnFlag)[] {
|
|
565
|
+
this._emitToWindowDispatch(eventName, data);
|
|
564
566
|
return this.emitId<EventName>(eventName, "*", ...data);
|
|
565
567
|
}
|
|
566
568
|
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
569
|
+
/**
|
|
570
|
+
* Emits an event by name and id.
|
|
571
|
+
* @param eventName The name of the event to emit.
|
|
572
|
+
* @param id The id of the event to emit.
|
|
573
|
+
* @param {...any} data The arguments to send to any listening methods.
|
|
574
|
+
* If you are sending multiple arguments, separate them with a comma so
|
|
575
|
+
* that they are received by the function as separate arguments.
|
|
576
|
+
* @return {any[]} An array of all return values from all the listeners for the event.
|
|
577
|
+
*/
|
|
578
|
+
emitId<EventName extends keyof EventsInterface> (eventName: EventName, id: string, ...data: Parameters<EventsInterface[EventName]>): (ReturnType<EventsInterface[EventName]> | ListenerReturnFlag)[] {
|
|
579
|
+
if (!this._eventListeners || (!this._eventListeners[eventName] && !this._eventListeners["*"])) {
|
|
573
580
|
return [];
|
|
574
581
|
}
|
|
575
582
|
|
|
576
583
|
let emitterResult: (ReturnType<EventsInterface[EventName]> | ListenerReturnFlag)[] = [];
|
|
577
584
|
this._eventsEmitting = true;
|
|
578
585
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
586
|
+
if (this._eventListeners[eventName]) {
|
|
587
|
+
// Handle id emit
|
|
588
|
+
if (this._eventListeners[eventName]![id]) {
|
|
589
|
+
const arr = this._eventListeners[eventName]![id];
|
|
590
|
+
emitterResult = emitterResult.concat(this._emitToArrayOfListeners<EventName>(arr, data));
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// Handle id wildcard emit if the id passed wasn't already a wildcard (*) id
|
|
594
|
+
if (id !== "*" && this._eventListeners[eventName]!["*"]) {
|
|
595
|
+
const arr = this._eventListeners[eventName]!["*"];
|
|
596
|
+
emitterResult = emitterResult.concat(this._emitToArrayOfListeners<EventName>(arr, data));
|
|
597
|
+
}
|
|
583
598
|
}
|
|
584
599
|
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
emitterResult = emitterResult.concat(this._emitToArrayOfListeners<EventName>(arr, data));
|
|
600
|
+
if (this._eventListeners["*"]) {
|
|
601
|
+
const arr = this._eventListeners["*"]!["*"];
|
|
602
|
+
emitterResult = emitterResult.concat(this._emitToArrayOfListeners<EventName>(arr as EventListenerCallback<EventsInterface[EventName]>[], [eventName, ...data] as Parameters<EventsInterface[EventName]>));
|
|
589
603
|
}
|
|
590
604
|
|
|
591
605
|
this._eventsEmitting = false;
|
|
@@ -595,24 +609,23 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
595
609
|
}
|
|
596
610
|
|
|
597
611
|
/**
|
|
598
|
-
* Creates a persistent
|
|
599
|
-
*
|
|
600
|
-
*
|
|
612
|
+
* Creates a persistent event. Once emitStatic() has been called, a
|
|
613
|
+
* persistent event is registered. Any existing event listeners will
|
|
614
|
+
* be called just like when you call emit(), however, any new
|
|
615
|
+
* listeners that are registered for this event via on() will also be
|
|
616
|
+
* called as soon as they are added.
|
|
601
617
|
* @param eventName The name of the event to emit.
|
|
602
618
|
* @param {...any} data Optional arguments to emit with the event.
|
|
603
619
|
* @returns The emitter instance.
|
|
604
620
|
* @private
|
|
605
621
|
*/
|
|
606
|
-
emitStatic<EventName extends keyof EventsInterface>(
|
|
607
|
-
eventName: EventName,
|
|
608
|
-
...data: Parameters<EventsInterface[EventName]>
|
|
609
|
-
) {
|
|
622
|
+
emitStatic<EventName extends keyof EventsInterface> (eventName: EventName, ...data: Parameters<EventsInterface[EventName]>) {
|
|
610
623
|
const id = "*";
|
|
611
624
|
this._eventListeners = this._eventListeners || {};
|
|
612
625
|
this._eventsEmitting = true;
|
|
613
626
|
|
|
614
627
|
if (this._eventListeners[eventName] && this._eventListeners[eventName]![id]) {
|
|
615
|
-
// Handle
|
|
628
|
+
// Handle id wildcard emit
|
|
616
629
|
const arr = this._eventListeners[eventName]![id];
|
|
617
630
|
const arrCount = arr.length;
|
|
618
631
|
|
|
@@ -650,11 +663,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
650
663
|
* @returns The emitter instance.
|
|
651
664
|
* @private
|
|
652
665
|
*/
|
|
653
|
-
emitStaticId<EventName extends keyof EventsInterface>(
|
|
654
|
-
eventName: EventName,
|
|
655
|
-
id: string,
|
|
656
|
-
...data: Parameters<EventsInterface[EventName]>
|
|
657
|
-
) {
|
|
666
|
+
emitStaticId<EventName extends keyof EventsInterface> (eventName: EventName, id: string, ...data: Parameters<EventsInterface[EventName]>) {
|
|
658
667
|
if (!id) throw new Error("Missing id from emitId call!");
|
|
659
668
|
|
|
660
669
|
this._eventListeners = this._eventListeners || {};
|
|
@@ -676,7 +685,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
676
685
|
}
|
|
677
686
|
}
|
|
678
687
|
|
|
679
|
-
// Handle
|
|
688
|
+
// Handle wildcard emit
|
|
680
689
|
if (this._eventListeners[eventName]!["*"]) {
|
|
681
690
|
const arr = this._eventListeners[eventName]!["*"];
|
|
682
691
|
const arrCount = arr.length;
|
|
@@ -712,7 +721,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
712
721
|
* @returns The emitter instance.
|
|
713
722
|
* @private
|
|
714
723
|
*/
|
|
715
|
-
cancelStatic<EventName extends keyof EventsInterface>(eventName: EventName) {
|
|
724
|
+
cancelStatic<EventName extends keyof EventsInterface> (eventName: EventName) {
|
|
716
725
|
this._eventStaticEmitters = this._eventStaticEmitters || {};
|
|
717
726
|
this._eventStaticEmitters[eventName] = [];
|
|
718
727
|
|
|
@@ -725,7 +734,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
725
734
|
* @returns True if one or more event listeners are registered for
|
|
726
735
|
* the event. False if none are found.
|
|
727
736
|
*/
|
|
728
|
-
willEmit<EventName extends keyof EventsInterface>(eventName: EventName) {
|
|
737
|
+
willEmit<EventName extends keyof EventsInterface> (eventName: EventName) {
|
|
729
738
|
const id = "*";
|
|
730
739
|
|
|
731
740
|
if (!this._eventListeners || !this._eventListeners[eventName]) {
|
|
@@ -754,7 +763,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
754
763
|
* @returns True if one or more event listeners are registered for
|
|
755
764
|
* the event. False if none are found.
|
|
756
765
|
*/
|
|
757
|
-
willEmitId<EventName extends keyof EventsInterface>(eventName: EventName, id: string) {
|
|
766
|
+
willEmitId<EventName extends keyof EventsInterface> (eventName: EventName, id: string) {
|
|
758
767
|
if (!this._eventListeners || !this._eventListeners[eventName]) {
|
|
759
768
|
return false;
|
|
760
769
|
}
|
|
@@ -774,7 +783,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
774
783
|
}
|
|
775
784
|
}
|
|
776
785
|
|
|
777
|
-
// Handle
|
|
786
|
+
// Handle wildcard emit
|
|
778
787
|
if (this._eventListeners[eventName]!["*"]) {
|
|
779
788
|
const arr = this._eventListeners[eventName]!["*"];
|
|
780
789
|
const arrCount = arr.length;
|
|
@@ -802,10 +811,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
802
811
|
* @param {...any} data Optional arguments to emit with the event.
|
|
803
812
|
* @returns The emitter instance.
|
|
804
813
|
*/
|
|
805
|
-
deferEmit<EventName extends keyof EventsInterface>(
|
|
806
|
-
eventName: EventName,
|
|
807
|
-
...data: Parameters<EventsInterface[EventName]>
|
|
808
|
-
) {
|
|
814
|
+
deferEmit<EventName extends keyof EventsInterface> (eventName: EventName, ...data: Parameters<EventsInterface[EventName]>) {
|
|
809
815
|
if (!this._eventsAllowDefer) {
|
|
810
816
|
// Check for an existing timeout
|
|
811
817
|
this._eventsDeferTimeouts = this._eventsDeferTimeouts || {};
|
|
@@ -830,7 +836,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
830
836
|
* argument contains a cancellation flag.
|
|
831
837
|
* @param resultArr
|
|
832
838
|
*/
|
|
833
|
-
didCancel(resultArr: (unknown | ListenerReturnFlag)[]) {
|
|
839
|
+
didCancel (resultArr: (unknown | ListenerReturnFlag)[]) {
|
|
834
840
|
return resultArr.findIndex((result) => result === ListenerReturnFlag.cancel) > -1;
|
|
835
841
|
}
|
|
836
842
|
|
|
@@ -843,7 +849,7 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
843
849
|
* the event emitter is finished processing.
|
|
844
850
|
* @private
|
|
845
851
|
*/
|
|
846
|
-
_processRemovalQueue() {
|
|
852
|
+
_processRemovalQueue () {
|
|
847
853
|
if (!this._eventRemovalQueue || !this._eventRemovalQueue.length) {
|
|
848
854
|
return;
|
|
849
855
|
}
|
|
@@ -858,10 +864,10 @@ export class Emitter<EventsInterface extends EmitterEventsInterface<EventsInterf
|
|
|
858
864
|
}
|
|
859
865
|
}
|
|
860
866
|
|
|
861
|
-
export function makeEmitter(obj:
|
|
862
|
-
export function makeEmitter(obj: boolean): Emitter
|
|
863
|
-
export function makeEmitter
|
|
864
|
-
export function makeEmitter(obj: any, prototypeMode?: any) {
|
|
867
|
+
export function makeEmitter<EventsInterface extends EmitterEventsWithStar<EventsInterface> = EmitterAnyEventsInterface> (obj: boolean): Emitter<EventsInterface>;
|
|
868
|
+
export function makeEmitter<EventsInterface extends EmitterEventsWithStar<EventsInterface> = EmitterAnyEventsInterface> (obj: Record<string, unknown>, prototypeMode: boolean): Emitter<EventsInterface>;
|
|
869
|
+
export function makeEmitter<EventsInterface extends EmitterEventsWithStar<EventsInterface> = EmitterAnyEventsInterface> (obj: AnyConstructor): Emitter<EventsInterface>;
|
|
870
|
+
export function makeEmitter<EventsInterface extends EmitterEventsWithStar<EventsInterface> = EmitterAnyEventsInterface> (obj: any, prototypeMode?: any): Emitter<EventsInterface> {
|
|
865
871
|
let operateOnObject;
|
|
866
872
|
|
|
867
873
|
if (obj === undefined && prototypeMode === undefined) {
|
|
@@ -884,9 +890,7 @@ export function makeEmitter(obj: any, prototypeMode?: any) {
|
|
|
884
890
|
|
|
885
891
|
if (prototypeMode) {
|
|
886
892
|
if (obj.prototype === undefined) {
|
|
887
|
-
throw new Error(
|
|
888
|
-
"Cannot modify prototype of passed object, it has no prototype property! Was it instantiated with the new operator correctly?"
|
|
889
|
-
);
|
|
893
|
+
throw new Error("Cannot modify prototype of passed object, it has no prototype property! Was it instantiated with the new operator correctly?");
|
|
890
894
|
}
|
|
891
895
|
|
|
892
896
|
operateOnObject = obj.prototype;
|
|
@@ -896,7 +900,9 @@ export function makeEmitter(obj: any, prototypeMode?: any) {
|
|
|
896
900
|
|
|
897
901
|
// Convert the object prototype to have eventing capability
|
|
898
902
|
operateOnObject.on = Emitter.prototype.on;
|
|
903
|
+
operateOnObject.onAny = Emitter.prototype.onAny;
|
|
899
904
|
operateOnObject.off = Emitter.prototype.off;
|
|
905
|
+
operateOnObject.offAny = Emitter.prototype.offAny;
|
|
900
906
|
operateOnObject.once = Emitter.prototype.once;
|
|
901
907
|
operateOnObject.emit = Emitter.prototype.emit;
|
|
902
908
|
operateOnObject.emitId = Emitter.prototype.emitId;
|
|
@@ -909,4 +915,4 @@ export function makeEmitter(obj: any, prototypeMode?: any) {
|
|
|
909
915
|
operateOnObject._processRemovalQueue = Emitter.prototype._processRemovalQueue;
|
|
910
916
|
|
|
911
917
|
return obj;
|
|
912
|
-
}
|
|
918
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|