emittery 0.13.1 → 1.0.1
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/index.d.ts +89 -94
- package/index.js +29 -26
- package/maps.js +3 -9
- package/package.json +12 -9
- package/readme.md +36 -34
package/index.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
/* eslint-disable no-redeclare */
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
Emittery accepts strings, symbols, and numbers as event names.
|
|
5
3
|
|
|
6
4
|
Symbol event names are preferred given that they can be used to avoid name collisions when your classes are extended, especially for internal events.
|
|
7
5
|
*/
|
|
8
|
-
type EventName = PropertyKey;
|
|
6
|
+
export type EventName = PropertyKey;
|
|
9
7
|
|
|
10
8
|
// Helper type for turning the passed `EventData` type map into a list of string keys that don't require data alongside the event name when emitting. Uses the same trick that `Omit` does internally to filter keys by building a map of keys to keys we want to keep, and then accessing all the keys to return just the list of keys we want to keep.
|
|
11
9
|
type DatalessEventNames<EventData> = {
|
|
@@ -14,7 +12,7 @@ type DatalessEventNames<EventData> = {
|
|
|
14
12
|
|
|
15
13
|
declare const listenerAdded: unique symbol;
|
|
16
14
|
declare const listenerRemoved: unique symbol;
|
|
17
|
-
type
|
|
15
|
+
type OmnipresentEventData = {[listenerAdded]: ListenerChangedData; [listenerRemoved]: ListenerChangedData};
|
|
18
16
|
|
|
19
17
|
/**
|
|
20
18
|
Emittery can collect and log debug information.
|
|
@@ -23,12 +21,12 @@ To enable this feature set the `DEBUG` environment variable to `emittery` or `*`
|
|
|
23
21
|
|
|
24
22
|
See API for more information on how debugging works.
|
|
25
23
|
*/
|
|
26
|
-
type DebugLogger<EventData, Name extends keyof EventData> = (type: string, debugName: string, eventName?: Name, eventData?: EventData[Name]) => void;
|
|
24
|
+
export type DebugLogger<EventData, Name extends keyof EventData> = (type: string, debugName: string, eventName?: Name, eventData?: EventData[Name]) => void;
|
|
27
25
|
|
|
28
26
|
/**
|
|
29
27
|
Configure debug options of an instance.
|
|
30
28
|
*/
|
|
31
|
-
|
|
29
|
+
export type DebugOptions<EventData> = {
|
|
32
30
|
/**
|
|
33
31
|
Define a name for the instance of Emittery to use when outputting debug data.
|
|
34
32
|
|
|
@@ -36,7 +34,7 @@ interface DebugOptions<EventData> {
|
|
|
36
34
|
|
|
37
35
|
@example
|
|
38
36
|
```
|
|
39
|
-
import Emittery
|
|
37
|
+
import Emittery from 'emittery';
|
|
40
38
|
|
|
41
39
|
Emittery.isDebugEnabled = true;
|
|
42
40
|
|
|
@@ -60,7 +58,7 @@ interface DebugOptions<EventData> {
|
|
|
60
58
|
|
|
61
59
|
@example
|
|
62
60
|
```
|
|
63
|
-
import Emittery
|
|
61
|
+
import Emittery from 'emittery';
|
|
64
62
|
|
|
65
63
|
const emitter1 = new Emittery({debug: {name: 'emitter1', enabled: true}});
|
|
66
64
|
const emitter2 = new Emittery({debug: {name: 'emitter2'}});
|
|
@@ -80,7 +78,7 @@ interface DebugOptions<EventData> {
|
|
|
80
78
|
emitter2.emit('test');
|
|
81
79
|
```
|
|
82
80
|
*/
|
|
83
|
-
enabled?: boolean;
|
|
81
|
+
readonly enabled?: boolean;
|
|
84
82
|
|
|
85
83
|
/**
|
|
86
84
|
Function that handles debug data.
|
|
@@ -102,9 +100,11 @@ interface DebugOptions<EventData> {
|
|
|
102
100
|
|
|
103
101
|
@example
|
|
104
102
|
```
|
|
105
|
-
import Emittery
|
|
103
|
+
import Emittery from 'emittery';
|
|
106
104
|
|
|
107
|
-
const myLogger = (type, debugName, eventName, eventData) =>
|
|
105
|
+
const myLogger = (type, debugName, eventName, eventData) => {
|
|
106
|
+
console.log(`[${type}]: ${eventName}`);
|
|
107
|
+
};
|
|
108
108
|
|
|
109
109
|
const emitter = new Emittery({
|
|
110
110
|
debug: {
|
|
@@ -122,22 +122,42 @@ interface DebugOptions<EventData> {
|
|
|
122
122
|
//=> [subscribe]: test
|
|
123
123
|
```
|
|
124
124
|
*/
|
|
125
|
-
logger?: DebugLogger<EventData, keyof EventData>;
|
|
126
|
-
}
|
|
125
|
+
readonly logger?: DebugLogger<EventData, keyof EventData>;
|
|
126
|
+
};
|
|
127
127
|
|
|
128
128
|
/**
|
|
129
129
|
Configuration options for Emittery.
|
|
130
130
|
*/
|
|
131
|
-
|
|
132
|
-
debug?: DebugOptions<EventData>;
|
|
133
|
-
}
|
|
131
|
+
export type Options<EventData> = {
|
|
132
|
+
readonly debug?: DebugOptions<EventData>;
|
|
133
|
+
};
|
|
134
134
|
|
|
135
135
|
/**
|
|
136
136
|
A promise returned from `emittery.once` with an extra `off` method to cancel your subscription.
|
|
137
137
|
*/
|
|
138
|
-
|
|
138
|
+
export type EmitteryOncePromise<T> = {
|
|
139
139
|
off(): void;
|
|
140
|
-
}
|
|
140
|
+
} & Promise<T>;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
Removes an event subscription.
|
|
144
|
+
*/
|
|
145
|
+
export type UnsubscribeFunction = () => void;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
The data provided as `eventData` when listening for `Emittery.listenerAdded` or `Emittery.listenerRemoved`.
|
|
149
|
+
*/
|
|
150
|
+
export type ListenerChangedData = {
|
|
151
|
+
/**
|
|
152
|
+
The listener that was added or removed.
|
|
153
|
+
*/
|
|
154
|
+
listener: (eventData?: unknown) => (void | Promise<void>);
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
The name of the event that was added or removed if `.on()` or `.off()` was used, or `undefined` if `.onAny()` or `.offAny()` was used.
|
|
158
|
+
*/
|
|
159
|
+
eventName?: EventName;
|
|
160
|
+
};
|
|
141
161
|
|
|
142
162
|
/**
|
|
143
163
|
Emittery is a strictly typed, fully async EventEmitter implementation. Event listeners can be registered with `on` or `once`, and events can be emitted with `emit`.
|
|
@@ -146,7 +166,7 @@ Emittery is a strictly typed, fully async EventEmitter implementation. Event lis
|
|
|
146
166
|
|
|
147
167
|
@example
|
|
148
168
|
```
|
|
149
|
-
import Emittery
|
|
169
|
+
import Emittery from 'emittery';
|
|
150
170
|
|
|
151
171
|
const emitter = new Emittery<
|
|
152
172
|
// Pass `{[eventName: <string | symbol | number>]: undefined | <eventArg>}` as the first type argument for events that pass data to their listeners.
|
|
@@ -170,10 +190,10 @@ emitter.emit('open', 1);
|
|
|
170
190
|
emitter.emit('other');
|
|
171
191
|
```
|
|
172
192
|
*/
|
|
173
|
-
|
|
174
|
-
EventData = Record<EventName, any>,
|
|
175
|
-
AllEventData = EventData &
|
|
176
|
-
DatalessEvents = DatalessEventNames<EventData
|
|
193
|
+
export default class Emittery<
|
|
194
|
+
EventData = Record<EventName, any>, // TODO: Use `unknown` instead of `any`.
|
|
195
|
+
AllEventData = EventData & OmnipresentEventData,
|
|
196
|
+
DatalessEvents = DatalessEventNames<EventData>,
|
|
177
197
|
> {
|
|
178
198
|
/**
|
|
179
199
|
Toggle debug mode for all instances.
|
|
@@ -182,7 +202,7 @@ declare class Emittery<
|
|
|
182
202
|
|
|
183
203
|
@example
|
|
184
204
|
```
|
|
185
|
-
import Emittery
|
|
205
|
+
import Emittery from 'emittery';
|
|
186
206
|
|
|
187
207
|
Emittery.isDebugEnabled = true;
|
|
188
208
|
|
|
@@ -215,7 +235,7 @@ declare class Emittery<
|
|
|
215
235
|
|
|
216
236
|
@example
|
|
217
237
|
```
|
|
218
|
-
import Emittery
|
|
238
|
+
import Emittery from 'emittery';
|
|
219
239
|
|
|
220
240
|
const emitter = new Emittery();
|
|
221
241
|
|
|
@@ -241,7 +261,7 @@ declare class Emittery<
|
|
|
241
261
|
|
|
242
262
|
@example
|
|
243
263
|
```
|
|
244
|
-
import Emittery
|
|
264
|
+
import Emittery from 'emittery';
|
|
245
265
|
|
|
246
266
|
const emitter = new Emittery();
|
|
247
267
|
|
|
@@ -262,24 +282,12 @@ declare class Emittery<
|
|
|
262
282
|
*/
|
|
263
283
|
static readonly listenerRemoved: typeof listenerRemoved;
|
|
264
284
|
|
|
265
|
-
/**
|
|
266
|
-
Debugging options for the current instance.
|
|
267
|
-
*/
|
|
268
|
-
debug: DebugOptions<EventData>;
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
Create a new Emittery instance with the specified options.
|
|
272
|
-
|
|
273
|
-
@returns An instance of Emittery that you can use to listen for and emit events.
|
|
274
|
-
*/
|
|
275
|
-
constructor(options?: Options<EventData>);
|
|
276
|
-
|
|
277
285
|
/**
|
|
278
286
|
In TypeScript, it returns a decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the target class.
|
|
279
287
|
|
|
280
288
|
@example
|
|
281
289
|
```
|
|
282
|
-
import Emittery
|
|
290
|
+
import Emittery from 'emittery';
|
|
283
291
|
|
|
284
292
|
@Emittery.mixin('emittery')
|
|
285
293
|
class MyClass {}
|
|
@@ -292,7 +300,19 @@ declare class Emittery<
|
|
|
292
300
|
static mixin(
|
|
293
301
|
emitteryPropertyName: string | symbol,
|
|
294
302
|
methodNames?: readonly string[]
|
|
295
|
-
): <T extends {new (...arguments_: any[]): any}>(klass: T) => T; // eslint-disable-line @typescript-eslint/prefer-function-type
|
|
303
|
+
): <T extends {new (...arguments_: readonly any[]): any}>(klass: T) => T; // eslint-disable-line @typescript-eslint/prefer-function-type
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
Debugging options for the current instance.
|
|
307
|
+
*/
|
|
308
|
+
debug: DebugOptions<EventData>;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
Create a new Emittery instance with the specified options.
|
|
312
|
+
|
|
313
|
+
@returns An instance of Emittery that you can use to listen for and emit events.
|
|
314
|
+
*/
|
|
315
|
+
constructor(options?: Options<EventData>);
|
|
296
316
|
|
|
297
317
|
/**
|
|
298
318
|
Subscribe to one or more events.
|
|
@@ -303,7 +323,7 @@ declare class Emittery<
|
|
|
303
323
|
|
|
304
324
|
@example
|
|
305
325
|
```
|
|
306
|
-
import Emittery
|
|
326
|
+
import Emittery from 'emittery';
|
|
307
327
|
|
|
308
328
|
const emitter = new Emittery();
|
|
309
329
|
|
|
@@ -320,9 +340,9 @@ declare class Emittery<
|
|
|
320
340
|
```
|
|
321
341
|
*/
|
|
322
342
|
on<Name extends keyof AllEventData>(
|
|
323
|
-
eventName: Name | Name[],
|
|
343
|
+
eventName: Name | readonly Name[],
|
|
324
344
|
listener: (eventData: AllEventData[Name]) => void | Promise<void>
|
|
325
|
-
):
|
|
345
|
+
): UnsubscribeFunction;
|
|
326
346
|
|
|
327
347
|
/**
|
|
328
348
|
Get an async iterator which buffers data each time an event is emitted.
|
|
@@ -331,7 +351,7 @@ declare class Emittery<
|
|
|
331
351
|
|
|
332
352
|
@example
|
|
333
353
|
```
|
|
334
|
-
import Emittery
|
|
354
|
+
import Emittery from 'emittery';
|
|
335
355
|
|
|
336
356
|
const emitter = new Emittery();
|
|
337
357
|
const iterator = emitter.events('🦄');
|
|
@@ -361,7 +381,7 @@ declare class Emittery<
|
|
|
361
381
|
|
|
362
382
|
@example
|
|
363
383
|
```
|
|
364
|
-
import Emittery
|
|
384
|
+
import Emittery from 'emittery';
|
|
365
385
|
|
|
366
386
|
const emitter = new Emittery();
|
|
367
387
|
const iterator = emitter.events('🦄');
|
|
@@ -381,7 +401,7 @@ declare class Emittery<
|
|
|
381
401
|
|
|
382
402
|
@example
|
|
383
403
|
```
|
|
384
|
-
import Emittery
|
|
404
|
+
import Emittery from 'emittery';
|
|
385
405
|
|
|
386
406
|
const emitter = new Emittery();
|
|
387
407
|
const iterator = emitter.events(['🦄', '🦊']);
|
|
@@ -408,7 +428,7 @@ declare class Emittery<
|
|
|
408
428
|
```
|
|
409
429
|
*/
|
|
410
430
|
events<Name extends keyof EventData>(
|
|
411
|
-
eventName: Name | Name[]
|
|
431
|
+
eventName: Name | readonly Name[]
|
|
412
432
|
): AsyncIterableIterator<EventData[Name]>;
|
|
413
433
|
|
|
414
434
|
/**
|
|
@@ -416,26 +436,27 @@ declare class Emittery<
|
|
|
416
436
|
|
|
417
437
|
@example
|
|
418
438
|
```
|
|
419
|
-
import Emittery
|
|
439
|
+
import Emittery from 'emittery';
|
|
420
440
|
|
|
421
441
|
const emitter = new Emittery();
|
|
422
442
|
|
|
423
|
-
const listener = data =>
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
443
|
+
const listener = data => {
|
|
444
|
+
console.log(data);
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
emitter.on(['🦄', '🐶', '🦊'], listener);
|
|
448
|
+
await emitter.emit('🦄', 'a');
|
|
449
|
+
await emitter.emit('🐶', 'b');
|
|
450
|
+
await emitter.emit('🦊', 'c');
|
|
451
|
+
emitter.off('🦄', listener);
|
|
452
|
+
emitter.off(['🐶', '🦊'], listener);
|
|
453
|
+
await emitter.emit('🦄', 'a'); // nothing happens
|
|
454
|
+
await emitter.emit('🐶', 'b'); // nothing happens
|
|
455
|
+
await emitter.emit('🦊', 'c'); // nothing happens
|
|
435
456
|
```
|
|
436
457
|
*/
|
|
437
458
|
off<Name extends keyof AllEventData>(
|
|
438
|
-
eventName: Name | Name[],
|
|
459
|
+
eventName: Name | readonly Name[],
|
|
439
460
|
listener: (eventData: AllEventData[Name]) => void | Promise<void>
|
|
440
461
|
): void;
|
|
441
462
|
|
|
@@ -447,7 +468,7 @@ declare class Emittery<
|
|
|
447
468
|
|
|
448
469
|
@example
|
|
449
470
|
```
|
|
450
|
-
import Emittery
|
|
471
|
+
import Emittery from 'emittery';
|
|
451
472
|
|
|
452
473
|
const emitter = new Emittery();
|
|
453
474
|
|
|
@@ -464,7 +485,7 @@ declare class Emittery<
|
|
|
464
485
|
emitter.emit('🐶', '🍖'); // Nothing happens
|
|
465
486
|
```
|
|
466
487
|
*/
|
|
467
|
-
once<Name extends keyof AllEventData>(eventName: Name | Name[]): EmitteryOncePromise<AllEventData[Name]>;
|
|
488
|
+
once<Name extends keyof AllEventData>(eventName: Name | readonly Name[]): EmitteryOncePromise<AllEventData[Name]>;
|
|
468
489
|
|
|
469
490
|
/**
|
|
470
491
|
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
|
|
@@ -500,7 +521,7 @@ declare class Emittery<
|
|
|
500
521
|
eventName: keyof EventData,
|
|
501
522
|
eventData: EventData[keyof EventData]
|
|
502
523
|
) => void | Promise<void>
|
|
503
|
-
):
|
|
524
|
+
): UnsubscribeFunction;
|
|
504
525
|
|
|
505
526
|
/**
|
|
506
527
|
Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
|
|
@@ -511,7 +532,7 @@ declare class Emittery<
|
|
|
511
532
|
|
|
512
533
|
@example
|
|
513
534
|
```
|
|
514
|
-
import Emittery
|
|
535
|
+
import Emittery from 'emittery';
|
|
515
536
|
|
|
516
537
|
const emitter = new Emittery();
|
|
517
538
|
const iterator = emitter.anyEvent();
|
|
@@ -555,19 +576,19 @@ declare class Emittery<
|
|
|
555
576
|
|
|
556
577
|
If `eventName` is given, only the listeners for that event are cleared.
|
|
557
578
|
*/
|
|
558
|
-
clearListeners<Name extends keyof EventData>(eventName?: Name | Name[]): void;
|
|
579
|
+
clearListeners<Name extends keyof EventData>(eventName?: Name | readonly Name[]): void;
|
|
559
580
|
|
|
560
581
|
/**
|
|
561
582
|
The number of listeners for the `eventName` or all events if not specified.
|
|
562
583
|
*/
|
|
563
|
-
listenerCount<Name extends keyof EventData>(eventName?: Name | Name[]): number;
|
|
584
|
+
listenerCount<Name extends keyof EventData>(eventName?: Name | readonly Name[]): number;
|
|
564
585
|
|
|
565
586
|
/**
|
|
566
587
|
Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
|
|
567
588
|
|
|
568
589
|
@example
|
|
569
590
|
```
|
|
570
|
-
import Emittery
|
|
591
|
+
import Emittery from 'emittery';
|
|
571
592
|
|
|
572
593
|
const object = {};
|
|
573
594
|
|
|
@@ -578,29 +599,3 @@ declare class Emittery<
|
|
|
578
599
|
*/
|
|
579
600
|
bindMethods(target: Record<string, unknown>, methodNames?: readonly string[]): void;
|
|
580
601
|
}
|
|
581
|
-
|
|
582
|
-
declare namespace Emittery {
|
|
583
|
-
/**
|
|
584
|
-
Removes an event subscription.
|
|
585
|
-
*/
|
|
586
|
-
type UnsubscribeFn = () => void;
|
|
587
|
-
|
|
588
|
-
/**
|
|
589
|
-
The data provided as `eventData` when listening for `Emittery.listenerAdded` or `Emittery.listenerRemoved`.
|
|
590
|
-
*/
|
|
591
|
-
interface ListenerChangedData {
|
|
592
|
-
/**
|
|
593
|
-
The listener that was added or removed.
|
|
594
|
-
*/
|
|
595
|
-
listener: (eventData?: unknown) => void | Promise<void>;
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
The name of the event that was added or removed if `.on()` or `.off()` was used, or `undefined` if `.onAny()` or `.offAny()` was used.
|
|
599
|
-
*/
|
|
600
|
-
eventName?: EventName;
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
type OmnipresentEventData = _OmnipresentEventData;
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
export = Emittery;
|
package/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const {anyMap, producersMap, eventsMap} = require('./maps.js');
|
|
1
|
+
import {anyMap, producersMap, eventsMap} from './maps.js';
|
|
4
2
|
|
|
5
3
|
const anyProducer = Symbol('anyProducer');
|
|
6
4
|
const resolvedPromise = Promise.resolve();
|
|
@@ -74,7 +72,7 @@ function iterator(instance, eventNames) {
|
|
|
74
72
|
finish() {
|
|
75
73
|
isFinished = true;
|
|
76
74
|
flush();
|
|
77
|
-
}
|
|
75
|
+
},
|
|
78
76
|
};
|
|
79
77
|
|
|
80
78
|
for (const eventName of eventNames) {
|
|
@@ -109,7 +107,7 @@ function iterator(instance, eventNames) {
|
|
|
109
107
|
|
|
110
108
|
return {
|
|
111
109
|
done: false,
|
|
112
|
-
value: await queue.shift()
|
|
110
|
+
value: await queue.shift(),
|
|
113
111
|
};
|
|
114
112
|
},
|
|
115
113
|
|
|
@@ -129,14 +127,14 @@ function iterator(instance, eventNames) {
|
|
|
129
127
|
|
|
130
128
|
flush();
|
|
131
129
|
|
|
132
|
-
return arguments.length > 0
|
|
133
|
-
{done: true, value: await value}
|
|
134
|
-
{done: true};
|
|
130
|
+
return arguments.length > 0
|
|
131
|
+
? {done: true, value: await value}
|
|
132
|
+
: {done: true};
|
|
135
133
|
},
|
|
136
134
|
|
|
137
135
|
[Symbol.asyncIterator]() {
|
|
138
136
|
return this;
|
|
139
|
-
}
|
|
137
|
+
},
|
|
140
138
|
};
|
|
141
139
|
}
|
|
142
140
|
|
|
@@ -175,7 +173,7 @@ function emitMetaEvent(emitter, eventName, eventData) {
|
|
|
175
173
|
}
|
|
176
174
|
}
|
|
177
175
|
|
|
178
|
-
class Emittery {
|
|
176
|
+
export default class Emittery {
|
|
179
177
|
static mixin(emitteryPropertyName, methodNames) {
|
|
180
178
|
methodNames = defaultMethodNamesOrAssert(methodNames);
|
|
181
179
|
return target => {
|
|
@@ -192,14 +190,14 @@ class Emittery {
|
|
|
192
190
|
function getEmitteryProperty() {
|
|
193
191
|
Object.defineProperty(this, emitteryPropertyName, {
|
|
194
192
|
enumerable: false,
|
|
195
|
-
value: new Emittery()
|
|
193
|
+
value: new Emittery(),
|
|
196
194
|
});
|
|
197
195
|
return this[emitteryPropertyName];
|
|
198
196
|
}
|
|
199
197
|
|
|
200
198
|
Object.defineProperty(target.prototype, emitteryPropertyName, {
|
|
201
199
|
enumerable: false,
|
|
202
|
-
get: getEmitteryProperty
|
|
200
|
+
get: getEmitteryProperty,
|
|
203
201
|
});
|
|
204
202
|
|
|
205
203
|
const emitteryMethodCaller = methodName => function (...args) {
|
|
@@ -209,7 +207,7 @@ class Emittery {
|
|
|
209
207
|
for (const methodName of methodNames) {
|
|
210
208
|
Object.defineProperty(target.prototype, methodName, {
|
|
211
209
|
enumerable: false,
|
|
212
|
-
value: emitteryMethodCaller(methodName)
|
|
210
|
+
value: emitteryMethodCaller(methodName),
|
|
213
211
|
});
|
|
214
212
|
}
|
|
215
213
|
|
|
@@ -218,11 +216,15 @@ class Emittery {
|
|
|
218
216
|
}
|
|
219
217
|
|
|
220
218
|
static get isDebugEnabled() {
|
|
221
|
-
|
|
219
|
+
// In a browser environment, `globalThis.process` can potentially reference a DOM Element with a `#process` ID,
|
|
220
|
+
// so instead of just type checking `globalThis.process`, we need to make sure that `globalThis.process.env` exists.
|
|
221
|
+
// eslint-disable-next-line n/prefer-global/process
|
|
222
|
+
if (typeof globalThis.process?.env !== 'object') {
|
|
222
223
|
return isGlobalDebugEnabled;
|
|
223
224
|
}
|
|
224
225
|
|
|
225
|
-
|
|
226
|
+
// eslint-disable-next-line n/prefer-global/process
|
|
227
|
+
const {env} = globalThis.process ?? {env: {}};
|
|
226
228
|
return env.DEBUG === 'emittery' || env.DEBUG === '*' || isGlobalDebugEnabled;
|
|
227
229
|
}
|
|
228
230
|
|
|
@@ -237,7 +239,7 @@ class Emittery {
|
|
|
237
239
|
|
|
238
240
|
producersMap.get(this).set(anyProducer, new Set());
|
|
239
241
|
|
|
240
|
-
this.debug = options.debug
|
|
242
|
+
this.debug = options.debug ?? {};
|
|
241
243
|
|
|
242
244
|
if (this.debug.enabled === undefined) {
|
|
243
245
|
this.debug.enabled = false;
|
|
@@ -351,7 +353,7 @@ class Emittery {
|
|
|
351
353
|
|
|
352
354
|
enqueueProducers(this, eventName, eventData);
|
|
353
355
|
|
|
354
|
-
const listeners = getListeners(this, eventName)
|
|
356
|
+
const listeners = getListeners(this, eventName) ?? new Set();
|
|
355
357
|
const anyListeners = anyMap.get(this);
|
|
356
358
|
const staticListeners = [...listeners];
|
|
357
359
|
const staticAnyListeners = isMetaEvent(eventName) ? [] : [...anyListeners];
|
|
@@ -367,7 +369,7 @@ class Emittery {
|
|
|
367
369
|
if (anyListeners.has(listener)) {
|
|
368
370
|
return listener(eventName, eventData);
|
|
369
371
|
}
|
|
370
|
-
})
|
|
372
|
+
}),
|
|
371
373
|
]);
|
|
372
374
|
}
|
|
373
375
|
|
|
@@ -380,7 +382,7 @@ class Emittery {
|
|
|
380
382
|
|
|
381
383
|
this.logIfDebugEnabled('emitSerial', eventName, eventData);
|
|
382
384
|
|
|
383
|
-
const listeners = getListeners(this, eventName)
|
|
385
|
+
const listeners = getListeners(this, eventName) ?? new Set();
|
|
384
386
|
const anyListeners = anyMap.get(this);
|
|
385
387
|
const staticListeners = [...listeners];
|
|
386
388
|
const staticAnyListeners = [...anyListeners];
|
|
@@ -470,8 +472,11 @@ class Emittery {
|
|
|
470
472
|
|
|
471
473
|
for (const eventName of eventNames) {
|
|
472
474
|
if (typeof eventName === 'string') {
|
|
473
|
-
count += anyMap.get(this).size
|
|
474
|
-
(
|
|
475
|
+
count += anyMap.get(this).size
|
|
476
|
+
+ (getListeners(this, eventName)?.size ?? 0)
|
|
477
|
+
+ (getEventProducers(this, eventName)?.size ?? 0)
|
|
478
|
+
+ (getEventProducers(this)?.size ?? 0);
|
|
479
|
+
|
|
475
480
|
continue;
|
|
476
481
|
}
|
|
477
482
|
|
|
@@ -507,7 +512,7 @@ class Emittery {
|
|
|
507
512
|
|
|
508
513
|
Object.defineProperty(target, methodName, {
|
|
509
514
|
enumerable: false,
|
|
510
|
-
value: this[methodName].bind(this)
|
|
515
|
+
value: this[methodName].bind(this),
|
|
511
516
|
});
|
|
512
517
|
}
|
|
513
518
|
}
|
|
@@ -519,13 +524,11 @@ Object.defineProperty(Emittery, 'listenerAdded', {
|
|
|
519
524
|
value: listenerAdded,
|
|
520
525
|
writable: false,
|
|
521
526
|
enumerable: true,
|
|
522
|
-
configurable: false
|
|
527
|
+
configurable: false,
|
|
523
528
|
});
|
|
524
529
|
Object.defineProperty(Emittery, 'listenerRemoved', {
|
|
525
530
|
value: listenerRemoved,
|
|
526
531
|
writable: false,
|
|
527
532
|
enumerable: true,
|
|
528
|
-
configurable: false
|
|
533
|
+
configurable: false,
|
|
529
534
|
});
|
|
530
|
-
|
|
531
|
-
module.exports = Emittery;
|
package/maps.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
const anyMap = new WeakMap();
|
|
2
|
-
const eventsMap = new WeakMap();
|
|
3
|
-
const producersMap = new WeakMap();
|
|
4
|
-
|
|
5
|
-
module.exports = {
|
|
6
|
-
anyMap,
|
|
7
|
-
eventsMap,
|
|
8
|
-
producersMap
|
|
9
|
-
};
|
|
1
|
+
export const anyMap = new WeakMap();
|
|
2
|
+
export const eventsMap = new WeakMap();
|
|
3
|
+
export const producersMap = new WeakMap();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emittery",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Simple and modern async event emitter",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/emittery",
|
|
@@ -10,8 +10,11 @@
|
|
|
10
10
|
"email": "sindresorhus@gmail.com",
|
|
11
11
|
"url": "https://sindresorhus.com"
|
|
12
12
|
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": "./index.js",
|
|
15
|
+
"types": "./index.d.ts",
|
|
13
16
|
"engines": {
|
|
14
|
-
"node": ">=
|
|
17
|
+
"node": ">=14.16"
|
|
15
18
|
},
|
|
16
19
|
"scripts": {
|
|
17
20
|
"test": "xo && nyc ava && tsd"
|
|
@@ -49,13 +52,13 @@
|
|
|
49
52
|
"typed"
|
|
50
53
|
],
|
|
51
54
|
"devDependencies": {
|
|
52
|
-
"@types/node": "^
|
|
53
|
-
"ava": "^
|
|
54
|
-
"delay": "^
|
|
55
|
-
"nyc": "^15.
|
|
56
|
-
"p-event": "^
|
|
57
|
-
"tsd": "^0.
|
|
58
|
-
"xo": "^0.
|
|
55
|
+
"@types/node": "^18.7.15",
|
|
56
|
+
"ava": "^4.3.3",
|
|
57
|
+
"delay": "^5.0.0",
|
|
58
|
+
"nyc": "^15.1.0",
|
|
59
|
+
"p-event": "^5.0.1",
|
|
60
|
+
"tsd": "^0.23.0",
|
|
61
|
+
"xo": "^0.52.3"
|
|
59
62
|
},
|
|
60
63
|
"nyc": {
|
|
61
64
|
"reporter": [
|
package/readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
> Simple and modern async event emitter
|
|
4
4
|
|
|
5
|
-
[](https://codecov.io/gh/sindresorhus/emittery)
|
|
5
|
+
<!-- [](https://codecov.io/gh/sindresorhus/emittery) -->
|
|
6
6
|
[](https://bundlephobia.com/result?p=emittery)
|
|
7
7
|
|
|
8
8
|
It works in Node.js and the browser (using a bundler).
|
|
@@ -11,14 +11,14 @@ Emitting events asynchronously is important for production code where you want t
|
|
|
11
11
|
|
|
12
12
|
## Install
|
|
13
13
|
|
|
14
|
-
```
|
|
15
|
-
|
|
14
|
+
```sh
|
|
15
|
+
npm install emittery
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
## Usage
|
|
19
19
|
|
|
20
20
|
```js
|
|
21
|
-
|
|
21
|
+
import Emittery from 'emittery';
|
|
22
22
|
|
|
23
23
|
const emitter = new Emittery();
|
|
24
24
|
|
|
@@ -53,7 +53,7 @@ Default: `true` if the `DEBUG` environment variable is set to `emittery` or `*`,
|
|
|
53
53
|
Example:
|
|
54
54
|
|
|
55
55
|
```js
|
|
56
|
-
|
|
56
|
+
import Emittery from 'emittery';
|
|
57
57
|
|
|
58
58
|
Emittery.isDebugEnabled = true;
|
|
59
59
|
|
|
@@ -103,7 +103,7 @@ Define a name for the instance of Emittery to use when outputting debug data.
|
|
|
103
103
|
Example:
|
|
104
104
|
|
|
105
105
|
```js
|
|
106
|
-
|
|
106
|
+
import Emittery from 'emittery';
|
|
107
107
|
|
|
108
108
|
Emittery.isDebugEnabled = true;
|
|
109
109
|
|
|
@@ -128,7 +128,7 @@ Toggle debug logging just for this instance.
|
|
|
128
128
|
Example:
|
|
129
129
|
|
|
130
130
|
```js
|
|
131
|
-
|
|
131
|
+
import Emittery from 'emittery';
|
|
132
132
|
|
|
133
133
|
const emitter1 = new Emittery({debug: {name: 'emitter1', enabled: true}});
|
|
134
134
|
const emitter2 = new Emittery({debug: {name: 'emitter2'}});
|
|
@@ -175,9 +175,11 @@ Function that handles debug data.
|
|
|
175
175
|
Example:
|
|
176
176
|
|
|
177
177
|
```js
|
|
178
|
-
|
|
178
|
+
import Emittery from 'emittery';
|
|
179
179
|
|
|
180
|
-
const myLogger = (type, debugName, eventName, eventData) =>
|
|
180
|
+
const myLogger = (type, debugName, eventName, eventData) => {
|
|
181
|
+
console.log(`[${type}]: ${eventName}`);
|
|
182
|
+
};
|
|
181
183
|
|
|
182
184
|
const emitter = new Emittery({
|
|
183
185
|
debug: {
|
|
@@ -204,7 +206,7 @@ Returns an unsubscribe method.
|
|
|
204
206
|
Using the same listener multiple times for the same event will result in only one method call per emitted event.
|
|
205
207
|
|
|
206
208
|
```js
|
|
207
|
-
|
|
209
|
+
import Emittery from 'emittery';
|
|
208
210
|
|
|
209
211
|
const emitter = new Emittery();
|
|
210
212
|
|
|
@@ -228,7 +230,7 @@ Emittery exports some symbols which represent "meta" events that can be passed t
|
|
|
228
230
|
- `Emittery.listenerRemoved` - Fires when an event listener was removed.
|
|
229
231
|
|
|
230
232
|
```js
|
|
231
|
-
|
|
233
|
+
import Emittery from 'emittery';
|
|
232
234
|
|
|
233
235
|
const emitter = new Emittery();
|
|
234
236
|
|
|
@@ -259,23 +261,23 @@ Only events that are not of this type are able to trigger these events.
|
|
|
259
261
|
Remove one or more event subscriptions.
|
|
260
262
|
|
|
261
263
|
```js
|
|
262
|
-
|
|
264
|
+
import Emittery from 'emittery';
|
|
263
265
|
|
|
264
266
|
const emitter = new Emittery();
|
|
265
267
|
|
|
266
|
-
const listener = data =>
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
268
|
+
const listener = data => {
|
|
269
|
+
console.log(data);
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
emitter.on(['🦄', '🐶', '🦊'], listener);
|
|
273
|
+
await emitter.emit('🦄', 'a');
|
|
274
|
+
await emitter.emit('🐶', 'b');
|
|
275
|
+
await emitter.emit('🦊', 'c');
|
|
276
|
+
emitter.off('🦄', listener);
|
|
277
|
+
emitter.off(['🐶', '🦊'], listener);
|
|
278
|
+
await emitter.emit('🦄', 'a'); // Nothing happens
|
|
279
|
+
await emitter.emit('🐶', 'b'); // Nothing happens
|
|
280
|
+
await emitter.emit('🦊', 'c'); // Nothing happens
|
|
279
281
|
```
|
|
280
282
|
|
|
281
283
|
##### listener(data)
|
|
@@ -287,7 +289,7 @@ Subscribe to one or more events only once. It will be unsubscribed after the fir
|
|
|
287
289
|
Returns a promise for the event data when `eventName` is emitted. This promise is extended with an `off` method.
|
|
288
290
|
|
|
289
291
|
```js
|
|
290
|
-
|
|
292
|
+
import Emittery from 'emittery';
|
|
291
293
|
|
|
292
294
|
const emitter = new Emittery();
|
|
293
295
|
|
|
@@ -311,7 +313,7 @@ Get an async iterator which buffers data each time an event is emitted.
|
|
|
311
313
|
Call `return()` on the iterator to remove the subscription.
|
|
312
314
|
|
|
313
315
|
```js
|
|
314
|
-
|
|
316
|
+
import Emittery from 'emittery';
|
|
315
317
|
|
|
316
318
|
const emitter = new Emittery();
|
|
317
319
|
const iterator = emitter.events('🦄');
|
|
@@ -340,7 +342,7 @@ iterator
|
|
|
340
342
|
In practice, you would usually consume the events using the [for await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) statement. In that case, to revoke the subscription simply break the loop.
|
|
341
343
|
|
|
342
344
|
```js
|
|
343
|
-
|
|
345
|
+
import Emittery from 'emittery';
|
|
344
346
|
|
|
345
347
|
const emitter = new Emittery();
|
|
346
348
|
const iterator = emitter.events('🦄');
|
|
@@ -359,7 +361,7 @@ for await (const data of iterator) {
|
|
|
359
361
|
It accepts multiple event names.
|
|
360
362
|
|
|
361
363
|
```js
|
|
362
|
-
|
|
364
|
+
import Emittery from 'emittery';
|
|
363
365
|
|
|
364
366
|
const emitter = new Emittery();
|
|
365
367
|
const iterator = emitter.events(['🦄', '🦊']);
|
|
@@ -416,7 +418,7 @@ Get an async iterator which buffers a tuple of an event name and data each time
|
|
|
416
418
|
Call `return()` on the iterator to remove the subscription.
|
|
417
419
|
|
|
418
420
|
```js
|
|
419
|
-
|
|
421
|
+
import Emittery from 'emittery';
|
|
420
422
|
|
|
421
423
|
const emitter = new Emittery();
|
|
422
424
|
const iterator = emitter.anyEvent();
|
|
@@ -458,7 +460,7 @@ The number of listeners for the `eventNames` or all events if not specified.
|
|
|
458
460
|
Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
|
|
459
461
|
|
|
460
462
|
```js
|
|
461
|
-
import Emittery
|
|
463
|
+
import Emittery from 'emittery';
|
|
462
464
|
|
|
463
465
|
const object = {};
|
|
464
466
|
|
|
@@ -472,7 +474,7 @@ object.emit('event');
|
|
|
472
474
|
The default `Emittery` class has generic types that can be provided by TypeScript users to strongly type the list of events and the data passed to their event listeners.
|
|
473
475
|
|
|
474
476
|
```ts
|
|
475
|
-
import Emittery
|
|
477
|
+
import Emittery from 'emittery';
|
|
476
478
|
|
|
477
479
|
const emitter = new Emittery<
|
|
478
480
|
// Pass `{[eventName]: undefined | <eventArg>}` as the first type argument for events that pass data to their listeners.
|
|
@@ -501,7 +503,7 @@ emitter.emit('other');
|
|
|
501
503
|
A decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the target class.
|
|
502
504
|
|
|
503
505
|
```ts
|
|
504
|
-
import Emittery
|
|
506
|
+
import Emittery from 'emittery';
|
|
505
507
|
|
|
506
508
|
@Emittery.mixin('emittery')
|
|
507
509
|
class MyClass {}
|
|
@@ -521,7 +523,7 @@ Note that when using `.emitSerial()`, a slow listener will delay invocation of s
|
|
|
521
523
|
|
|
522
524
|
Emittery can collect and log debug information.
|
|
523
525
|
|
|
524
|
-
To enable this feature set the DEBUG environment variable to 'emittery' or '*'
|
|
526
|
+
To enable this feature set the DEBUG environment variable to `'emittery'` or `'*'`. Additionally you can set the static `isDebugEnabled` variable to true on the Emittery class, or `myEmitter.debug.enabled` on an instance of it for debugging a single instance.
|
|
525
527
|
|
|
526
528
|
See [API](#api) for more details on how debugging works.
|
|
527
529
|
|