@irrelon/emitter 5.0.2 → 5.0.4
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/.eslintrc.json +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/src/Emitter.d.ts +202 -0
- package/{src → dist/cjs/src}/Emitter.js +50 -129
- package/dist/cjs/tests/Emitter.test.js +357 -0
- package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/src/Emitter.d.ts +202 -0
- package/dist/mjs/src/Emitter.js +483 -0
- package/dist/mjs/tests/Emitter.test.d.ts +1 -0
- package/dist/mjs/tests/Emitter.test.js +336 -0
- package/dist/mjs/tsconfig.tsbuildinfo +1 -0
- package/fixup.sh +11 -0
- package/license.md +11 -0
- package/package.json +10 -4
- package/readme.md +19 -19
- package/src/Emitter.ts +133 -113
- package/tests/Emitter.test.ts +132 -32
- package/tsconfig-base.json +36 -0
- package/tsconfig-cjs.json +8 -0
- package/tsconfig.json +5 -47
- package/src/Emitter.d.ts +0 -190
- package/tests/Emitter.test.js +0 -277
- /package/{tests → dist/cjs/tests}/Emitter.test.d.ts +0 -0
package/src/Emitter.ts
CHANGED
|
@@ -25,6 +25,13 @@
|
|
|
25
25
|
Source: https://github.com/irrelon/emitter
|
|
26
26
|
|
|
27
27
|
Changelog:
|
|
28
|
+
Version 5.0.3 and 5.0.4: Updated to allow specifying an events type
|
|
29
|
+
to the Emitter to allow complete type safety when registering
|
|
30
|
+
listeners and emitting events.
|
|
31
|
+
|
|
32
|
+
Updated the build and package to split between delivery of CJS
|
|
33
|
+
and MJS module types so that this package can be eiter imported
|
|
34
|
+
or required without having to jump through any hoops.
|
|
28
35
|
Version 5.0.2:
|
|
29
36
|
Removed some unnecessary code, updated to be in step with version
|
|
30
37
|
that is being used in the new version of Isogenic Game Engine.
|
|
@@ -84,54 +91,66 @@
|
|
|
84
91
|
Version 1.0.0:
|
|
85
92
|
First commit
|
|
86
93
|
*/
|
|
94
|
+
export type AnyFunction<ReturnType = any> = (...args: any[]) => ReturnType;
|
|
95
|
+
export type AnyConstructor = new (...args: any[]) => any;
|
|
96
|
+
|
|
87
97
|
export enum EventReturnFlag {
|
|
88
98
|
none,
|
|
89
99
|
cancel
|
|
90
100
|
}
|
|
91
101
|
|
|
92
|
-
export type EventListenerCallback = (...args:
|
|
93
|
-
|
|
102
|
+
export type EventListenerCallback<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> = (...args: Parameters<EventCallbackFunctionSignature>) => ReturnType<EventCallbackFunctionSignature>;
|
|
103
|
+
|
|
104
|
+
export interface EventStaticEmitterObject<EventCallbackFunctionSignature extends AnyFunction = AnyFunction> {
|
|
94
105
|
id: string;
|
|
95
|
-
args:
|
|
106
|
+
args: Parameters<EventCallbackFunctionSignature>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface EmitterEventsInterface {
|
|
110
|
+
[key: string]: AnyFunction
|
|
96
111
|
}
|
|
97
112
|
|
|
113
|
+
export type ConvertInterfaceToDict<T> = {
|
|
114
|
+
[K in keyof T]: T[K];
|
|
115
|
+
};
|
|
116
|
+
|
|
98
117
|
/**
|
|
99
118
|
* Creates a new class with the capability to emit events.
|
|
100
119
|
*/
|
|
101
|
-
export class Emitter {
|
|
120
|
+
export class Emitter<EventsInterface extends EmitterEventsInterface = EmitterEventsInterface> {
|
|
102
121
|
_eventsEmitting: boolean = false;
|
|
103
122
|
_eventRemovalQueue: any[] = [];
|
|
104
|
-
_eventListeners?:
|
|
105
|
-
_eventStaticEmitters:
|
|
123
|
+
_eventListeners?: { [EventName in (keyof EventsInterface)]?: Record<string, EventListenerCallback<EventsInterface[EventName]>[]> } = {};
|
|
124
|
+
_eventStaticEmitters: {[EventName in (keyof EventsInterface)]?: EventStaticEmitterObject<EventsInterface[EventName]>[] } = {};
|
|
106
125
|
_eventsAllowDefer: boolean = false;
|
|
107
126
|
_eventsDeferTimeouts: Record<any, number> = {};
|
|
108
127
|
|
|
109
128
|
/**
|
|
110
129
|
* Attach an event listener to the passed event only if the passed
|
|
111
130
|
* id matches the id for the event being fired.
|
|
112
|
-
* @param
|
|
113
|
-
* @param
|
|
114
|
-
* @param
|
|
115
|
-
* @returns
|
|
131
|
+
* @param eventName The name of the event to listen for.
|
|
132
|
+
* @param id The id to match against.
|
|
133
|
+
* @param listener The method to call when the event is fired.
|
|
134
|
+
* @returns The emitter instance.
|
|
116
135
|
*/
|
|
117
|
-
_on (eventName:
|
|
118
|
-
const generateTimeout = (emitter: EventStaticEmitterObject) => {
|
|
136
|
+
_on<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>) {
|
|
137
|
+
const generateTimeout = (emitter: EventStaticEmitterObject<EventsInterface[EventName]>) => {
|
|
119
138
|
setTimeout(() => {
|
|
120
139
|
listener(...emitter.args);
|
|
121
140
|
}, 1);
|
|
122
141
|
};
|
|
123
142
|
|
|
124
143
|
this._eventListeners = this._eventListeners || {};
|
|
125
|
-
this._eventListeners[eventName] = this._eventListeners[eventName] || {};
|
|
126
|
-
|
|
127
|
-
|
|
144
|
+
const nonNullableEventListeners = this._eventListeners[eventName] = this._eventListeners[eventName] || {};
|
|
145
|
+
nonNullableEventListeners[id] = nonNullableEventListeners[id] || [];
|
|
146
|
+
nonNullableEventListeners[id].push(listener);
|
|
128
147
|
|
|
129
148
|
// Check for any static emitters, and fire the event if any exist
|
|
130
|
-
if (!this._eventStaticEmitters || !this._eventStaticEmitters[eventName] || !this._eventStaticEmitters[eventName]
|
|
149
|
+
if (!this._eventStaticEmitters || !this._eventStaticEmitters[eventName] || !this._eventStaticEmitters[eventName]!.length) return this;
|
|
131
150
|
|
|
132
151
|
// Emit events for each emitter
|
|
133
|
-
for (let i = 0; i < this._eventStaticEmitters[eventName]
|
|
134
|
-
const emitter = this._eventStaticEmitters[eventName][i];
|
|
152
|
+
for (let i = 0; i < this._eventStaticEmitters[eventName]!.length; i++) {
|
|
153
|
+
const emitter = this._eventStaticEmitters[eventName]![i];
|
|
135
154
|
|
|
136
155
|
if (id === "*" || emitter.id === id) {
|
|
137
156
|
// Call the listener out of process so that any code that expects a listener
|
|
@@ -147,20 +166,23 @@ export class Emitter {
|
|
|
147
166
|
/**
|
|
148
167
|
* Attach an event listener to the passed event only if the passed
|
|
149
168
|
* id matches the document id for the event being fired.
|
|
150
|
-
* @param
|
|
151
|
-
* @param
|
|
152
|
-
* @param
|
|
153
|
-
* @returns
|
|
169
|
+
* @param eventName The name of the event to listen for.
|
|
170
|
+
* @param id The id to match against.
|
|
171
|
+
* @param listener The method to call when the event is fired.
|
|
172
|
+
* @returns The emitter instance.
|
|
154
173
|
*/
|
|
155
|
-
_once (eventName:
|
|
174
|
+
_once<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>) {
|
|
156
175
|
let fired = false;
|
|
176
|
+
let originalReturnData: ReturnType<EventsInterface[EventName]>;
|
|
157
177
|
|
|
158
|
-
const internalCallback = (...args:
|
|
159
|
-
if (fired) return;
|
|
178
|
+
const internalCallback: EventListenerCallback<EventsInterface[EventName]> = (...args: Parameters<EventsInterface[EventName]>): ReturnType<EventsInterface[EventName]> => {
|
|
179
|
+
if (fired) return originalReturnData;
|
|
160
180
|
|
|
161
181
|
fired = true;
|
|
162
182
|
this.off(eventName, id, internalCallback);
|
|
163
|
-
|
|
183
|
+
|
|
184
|
+
originalReturnData = listener(...args);
|
|
185
|
+
return originalReturnData;
|
|
164
186
|
};
|
|
165
187
|
|
|
166
188
|
return this.on(eventName, id, internalCallback);
|
|
@@ -168,15 +190,15 @@ export class Emitter {
|
|
|
168
190
|
|
|
169
191
|
/**
|
|
170
192
|
* Cancels an event listener based on an event name, id and listener function.
|
|
171
|
-
* @param
|
|
172
|
-
* @param
|
|
173
|
-
* @param
|
|
193
|
+
* @param eventName The event to cancel listener for.
|
|
194
|
+
* @param id The ID of the event to cancel listening for.
|
|
195
|
+
* @param listener The event listener function used in the on()
|
|
174
196
|
* or once() call to cancel.
|
|
175
|
-
* @returns
|
|
197
|
+
* @returns The emitter instance.
|
|
176
198
|
*/
|
|
177
|
-
_off (eventName:
|
|
199
|
+
_off<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener?: EventListenerCallback<EventsInterface[EventName]>): this {
|
|
178
200
|
// If the event name doesn't have any listeners, exit early
|
|
179
|
-
if (!this._eventListeners || !this._eventListeners[eventName] || !this._eventListeners[eventName][id]) return this;
|
|
201
|
+
if (!this._eventListeners || !this._eventListeners[eventName] || !this._eventListeners[eventName]![id]) return this;
|
|
180
202
|
|
|
181
203
|
// If we are emitting events at the moment, don't remove this listener
|
|
182
204
|
// until the process has completed, so we queue for removal instead
|
|
@@ -200,11 +222,11 @@ export class Emitter {
|
|
|
200
222
|
}
|
|
201
223
|
|
|
202
224
|
// No listener provided, delete all listeners for this id
|
|
203
|
-
delete this._eventListeners[eventName][id];
|
|
225
|
+
delete this._eventListeners[eventName]![id];
|
|
204
226
|
return this;
|
|
205
227
|
}
|
|
206
228
|
|
|
207
|
-
const arr = this._eventListeners[eventName][id] || [];
|
|
229
|
+
const arr = this._eventListeners[eventName]![id] || [];
|
|
208
230
|
const index = arr.indexOf(listener);
|
|
209
231
|
|
|
210
232
|
if (index > -1) {
|
|
@@ -217,34 +239,32 @@ export class Emitter {
|
|
|
217
239
|
/**
|
|
218
240
|
* Attach an event listener to the passed event only if the passed
|
|
219
241
|
* id matches the document id for the event being fired.
|
|
220
|
-
* @param
|
|
221
|
-
* @param
|
|
222
|
-
* @param
|
|
223
|
-
* @returns
|
|
242
|
+
* @param eventName The name of the event to listen for.
|
|
243
|
+
* @param id The id to match against.
|
|
244
|
+
* @param listener The method to call when the event is fired.
|
|
245
|
+
* @returns The emitter instance.
|
|
224
246
|
*/
|
|
225
|
-
on (eventName:
|
|
226
|
-
on (eventName:
|
|
227
|
-
on (eventName:
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
if (restTypes[0] === "function") {
|
|
231
|
-
return this._on(eventName, "*", rest[0]);
|
|
247
|
+
on<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
248
|
+
on<EventName extends keyof EventsInterface> (eventName: EventName, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
249
|
+
on<EventName extends keyof EventsInterface> (eventName: EventName, idOrListener: string | EventListenerCallback<EventsInterface[EventName]>, listener?: EventListenerCallback<EventsInterface[EventName]>): this {
|
|
250
|
+
if (typeof idOrListener === "string") {
|
|
251
|
+
return this._on(eventName, idOrListener, listener as EventListenerCallback<EventsInterface[EventName]>);
|
|
232
252
|
}
|
|
233
253
|
|
|
234
|
-
return this._on(eventName,
|
|
254
|
+
return this._on(eventName, "*", idOrListener);
|
|
235
255
|
}
|
|
236
256
|
|
|
237
257
|
/**
|
|
238
258
|
* Attach an event listener to the passed event only if the passed
|
|
239
259
|
* id matches the document id for the event being fired.
|
|
240
|
-
* @param
|
|
260
|
+
* @param eventName The name of the event to listen for.
|
|
241
261
|
* @param id The id to match against.
|
|
242
262
|
* @param listener
|
|
243
|
-
* @returns
|
|
263
|
+
* @returns The emitter instance.
|
|
244
264
|
*/
|
|
245
|
-
once (eventName:
|
|
246
|
-
once (eventName:
|
|
247
|
-
once (eventName:
|
|
265
|
+
once<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
266
|
+
once<EventName extends keyof EventsInterface> (eventName: EventName, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
267
|
+
once<EventName extends keyof EventsInterface> (eventName: EventName, ...rest: any[]): this {
|
|
248
268
|
const restTypes = rest.map((arg) => typeof arg);
|
|
249
269
|
|
|
250
270
|
if (restTypes[0] === "function") {
|
|
@@ -261,14 +281,14 @@ export class Emitter {
|
|
|
261
281
|
* to calling `.off(eventName)` then `on(eventName, listener)` since
|
|
262
282
|
* `off(eventName)` will cancel any previous event listeners for the passed
|
|
263
283
|
* event name.
|
|
264
|
-
* @param
|
|
265
|
-
* @param
|
|
266
|
-
* @param
|
|
267
|
-
* @returns
|
|
284
|
+
* @param eventName The name of the event to listen for.
|
|
285
|
+
* @param id The id to match against.
|
|
286
|
+
* @param listener The method to call when the event is fired.
|
|
287
|
+
* @returns The emitter instance.
|
|
268
288
|
*/
|
|
269
|
-
overwrite (eventName:
|
|
270
|
-
overwrite (eventName:
|
|
271
|
-
overwrite (eventName:
|
|
289
|
+
overwrite<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
290
|
+
overwrite<EventName extends keyof EventsInterface> (eventName: EventName, listener: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
291
|
+
overwrite<EventName extends keyof EventsInterface> (eventName: EventName, ...rest: any[]) {
|
|
272
292
|
const restTypes = rest.map((arg) => typeof arg);
|
|
273
293
|
|
|
274
294
|
if (restTypes[0] === "function") {
|
|
@@ -282,16 +302,16 @@ export class Emitter {
|
|
|
282
302
|
|
|
283
303
|
/**
|
|
284
304
|
* Cancels an event listener based on an event name, id and listener function.
|
|
285
|
-
* @param
|
|
286
|
-
* @param
|
|
287
|
-
* @param
|
|
305
|
+
* @param eventName The event to cancel listener for.
|
|
306
|
+
* @param id The ID of the event to cancel listening for.
|
|
307
|
+
* @param listener The event listener function used in the on()
|
|
288
308
|
* or once() call to cancel.
|
|
289
|
-
* @returns
|
|
309
|
+
* @returns The emitter instance.
|
|
290
310
|
*/
|
|
291
|
-
off (eventName:
|
|
292
|
-
off (eventName:
|
|
293
|
-
off (eventName:
|
|
294
|
-
off (eventName:
|
|
311
|
+
off<EventName extends keyof EventsInterface> (eventName: EventName, id: string, listener?: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
312
|
+
off<EventName extends keyof EventsInterface> (eventName: EventName, listener?: EventListenerCallback<EventsInterface[EventName]>): this;
|
|
313
|
+
off<EventName extends keyof EventsInterface> (eventName: EventName): this;
|
|
314
|
+
off<EventName extends keyof EventsInterface> (eventName: EventName, ...rest: any[]) {
|
|
295
315
|
if (rest.length === 0) {
|
|
296
316
|
// Only event was provided, use * as the id to mean "any without"
|
|
297
317
|
// a specific id
|
|
@@ -312,7 +332,7 @@ export class Emitter {
|
|
|
312
332
|
|
|
313
333
|
/**
|
|
314
334
|
* Emit an event by name.
|
|
315
|
-
* @param
|
|
335
|
+
* @param eventName The name of the event to emit.
|
|
316
336
|
* @param {...any} data The arguments to send to any listening methods.
|
|
317
337
|
* If you are sending multiple arguments, separate them with a comma so
|
|
318
338
|
* that they are received by the function as separate arguments.
|
|
@@ -339,7 +359,7 @@ export class Emitter {
|
|
|
339
359
|
* // The console output is:
|
|
340
360
|
* // data1, data2
|
|
341
361
|
*/
|
|
342
|
-
async emit (eventName:
|
|
362
|
+
async emit<EventName extends keyof EventsInterface> (eventName: EventName, ...data: Parameters<EventsInterface[EventName]>): Promise<EventReturnFlag> {
|
|
343
363
|
if (!this._eventListeners) {
|
|
344
364
|
return EventReturnFlag.none;
|
|
345
365
|
}
|
|
@@ -349,9 +369,9 @@ export class Emitter {
|
|
|
349
369
|
let returnFlag: EventReturnFlag = EventReturnFlag.none;
|
|
350
370
|
this._eventsEmitting = true;
|
|
351
371
|
|
|
352
|
-
if (this._eventListeners[eventName] && this._eventListeners[eventName][id]) {
|
|
372
|
+
if (this._eventListeners[eventName] && this._eventListeners[eventName]![id]) {
|
|
353
373
|
// Handle global emit
|
|
354
|
-
const arr = this._eventListeners[eventName][id];
|
|
374
|
+
const arr = this._eventListeners[eventName]![id];
|
|
355
375
|
|
|
356
376
|
const promiseArr = arr.map((tmpFunc) => {
|
|
357
377
|
if (typeof tmpFunc !== "function") return;
|
|
@@ -371,7 +391,7 @@ export class Emitter {
|
|
|
371
391
|
return returnFlag;
|
|
372
392
|
}
|
|
373
393
|
|
|
374
|
-
emitId (eventName:
|
|
394
|
+
emitId<EventName extends keyof EventsInterface> (eventName: EventName, id: string, ...data: Parameters<EventsInterface[EventName]>): EventReturnFlag {
|
|
375
395
|
if (!this._eventListeners) {
|
|
376
396
|
return EventReturnFlag.none;
|
|
377
397
|
}
|
|
@@ -387,8 +407,8 @@ export class Emitter {
|
|
|
387
407
|
}
|
|
388
408
|
|
|
389
409
|
// Handle id emit
|
|
390
|
-
if (this._eventListeners[eventName][id]) {
|
|
391
|
-
const arr = this._eventListeners[eventName][id];
|
|
410
|
+
if (this._eventListeners[eventName]![id]) {
|
|
411
|
+
const arr = this._eventListeners[eventName]![id];
|
|
392
412
|
const arrCount = arr.length;
|
|
393
413
|
|
|
394
414
|
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
@@ -402,8 +422,8 @@ export class Emitter {
|
|
|
402
422
|
}
|
|
403
423
|
|
|
404
424
|
// Handle global emit
|
|
405
|
-
if (this._eventListeners[eventName]["*"]) {
|
|
406
|
-
const arr = this._eventListeners[eventName]["*"];
|
|
425
|
+
if (this._eventListeners[eventName]!["*"]) {
|
|
426
|
+
const arr = this._eventListeners[eventName]!["*"];
|
|
407
427
|
const arrCount = arr.length;
|
|
408
428
|
|
|
409
429
|
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
@@ -426,19 +446,19 @@ export class Emitter {
|
|
|
426
446
|
* Creates a persistent emitter record that will fire a listener if
|
|
427
447
|
* one is added for this event after the emitStatic() call has been
|
|
428
448
|
* made.
|
|
429
|
-
* @param
|
|
449
|
+
* @param eventName The name of the event to emit.
|
|
430
450
|
* @param {...any} data Optional arguments to emit with the event.
|
|
431
|
-
* @returns
|
|
451
|
+
* @returns The emitter instance.
|
|
432
452
|
* @private
|
|
433
453
|
*/
|
|
434
|
-
emitStatic (eventName:
|
|
454
|
+
emitStatic<EventName extends keyof EventsInterface> (eventName: EventName, ...data: Parameters<EventsInterface[EventName]>) {
|
|
435
455
|
const id = "*";
|
|
436
456
|
this._eventListeners = this._eventListeners || {};
|
|
437
457
|
this._eventsEmitting = true;
|
|
438
458
|
|
|
439
|
-
if (this._eventListeners[eventName] && this._eventListeners[eventName][id]) {
|
|
459
|
+
if (this._eventListeners[eventName] && this._eventListeners[eventName]![id]) {
|
|
440
460
|
// Handle global emit
|
|
441
|
-
const arr = this._eventListeners[eventName][id];
|
|
461
|
+
const arr = this._eventListeners[eventName]![id];
|
|
442
462
|
const arrCount = arr.length;
|
|
443
463
|
|
|
444
464
|
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
@@ -455,7 +475,7 @@ export class Emitter {
|
|
|
455
475
|
|
|
456
476
|
this._eventStaticEmitters = this._eventStaticEmitters || {};
|
|
457
477
|
this._eventStaticEmitters[eventName] = this._eventStaticEmitters[eventName] || [];
|
|
458
|
-
this._eventStaticEmitters[eventName]
|
|
478
|
+
this._eventStaticEmitters[eventName]!.push({
|
|
459
479
|
"id": "*",
|
|
460
480
|
"args": data
|
|
461
481
|
});
|
|
@@ -469,13 +489,13 @@ export class Emitter {
|
|
|
469
489
|
* Creates a persistent emitter record that will fire a listener if
|
|
470
490
|
* one is added for this event after the emitStatic() call has been
|
|
471
491
|
* made.
|
|
472
|
-
* @param
|
|
473
|
-
* @param
|
|
492
|
+
* @param eventName The name of the event to emit.
|
|
493
|
+
* @param id The id of the event to emit.
|
|
474
494
|
* @param {...any} data Optional arguments to emit with the event.
|
|
475
|
-
* @returns
|
|
495
|
+
* @returns The emitter instance.
|
|
476
496
|
* @private
|
|
477
497
|
*/
|
|
478
|
-
emitStaticId (eventName:
|
|
498
|
+
emitStaticId<EventName extends keyof EventsInterface> (eventName: EventName, id: string, ...data: Parameters<EventsInterface[EventName]>) {
|
|
479
499
|
if (!id) throw new Error("Missing id from emitId call!");
|
|
480
500
|
|
|
481
501
|
this._eventListeners = this._eventListeners || {};
|
|
@@ -483,8 +503,8 @@ export class Emitter {
|
|
|
483
503
|
|
|
484
504
|
if (this._eventListeners[eventName]) {
|
|
485
505
|
// Handle id emit
|
|
486
|
-
if (this._eventListeners[eventName][id]) {
|
|
487
|
-
const arr = this._eventListeners[eventName][id];
|
|
506
|
+
if (this._eventListeners[eventName]![id]) {
|
|
507
|
+
const arr = this._eventListeners[eventName]![id];
|
|
488
508
|
const arrCount = arr.length;
|
|
489
509
|
|
|
490
510
|
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
@@ -498,8 +518,8 @@ export class Emitter {
|
|
|
498
518
|
}
|
|
499
519
|
|
|
500
520
|
// Handle global emit
|
|
501
|
-
if (this._eventListeners[eventName]["*"]) {
|
|
502
|
-
const arr = this._eventListeners[eventName]["*"];
|
|
521
|
+
if (this._eventListeners[eventName]!["*"]) {
|
|
522
|
+
const arr = this._eventListeners[eventName]!["*"];
|
|
503
523
|
const arrCount = arr.length;
|
|
504
524
|
|
|
505
525
|
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
@@ -517,7 +537,7 @@ export class Emitter {
|
|
|
517
537
|
|
|
518
538
|
this._eventStaticEmitters = this._eventStaticEmitters || {};
|
|
519
539
|
this._eventStaticEmitters[eventName] = this._eventStaticEmitters[eventName] || [];
|
|
520
|
-
this._eventStaticEmitters[eventName]
|
|
540
|
+
this._eventStaticEmitters[eventName]!.push({
|
|
521
541
|
id,
|
|
522
542
|
"args": data
|
|
523
543
|
});
|
|
@@ -529,11 +549,11 @@ export class Emitter {
|
|
|
529
549
|
|
|
530
550
|
/**
|
|
531
551
|
* Handles removing emitters, is an internal method not called directly.
|
|
532
|
-
* @param
|
|
533
|
-
* @returns
|
|
552
|
+
* @param eventName The event to remove static emitter for.
|
|
553
|
+
* @returns The emitter instance.
|
|
534
554
|
* @private
|
|
535
555
|
*/
|
|
536
|
-
cancelStatic (eventName:
|
|
556
|
+
cancelStatic<EventName extends keyof EventsInterface> (eventName: EventName) {
|
|
537
557
|
this._eventStaticEmitters = this._eventStaticEmitters || {};
|
|
538
558
|
this._eventStaticEmitters[eventName] = [];
|
|
539
559
|
|
|
@@ -542,18 +562,18 @@ export class Emitter {
|
|
|
542
562
|
|
|
543
563
|
/**
|
|
544
564
|
* Checks if an event has any event listeners or not.
|
|
545
|
-
* @param
|
|
546
|
-
* @returns
|
|
565
|
+
* @param eventName The name of the event to check for.
|
|
566
|
+
* @returns True if one or more event listeners are registered for
|
|
547
567
|
* the event. False if none are found.
|
|
548
568
|
*/
|
|
549
|
-
willEmit (eventName:
|
|
569
|
+
willEmit<EventName extends keyof EventsInterface> (eventName: EventName) {
|
|
550
570
|
const id = "*";
|
|
551
571
|
|
|
552
572
|
if (!this._eventListeners || !this._eventListeners[eventName]) {
|
|
553
573
|
return false;
|
|
554
574
|
}
|
|
555
575
|
|
|
556
|
-
const arr = this._eventListeners[eventName][id];
|
|
576
|
+
const arr = this._eventListeners[eventName]![id];
|
|
557
577
|
const arrCount = arr.length;
|
|
558
578
|
|
|
559
579
|
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
@@ -570,19 +590,19 @@ export class Emitter {
|
|
|
570
590
|
|
|
571
591
|
/**
|
|
572
592
|
* Checks if an event has any event listeners or not based on the passed id.
|
|
573
|
-
* @param
|
|
574
|
-
* @param
|
|
575
|
-
* @returns
|
|
593
|
+
* @param eventName The name of the event to check for.
|
|
594
|
+
* @param id The event ID to check for.
|
|
595
|
+
* @returns True if one or more event listeners are registered for
|
|
576
596
|
* the event. False if none are found.
|
|
577
597
|
*/
|
|
578
|
-
willEmitId (eventName:
|
|
598
|
+
willEmitId<EventName extends keyof EventsInterface> (eventName: EventName, id: string) {
|
|
579
599
|
if (!this._eventListeners || !this._eventListeners[eventName]) {
|
|
580
600
|
return false;
|
|
581
601
|
}
|
|
582
602
|
|
|
583
603
|
// Handle id emit
|
|
584
|
-
if (this._eventListeners[eventName][id]) {
|
|
585
|
-
const arr = this._eventListeners[eventName][id];
|
|
604
|
+
if (this._eventListeners[eventName]![id]) {
|
|
605
|
+
const arr = this._eventListeners[eventName]![id];
|
|
586
606
|
const arrCount = arr.length;
|
|
587
607
|
|
|
588
608
|
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
@@ -596,8 +616,8 @@ export class Emitter {
|
|
|
596
616
|
}
|
|
597
617
|
|
|
598
618
|
// Handle global emit
|
|
599
|
-
if (this._eventListeners[eventName]["*"]) {
|
|
600
|
-
const arr = this._eventListeners[eventName]["*"];
|
|
619
|
+
if (this._eventListeners[eventName]!["*"]) {
|
|
620
|
+
const arr = this._eventListeners[eventName]!["*"];
|
|
601
621
|
const arrCount = arr.length;
|
|
602
622
|
|
|
603
623
|
for (let arrIndex = 0; arrIndex < arrCount; arrIndex++) {
|
|
@@ -619,11 +639,11 @@ export class Emitter {
|
|
|
619
639
|
* one will all be wrapped into a single emit rather than emitting tons of
|
|
620
640
|
* events for lots of chained inserts etc. Only the data from the last
|
|
621
641
|
* de-bounced event will be emitted.
|
|
622
|
-
* @param
|
|
642
|
+
* @param eventName The name of the event to emit.
|
|
623
643
|
* @param {...any} data Optional arguments to emit with the event.
|
|
624
|
-
* @returns
|
|
644
|
+
* @returns The emitter instance.
|
|
625
645
|
*/
|
|
626
|
-
deferEmit (eventName:
|
|
646
|
+
deferEmit<EventName extends keyof EventsInterface> (eventName: EventName, ...data: Parameters<EventsInterface[EventName]>) {
|
|
627
647
|
if (!this._eventsAllowDefer) {
|
|
628
648
|
// Check for an existing timeout
|
|
629
649
|
this._eventsDeferTimeouts = this._eventsDeferTimeouts || {};
|
|
@@ -634,10 +654,10 @@ export class Emitter {
|
|
|
634
654
|
|
|
635
655
|
// Set a timeout
|
|
636
656
|
this._eventsDeferTimeouts[eventName] = setTimeout(() => {
|
|
637
|
-
this.emit.call(this, eventName, ...data);
|
|
657
|
+
void this.emit.call(this, eventName, ...data);
|
|
638
658
|
}, 1) as unknown as number;
|
|
639
659
|
} else {
|
|
640
|
-
this.emit.call(this, eventName, ...data);
|
|
660
|
+
void this.emit.call(this, eventName, ...data);
|
|
641
661
|
}
|
|
642
662
|
|
|
643
663
|
return this;
|
|
@@ -667,9 +687,9 @@ export class Emitter {
|
|
|
667
687
|
}
|
|
668
688
|
}
|
|
669
689
|
|
|
670
|
-
export function makeEmitter(obj:
|
|
671
|
-
export function makeEmitter(obj: boolean): Emitter;
|
|
672
|
-
export function makeEmitter(obj: Record<string, unknown>, prototypeMode: boolean): Emitter;
|
|
690
|
+
export function makeEmitter (obj: AnyConstructor, prototypeMode: boolean): Emitter;
|
|
691
|
+
export function makeEmitter (obj: boolean): Emitter;
|
|
692
|
+
export function makeEmitter (obj: Record<string, unknown>, prototypeMode: boolean): Emitter;
|
|
673
693
|
export function makeEmitter (obj: any, prototypeMode?: any) {
|
|
674
694
|
let operateOnObject;
|
|
675
695
|
|