emittery 1.0.0 → 1.0.2
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.js +19 -12
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -10,8 +10,10 @@ const listenerRemoved = Symbol('listenerRemoved');
|
|
|
10
10
|
let canEmitMetaEvents = false;
|
|
11
11
|
let isGlobalDebugEnabled = false;
|
|
12
12
|
|
|
13
|
+
const isEventKeyType = key => typeof key === 'string' || typeof key === 'symbol' || typeof key === 'number';
|
|
14
|
+
|
|
13
15
|
function assertEventName(eventName) {
|
|
14
|
-
if (
|
|
16
|
+
if (!isEventKeyType(eventName)) {
|
|
15
17
|
throw new TypeError('`eventName` must be a string, symbol, or number');
|
|
16
18
|
}
|
|
17
19
|
}
|
|
@@ -32,7 +34,7 @@ function getListeners(instance, eventName) {
|
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
function getEventProducers(instance, eventName) {
|
|
35
|
-
const key =
|
|
37
|
+
const key = isEventKeyType(eventName) ? eventName : anyProducer;
|
|
36
38
|
const producers = producersMap.get(instance);
|
|
37
39
|
if (!producers.has(key)) {
|
|
38
40
|
return;
|
|
@@ -216,13 +218,15 @@ export default class Emittery {
|
|
|
216
218
|
}
|
|
217
219
|
|
|
218
220
|
static get isDebugEnabled() {
|
|
221
|
+
// In a browser environment, `globalThis.process` can potentially reference a DOM Element with a `#process` ID,
|
|
222
|
+
// so instead of just type checking `globalThis.process`, we need to make sure that `globalThis.process.env` exists.
|
|
219
223
|
// eslint-disable-next-line n/prefer-global/process
|
|
220
|
-
if (typeof globalThis.process !== 'object') {
|
|
224
|
+
if (typeof globalThis.process?.env !== 'object') {
|
|
221
225
|
return isGlobalDebugEnabled;
|
|
222
226
|
}
|
|
223
227
|
|
|
224
228
|
// eslint-disable-next-line n/prefer-global/process
|
|
225
|
-
const {env} = globalThis.process
|
|
229
|
+
const {env} = globalThis.process ?? {env: {}};
|
|
226
230
|
return env.DEBUG === 'emittery' || env.DEBUG === '*' || isGlobalDebugEnabled;
|
|
227
231
|
}
|
|
228
232
|
|
|
@@ -237,7 +241,7 @@ export default class Emittery {
|
|
|
237
241
|
|
|
238
242
|
producersMap.get(this).set(anyProducer, new Set());
|
|
239
243
|
|
|
240
|
-
this.debug = options.debug
|
|
244
|
+
this.debug = options.debug ?? {};
|
|
241
245
|
|
|
242
246
|
if (this.debug.enabled === undefined) {
|
|
243
247
|
this.debug.enabled = false;
|
|
@@ -351,7 +355,7 @@ export default class Emittery {
|
|
|
351
355
|
|
|
352
356
|
enqueueProducers(this, eventName, eventData);
|
|
353
357
|
|
|
354
|
-
const listeners = getListeners(this, eventName)
|
|
358
|
+
const listeners = getListeners(this, eventName) ?? new Set();
|
|
355
359
|
const anyListeners = anyMap.get(this);
|
|
356
360
|
const staticListeners = [...listeners];
|
|
357
361
|
const staticAnyListeners = isMetaEvent(eventName) ? [] : [...anyListeners];
|
|
@@ -380,7 +384,7 @@ export default class Emittery {
|
|
|
380
384
|
|
|
381
385
|
this.logIfDebugEnabled('emitSerial', eventName, eventData);
|
|
382
386
|
|
|
383
|
-
const listeners = getListeners(this, eventName)
|
|
387
|
+
const listeners = getListeners(this, eventName) ?? new Set();
|
|
384
388
|
const anyListeners = anyMap.get(this);
|
|
385
389
|
const staticListeners = [...listeners];
|
|
386
390
|
const staticAnyListeners = [...anyListeners];
|
|
@@ -430,7 +434,7 @@ export default class Emittery {
|
|
|
430
434
|
for (const eventName of eventNames) {
|
|
431
435
|
this.logIfDebugEnabled('clear', eventName, undefined);
|
|
432
436
|
|
|
433
|
-
if (
|
|
437
|
+
if (isEventKeyType(eventName)) {
|
|
434
438
|
const set = getListeners(this, eventName);
|
|
435
439
|
if (set) {
|
|
436
440
|
set.clear();
|
|
@@ -469,13 +473,16 @@ export default class Emittery {
|
|
|
469
473
|
let count = 0;
|
|
470
474
|
|
|
471
475
|
for (const eventName of eventNames) {
|
|
472
|
-
if (
|
|
473
|
-
count += anyMap.get(this).size
|
|
474
|
-
+ (
|
|
476
|
+
if (isEventKeyType(eventName)) {
|
|
477
|
+
count += anyMap.get(this).size
|
|
478
|
+
+ (getListeners(this, eventName)?.size ?? 0)
|
|
479
|
+
+ (getEventProducers(this, eventName)?.size ?? 0)
|
|
480
|
+
+ (getEventProducers(this)?.size ?? 0);
|
|
481
|
+
|
|
475
482
|
continue;
|
|
476
483
|
}
|
|
477
484
|
|
|
478
|
-
if (
|
|
485
|
+
if (eventName !== undefined) {
|
|
479
486
|
assertEventName(eventName);
|
|
480
487
|
}
|
|
481
488
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emittery",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Simple and modern async event emitter",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/emittery",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"type": "module",
|
|
14
14
|
"exports": "./index.js",
|
|
15
15
|
"types": "./index.d.ts",
|
|
16
|
+
"sideEffects": false,
|
|
16
17
|
"engines": {
|
|
17
18
|
"node": ">=14.16"
|
|
18
19
|
},
|
|
@@ -58,7 +59,7 @@
|
|
|
58
59
|
"nyc": "^15.1.0",
|
|
59
60
|
"p-event": "^5.0.1",
|
|
60
61
|
"tsd": "^0.23.0",
|
|
61
|
-
"xo": "^0.
|
|
62
|
+
"xo": "^0.55.0"
|
|
62
63
|
},
|
|
63
64
|
"nyc": {
|
|
64
65
|
"reporter": [
|