emittery 0.4.0 → 0.6.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/index.d.ts ADDED
@@ -0,0 +1,324 @@
1
+ /**
2
+ Emittery accepts strings and symbols as event names.
3
+
4
+ Symbol event names can be used to avoid name collisions when your classes are extended, especially for internal events.
5
+ */
6
+ type EventName = string | symbol;
7
+
8
+ declare class Emittery {
9
+ /**
10
+ 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.
11
+
12
+ @example
13
+ ```
14
+ import Emittery = require('emittery');
15
+
16
+ @Emittery.mixin('emittery')
17
+ class MyClass {}
18
+
19
+ const instance = new MyClass();
20
+
21
+ instance.emit('event');
22
+ ```
23
+ */
24
+ static mixin(emitteryPropertyName: string, methodNames?: readonly string[]): Function;
25
+
26
+ /**
27
+ Fires when an event listener was added.
28
+
29
+ An object with `listener` and `eventName` (if `on` or `off` was used) is provided as event data.
30
+
31
+ @example
32
+ ```
33
+ import Emittery = require('emittery');
34
+
35
+ const emitter = new Emittery();
36
+
37
+ emitter.on(Emittery.listenerAdded, ({listener, eventName}) => {
38
+ console.log(listener);
39
+ //=> data => {}
40
+
41
+ console.log(eventName);
42
+ //=> '🦄'
43
+ });
44
+
45
+ emitter.on('🦄', data => {
46
+ // Handle data
47
+ });
48
+ ```
49
+ */
50
+ static readonly listenerAdded: unique symbol;
51
+
52
+ /**
53
+ Fires when an event listener was removed.
54
+
55
+ An object with `listener` and `eventName` (if `on` or `off` was used) is provided as event data.
56
+
57
+ @example
58
+ ```
59
+ import Emittery = require('emittery');
60
+
61
+ const emitter = new Emittery();
62
+
63
+ const off = emitter.on('🦄', data => {
64
+ // Handle data
65
+ });
66
+
67
+ emitter.on(Emittery.listenerRemoved, ({listener, eventName}) => {
68
+ console.log(listener);
69
+ //=> data => {}
70
+
71
+ console.log(eventName);
72
+ //=> '🦄'
73
+ });
74
+
75
+ off();
76
+ ```
77
+ */
78
+ static readonly listenerRemoved: unique symbol;
79
+
80
+ /**
81
+ Subscribe to an event.
82
+
83
+ Using the same listener multiple times for the same event will result in only one method call per emitted event.
84
+
85
+ @returns An unsubscribe method.
86
+ */
87
+ on(eventName: typeof Emittery.listenerAdded | typeof Emittery.listenerRemoved, listener: (eventData: Emittery.ListenerChangedData) => void): Emittery.UnsubscribeFn
88
+ on(eventName: EventName, listener: (eventData?: unknown) => void): Emittery.UnsubscribeFn;
89
+
90
+ /**
91
+ Get an async iterator which buffers data each time an event is emitted.
92
+
93
+ Call `return()` on the iterator to remove the subscription.
94
+
95
+ @example
96
+ ```
97
+ import Emittery = require('emittery');
98
+
99
+ const emitter = new Emittery();
100
+ const iterator = emitter.events('🦄');
101
+
102
+ emitter.emit('🦄', '🌈1'); // Buffered
103
+ emitter.emit('🦄', '🌈2'); // Buffered
104
+
105
+ iterator
106
+ .next()
107
+ .then(({value, done}) => {
108
+ // done === false
109
+ // value === '🌈1'
110
+ return iterator.next();
111
+ })
112
+ .then(({value, done}) => {
113
+ // done === false
114
+ // value === '🌈2'
115
+ // Revoke subscription
116
+ return iterator.return();
117
+ })
118
+ .then(({done}) => {
119
+ // done === true
120
+ });
121
+ ```
122
+
123
+ 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.
124
+
125
+ @example
126
+ ```
127
+ import Emittery = require('emittery');
128
+
129
+ const emitter = new Emittery();
130
+ const iterator = emitter.events('🦄');
131
+
132
+ emitter.emit('🦄', '🌈1'); // Buffered
133
+ emitter.emit('🦄', '🌈2'); // Buffered
134
+
135
+ // In an async context.
136
+ for await (const data of iterator) {
137
+ if (data === '🌈2') {
138
+ break; // Revoke the subscription when we see the value `🌈2`.
139
+ }
140
+ }
141
+ ```
142
+ */
143
+ events(eventName: EventName): AsyncIterableIterator<unknown>
144
+
145
+ /**
146
+ Remove an event subscription.
147
+ */
148
+ off(eventName: EventName, listener: (eventData?: unknown) => void): void;
149
+
150
+ /**
151
+ Subscribe to an event only once. It will be unsubscribed after the first
152
+ event.
153
+
154
+ @returns The event data when `eventName` is emitted.
155
+ */
156
+ once(eventName: typeof Emittery.listenerAdded | typeof Emittery.listenerRemoved): Promise<Emittery.ListenerChangedData>
157
+ once(eventName: EventName): Promise<unknown>;
158
+
159
+ /**
160
+ Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
161
+
162
+ @returns A promise that resolves when all the event listeners are done. *Done* meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
163
+ */
164
+ emit(eventName: EventName, eventData?: unknown): Promise<void>;
165
+
166
+ /**
167
+ Same as `emit()`, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer `emit()` whenever possible.
168
+
169
+ If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will *not* be called.
170
+
171
+ @returns A promise that resolves when all the event listeners are done.
172
+ */
173
+ emitSerial(eventName: EventName, eventData?: unknown): Promise<void>;
174
+
175
+ /**
176
+ Subscribe to be notified about any event.
177
+
178
+ @returns A method to unsubscribe.
179
+ */
180
+ onAny(listener: (eventName: EventName, eventData?: unknown) => unknown): Emittery.UnsubscribeFn;
181
+
182
+ /**
183
+ Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
184
+
185
+ Call `return()` on the iterator to remove the subscription.
186
+
187
+ In the same way as for `events`, you can subscribe by using the `for await` statement.
188
+
189
+ @example
190
+ ```
191
+ import Emittery = require('emittery');
192
+
193
+ const emitter = new Emittery();
194
+ const iterator = emitter.anyEvent();
195
+
196
+ emitter.emit('🦄', '🌈1'); // Buffered
197
+ emitter.emit('🌟', '🌈2'); // Buffered
198
+
199
+ iterator.next()
200
+ .then(({value, done}) => {
201
+ // done is false
202
+ // value is ['🦄', '🌈1']
203
+ return iterator.next();
204
+ })
205
+ .then(({value, done}) => {
206
+ // done is false
207
+ // value is ['🌟', '🌈2']
208
+ // revoke subscription
209
+ return iterator.return();
210
+ })
211
+ .then(({done}) => {
212
+ // done is true
213
+ });
214
+ ```
215
+ */
216
+ anyEvent(): AsyncIterableIterator<unknown>
217
+
218
+ /**
219
+ Remove an `onAny` subscription.
220
+ */
221
+ offAny(listener: (eventName: EventName, eventData?: unknown) => void): void;
222
+
223
+ /**
224
+ Clear all event listeners on the instance.
225
+
226
+ If `eventName` is given, only the listeners for that event are cleared.
227
+ */
228
+ clearListeners(eventName?: EventName): void;
229
+
230
+ /**
231
+ The number of listeners for the `eventName` or all events if not specified.
232
+ */
233
+ listenerCount(eventName?: EventName): number;
234
+
235
+ /**
236
+ Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
237
+
238
+ @example
239
+ ```
240
+ import Emittery = require('emittery');
241
+
242
+ const object = {};
243
+
244
+ new Emittery().bindMethods(object);
245
+
246
+ object.emit('event');
247
+ ```
248
+ */
249
+ bindMethods(target: object, methodNames?: readonly string[]): void;
250
+ }
251
+
252
+ declare namespace Emittery {
253
+ /**
254
+ Removes an event subscription.
255
+ */
256
+ type UnsubscribeFn = () => void;
257
+ type EventNameFromDataMap<EventDataMap> = Extract<keyof EventDataMap, EventName>;
258
+
259
+ /**
260
+ Maps event names to their emitted data type.
261
+ */
262
+ interface Events {
263
+ // Blocked by https://github.com/microsoft/TypeScript/issues/1863, should be
264
+ // `[eventName: EventName]: unknown;`
265
+ }
266
+
267
+ /**
268
+ The data provided as `eventData` when listening for `Emittery.listenerAdded` or `Emittery.listenerRemoved`.
269
+ */
270
+ interface ListenerChangedData {
271
+ /**
272
+ The listener that was added or removed.
273
+ */
274
+ listener: (eventData?: unknown) => void;
275
+
276
+ /**
277
+ The name of the event that was added or removed if `.on()` or `.off()` was used, or `undefined` if `.onAny()` or `.offAny()` was used.
278
+ */
279
+ eventName?: EventName;
280
+ }
281
+
282
+ /**
283
+ Async event emitter.
284
+
285
+ You must list supported events and the data type they emit, if any.
286
+
287
+ @example
288
+ ```
289
+ import Emittery = require('emittery');
290
+
291
+ const emitter = new Emittery.Typed<{value: string}, 'open' | 'close'>();
292
+
293
+ emitter.emit('open');
294
+ emitter.emit('value', 'foo\n');
295
+ emitter.emit('value', 1); // TS compilation error
296
+ emitter.emit('end'); // TS compilation error
297
+ ```
298
+ */
299
+ class Typed<EventDataMap extends Events, EmptyEvents extends EventName = never> extends Emittery {
300
+ on<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, listener: (eventData: EventDataMap[Name]) => void): Emittery.UnsubscribeFn;
301
+ on<Name extends EmptyEvents>(eventName: Name, listener: () => void): Emittery.UnsubscribeFn;
302
+
303
+ events<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name): AsyncIterableIterator<EventDataMap[Name]>;
304
+
305
+ once<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name): Promise<EventDataMap[Name]>;
306
+ once<Name extends EmptyEvents>(eventName: Name): Promise<void>;
307
+
308
+ off<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, listener: (eventData: EventDataMap[Name]) => void): void;
309
+ off<Name extends EmptyEvents>(eventName: Name, listener: () => void): void;
310
+
311
+ onAny(listener: (eventName: EventNameFromDataMap<EventDataMap> | EmptyEvents, eventData?: EventDataMap[EventNameFromDataMap<EventDataMap>]) => void): Emittery.UnsubscribeFn;
312
+ anyEvent(): AsyncIterableIterator<[EventNameFromDataMap<EventDataMap>, EventDataMap[EventNameFromDataMap<EventDataMap>]]>;
313
+
314
+ offAny(listener: (eventName: EventNameFromDataMap<EventDataMap> | EmptyEvents, eventData?: EventDataMap[EventNameFromDataMap<EventDataMap>]) => void): void;
315
+
316
+ emit<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
317
+ emit<Name extends EmptyEvents>(eventName: Name): Promise<void>;
318
+
319
+ emitSerial<Name extends EventNameFromDataMap<EventDataMap>>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
320
+ emitSerial<Name extends EmptyEvents>(eventName: Name): Promise<void>;
321
+ }
322
+ }
323
+
324
+ export = Emittery;
package/index.js CHANGED
@@ -2,11 +2,16 @@
2
2
 
3
3
  const anyMap = new WeakMap();
4
4
  const eventsMap = new WeakMap();
5
+ const producersMap = new WeakMap();
6
+ const anyProducer = Symbol('anyProducer');
5
7
  const resolvedPromise = Promise.resolve();
6
8
 
9
+ const listenerAdded = Symbol('listenerAdded');
10
+ const listenerRemoved = Symbol('listenerRemoved');
11
+
7
12
  function assertEventName(eventName) {
8
- if (typeof eventName !== 'string') {
9
- throw new TypeError('eventName must be a string');
13
+ if (typeof eventName !== 'string' && typeof eventName !== 'symbol') {
14
+ throw new TypeError('eventName must be a string or a symbol');
10
15
  }
11
16
  }
12
17
 
@@ -25,22 +30,183 @@ function getListeners(instance, eventName) {
25
30
  return events.get(eventName);
26
31
  }
27
32
 
33
+ function getEventProducers(instance, eventName) {
34
+ const key = typeof eventName === 'string' ? eventName : anyProducer;
35
+ const producers = producersMap.get(instance);
36
+ if (!producers.has(key)) {
37
+ producers.set(key, new Set());
38
+ }
39
+
40
+ return producers.get(key);
41
+ }
42
+
43
+ function enqueueProducers(instance, eventName, eventData) {
44
+ const producers = producersMap.get(instance);
45
+ if (producers.has(eventName)) {
46
+ for (const producer of producers.get(eventName)) {
47
+ producer.enqueue(eventData);
48
+ }
49
+ }
50
+
51
+ if (producers.has(anyProducer)) {
52
+ const item = Promise.all([eventName, eventData]);
53
+ for (const producer of producers.get(anyProducer)) {
54
+ producer.enqueue(item);
55
+ }
56
+ }
57
+ }
58
+
59
+ function iterator(instance, eventName) {
60
+ let isFinished = false;
61
+ let flush = () => {};
62
+ let queue = [];
63
+
64
+ const producer = {
65
+ enqueue(item) {
66
+ queue.push(item);
67
+ flush();
68
+ },
69
+ finish() {
70
+ isFinished = true;
71
+ flush();
72
+ }
73
+ };
74
+
75
+ getEventProducers(instance, eventName).add(producer);
76
+
77
+ return {
78
+ async next() {
79
+ if (!queue) {
80
+ return {done: true};
81
+ }
82
+
83
+ if (queue.length === 0) {
84
+ if (isFinished) {
85
+ queue = undefined;
86
+ return this.next();
87
+ }
88
+
89
+ await new Promise(resolve => {
90
+ flush = resolve;
91
+ });
92
+
93
+ return this.next();
94
+ }
95
+
96
+ return {
97
+ done: false,
98
+ value: await queue.shift()
99
+ };
100
+ },
101
+
102
+ async return(value) {
103
+ queue = undefined;
104
+ getEventProducers(instance, eventName).delete(producer);
105
+ flush();
106
+
107
+ return arguments.length > 0 ?
108
+ {done: true, value: await value} :
109
+ {done: true};
110
+ },
111
+
112
+ [Symbol.asyncIterator]() {
113
+ return this;
114
+ }
115
+ };
116
+ }
117
+
118
+ function defaultMethodNamesOrAssert(methodNames) {
119
+ if (methodNames === undefined) {
120
+ return allEmitteryMethods;
121
+ }
122
+
123
+ if (!Array.isArray(methodNames)) {
124
+ throw new TypeError('`methodNames` must be an array of strings');
125
+ }
126
+
127
+ for (const methodName of methodNames) {
128
+ if (!allEmitteryMethods.includes(methodName)) {
129
+ if (typeof methodName !== 'string') {
130
+ throw new TypeError('`methodNames` element must be a string');
131
+ }
132
+
133
+ throw new Error(`${methodName} is not Emittery method`);
134
+ }
135
+ }
136
+
137
+ return methodNames;
138
+ }
139
+
140
+ const isListenerSymbol = symbol => symbol === listenerAdded || symbol === listenerRemoved;
141
+
28
142
  class Emittery {
143
+ static mixin(emitteryPropertyName, methodNames) {
144
+ methodNames = defaultMethodNamesOrAssert(methodNames);
145
+ return target => {
146
+ if (typeof target !== 'function') {
147
+ throw new TypeError('`target` must be function');
148
+ }
149
+
150
+ for (const methodName of methodNames) {
151
+ if (target.prototype[methodName] !== undefined) {
152
+ throw new Error(`The property \`${methodName}\` already exists on \`target\``);
153
+ }
154
+ }
155
+
156
+ function getEmitteryProperty() {
157
+ Object.defineProperty(this, emitteryPropertyName, {
158
+ enumerable: false,
159
+ value: new Emittery()
160
+ });
161
+ return this[emitteryPropertyName];
162
+ }
163
+
164
+ Object.defineProperty(target.prototype, emitteryPropertyName, {
165
+ enumerable: false,
166
+ get: getEmitteryProperty
167
+ });
168
+
169
+ const emitteryMethodCaller = methodName => function (...args) {
170
+ return this[emitteryPropertyName][methodName](...args);
171
+ };
172
+
173
+ for (const methodName of methodNames) {
174
+ Object.defineProperty(target.prototype, methodName, {
175
+ enumerable: false,
176
+ value: emitteryMethodCaller(methodName)
177
+ });
178
+ }
179
+
180
+ return target;
181
+ };
182
+ }
183
+
29
184
  constructor() {
30
185
  anyMap.set(this, new Set());
31
186
  eventsMap.set(this, new Map());
187
+ producersMap.set(this, new Map());
32
188
  }
33
189
 
34
190
  on(eventName, listener) {
35
191
  assertEventName(eventName);
36
192
  assertListener(listener);
37
193
  getListeners(this, eventName).add(listener);
194
+
195
+ if (!isListenerSymbol(eventName)) {
196
+ this.emit(listenerAdded, {eventName, listener});
197
+ }
198
+
38
199
  return this.off.bind(this, eventName, listener);
39
200
  }
40
201
 
41
202
  off(eventName, listener) {
42
203
  assertEventName(eventName);
43
204
  assertListener(listener);
205
+
206
+ if (!isListenerSymbol(eventName)) {
207
+ this.emit(listenerRemoved, {eventName, listener});
208
+ }
209
+
44
210
  getListeners(this, eventName).delete(listener);
45
211
  }
46
212
 
@@ -54,13 +220,20 @@ class Emittery {
54
220
  });
55
221
  }
56
222
 
223
+ events(eventName) {
224
+ assertEventName(eventName);
225
+ return iterator(this, eventName);
226
+ }
227
+
57
228
  async emit(eventName, eventData) {
58
229
  assertEventName(eventName);
59
230
 
231
+ enqueueProducers(this, eventName, eventData);
232
+
60
233
  const listeners = getListeners(this, eventName);
61
234
  const anyListeners = anyMap.get(this);
62
235
  const staticListeners = [...listeners];
63
- const staticAnyListeners = [...anyListeners];
236
+ const staticAnyListeners = isListenerSymbol(eventName) ? [] : [...anyListeners];
64
237
 
65
238
  await resolvedPromise;
66
239
  return Promise.all([
@@ -104,28 +277,52 @@ class Emittery {
104
277
  onAny(listener) {
105
278
  assertListener(listener);
106
279
  anyMap.get(this).add(listener);
280
+ this.emit(listenerAdded, {listener});
107
281
  return this.offAny.bind(this, listener);
108
282
  }
109
283
 
284
+ anyEvent() {
285
+ return iterator(this);
286
+ }
287
+
110
288
  offAny(listener) {
111
289
  assertListener(listener);
290
+ this.emit(listenerRemoved, {listener});
112
291
  anyMap.get(this).delete(listener);
113
292
  }
114
293
 
115
294
  clearListeners(eventName) {
116
295
  if (typeof eventName === 'string') {
117
296
  getListeners(this, eventName).clear();
297
+
298
+ const producers = getEventProducers(this, eventName);
299
+
300
+ for (const producer of producers) {
301
+ producer.finish();
302
+ }
303
+
304
+ producers.clear();
118
305
  } else {
119
306
  anyMap.get(this).clear();
307
+
120
308
  for (const listeners of eventsMap.get(this).values()) {
121
309
  listeners.clear();
122
310
  }
311
+
312
+ for (const producers of producersMap.get(this).values()) {
313
+ for (const producer of producers) {
314
+ producer.finish();
315
+ }
316
+
317
+ producers.clear();
318
+ }
123
319
  }
124
320
  }
125
321
 
126
322
  listenerCount(eventName) {
127
323
  if (typeof eventName === 'string') {
128
- return anyMap.get(this).size + getListeners(this, eventName).size;
324
+ return anyMap.get(this).size + getListeners(this, eventName).size +
325
+ getEventProducers(this, eventName).size + getEventProducers(this).size;
129
326
  }
130
327
 
131
328
  if (typeof eventName !== 'undefined') {
@@ -138,10 +335,35 @@ class Emittery {
138
335
  count += value.size;
139
336
  }
140
337
 
338
+ for (const value of producersMap.get(this).values()) {
339
+ count += value.size;
340
+ }
341
+
141
342
  return count;
142
343
  }
344
+
345
+ bindMethods(target, methodNames) {
346
+ if (typeof target !== 'object' || target === null) {
347
+ throw new TypeError('`target` must be an object');
348
+ }
349
+
350
+ methodNames = defaultMethodNamesOrAssert(methodNames);
351
+
352
+ for (const methodName of methodNames) {
353
+ if (target[methodName] !== undefined) {
354
+ throw new Error(`The property \`${methodName}\` already exists on \`target\``);
355
+ }
356
+
357
+ Object.defineProperty(target, methodName, {
358
+ enumerable: false,
359
+ value: this[methodName].bind(this)
360
+ });
361
+ }
362
+ }
143
363
  }
144
364
 
365
+ const allEmitteryMethods = Object.getOwnPropertyNames(Emittery.prototype).filter(v => v !== 'constructor');
366
+
145
367
  // Subclass used to encourage TS users to type their events.
146
368
  Emittery.Typed = class extends Emittery {};
147
369
  Object.defineProperty(Emittery.Typed, 'Typed', {
@@ -149,4 +371,17 @@ Object.defineProperty(Emittery.Typed, 'Typed', {
149
371
  value: undefined
150
372
  });
151
373
 
374
+ Object.defineProperty(Emittery, 'listenerAdded', {
375
+ value: listenerAdded,
376
+ writable: false,
377
+ enumerable: true,
378
+ configurable: false
379
+ });
380
+ Object.defineProperty(Emittery, 'listenerRemoved', {
381
+ value: listenerRemoved,
382
+ writable: false,
383
+ enumerable: true,
384
+ configurable: false
385
+ });
386
+
152
387
  module.exports = Emittery;
package/license CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
package/package.json CHANGED
@@ -1,30 +1,25 @@
1
1
  {
2
2
  "name": "emittery",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Simple and modern async event emitter",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/emittery",
7
+ "funding": "https://github.com/sindresorhus/emittery?sponsor=1",
7
8
  "author": {
8
9
  "name": "Sindre Sorhus",
9
10
  "email": "sindresorhus@gmail.com",
10
- "url": "sindresorhus.com"
11
+ "url": "https://sindresorhus.com"
11
12
  },
12
13
  "engines": {
13
- "node": ">=6"
14
+ "node": ">=10"
14
15
  },
15
16
  "scripts": {
16
- "build": "babel --out-file=legacy.js index.js",
17
- "build:watch": "npm run build -- --watch",
18
- "prepare": "npm run build",
19
- "test": "xo && tsc --noEmit && nyc ava"
17
+ "test": "xo && nyc ava && tsd"
20
18
  },
21
19
  "files": [
22
- "Emittery.d.ts",
23
20
  "index.js",
24
- "legacy.js",
25
- "legacy.d.ts"
21
+ "index.d.ts"
26
22
  ],
27
- "typings": "./Emittery.d.ts",
28
23
  "keywords": [
29
24
  "event",
30
25
  "emitter",
@@ -47,26 +42,20 @@
47
42
  "observer",
48
43
  "trigger",
49
44
  "await",
50
- "promise"
45
+ "promise",
46
+ "typescript",
47
+ "ts",
48
+ "typed"
51
49
  ],
52
50
  "devDependencies": {
53
- "@types/node": "^10.1.2",
54
- "ava": "*",
55
- "babel-cli": "^6.26.0",
56
- "babel-core": "^6.26.0",
57
- "babel-plugin-transform-async-to-generator": "^6.24.1",
58
- "codecov": "^3.0.0",
59
- "delay": "^3.0.0",
60
- "glob": "^7.1.2",
61
- "nyc": "^12.0.2",
62
- "ts-node": "^6.0.3",
63
- "typescript": "^2.9.0",
64
- "xo": "*"
65
- },
66
- "babel": {
67
- "plugins": [
68
- "transform-async-to-generator"
69
- ]
51
+ "@types/node": "^13.7.5",
52
+ "ava": "^2.4.0",
53
+ "codecov": "^3.1.0",
54
+ "delay": "^4.3.0",
55
+ "nyc": "^15.0.0",
56
+ "p-event": "^4.1.0",
57
+ "tsd": "^0.11.0",
58
+ "xo": "^0.25.4"
70
59
  },
71
60
  "nyc": {
72
61
  "reporter": [
package/readme.md CHANGED
@@ -2,14 +2,11 @@
2
2
 
3
3
  > Simple and modern async event emitter
4
4
 
5
- [![Build Status](https://travis-ci.org/sindresorhus/emittery.svg?branch=master)](https://travis-ci.org/sindresorhus/emittery) [![codecov](https://codecov.io/gh/sindresorhus/emittery/branch/master/graph/badge.svg)](https://codecov.io/gh/sindresorhus/emittery)
5
+ [![Build Status](https://travis-ci.org/sindresorhus/emittery.svg?branch=master)](https://travis-ci.org/sindresorhus/emittery) [![codecov](https://codecov.io/gh/sindresorhus/emittery/branch/master/graph/badge.svg)](https://codecov.io/gh/sindresorhus/emittery) [![](https://badgen.net/bundlephobia/minzip/emittery)](https://bundlephobia.com/result?p=emittery)
6
6
 
7
- It's only ~200 bytes minified and gzipped. [I'm not fanatic about keeping the size at this level though.](https://github.com/sindresorhus/emittery/pull/5#issuecomment-347479211)
8
-
9
- It's works in Node.js and the browser (using a bundler).
10
-
11
- Emitting events asynchronously is important for production code where you want the least amount of synchronous operations.
7
+ It works in Node.js and the browser (using a bundler).
12
8
 
9
+ Emitting events asynchronously is important for production code where you want the least amount of synchronous operations. Since JavaScript is single-threaded, no other code can run while doing synchronous operations. For Node.js, that means it will block other requests, defeating the strength of the platform, which is scalability through async. In the browser, a synchronous operation could potentially cause lags and block user interaction.
13
10
 
14
11
  ## Install
15
12
 
@@ -17,27 +14,34 @@ Emitting events asynchronously is important for production code where you want t
17
14
  $ npm install emittery
18
15
  ```
19
16
 
20
-
21
17
  ## Usage
22
18
 
23
19
  ```js
24
20
  const Emittery = require('emittery');
21
+
25
22
  const emitter = new Emittery();
26
23
 
27
24
  emitter.on('🦄', data => {
28
25
  console.log(data);
29
- // '🌈'
30
26
  });
31
27
 
32
- emitter.emit('🦄', '🌈');
28
+ const myUnicorn = Symbol('🦄');
29
+
30
+ emitter.on(myUnicorn, data => {
31
+ console.log(`Unicorns love ${data}`);
32
+ });
33
+
34
+ emitter.emit('🦄', '🌈'); // Will trigger printing '🌈'
35
+ emitter.emit(myUnicorn, '🦋'); // Will trigger printing 'Unicorns love 🦋'
33
36
  ```
34
37
 
35
- ### Node.js 6
38
+ ## API
36
39
 
37
- The above only works in Node.js 8 or newer. For older Node.js versions you can use `require('emittery/legacy')`.
40
+ ### eventName
38
41
 
42
+ Emittery accepts strings and symbols as event names.
39
43
 
40
- ## API
44
+ Symbol event names can be used to avoid name collisions when your classes are extended, especially for internal events.
41
45
 
42
46
  ### emitter = new Emittery()
43
47
 
@@ -49,6 +53,38 @@ Returns an unsubscribe method.
49
53
 
50
54
  Using the same listener multiple times for the same event will result in only one method call per emitted event.
51
55
 
56
+ ##### Custom subscribable events
57
+
58
+ Emittery exports some symbols which represent custom events that can be passed to `Emitter.on` and similar methods.
59
+
60
+ - `Emittery.listenerAdded` - Fires when an event listener was added.
61
+ - `Emittery.listenerRemoved` - Fires when an event listener was removed.
62
+
63
+ ```js
64
+ const Emittery = require('emittery');
65
+
66
+ const emitter = new Emittery();
67
+
68
+ emitter.on(Emittery.listenerAdded, ({listener, eventName}) => {
69
+ console.log(listener);
70
+ //=> data => {}
71
+
72
+ console.log(eventName);
73
+ //=> '🦄'
74
+ });
75
+
76
+ emitter.on('🦄', data => {
77
+ // Handle data
78
+ });
79
+ ```
80
+
81
+ ###### Listener data
82
+
83
+ - `listener` - The listener that was added.
84
+ - `eventName` - The name of the event that was added or removed if `.on()` or `.off()` was used, or `undefined` if `.onAny()` or `.offAny()` was used.
85
+
86
+ Only events that are not of this type are able to trigger these events.
87
+
52
88
  ##### listener(data)
53
89
 
54
90
  #### off(eventName, listener)
@@ -64,6 +100,10 @@ Subscribe to an event only once. It will be unsubscribed after the first event.
64
100
  Returns a promise for the event data when `eventName` is emitted.
65
101
 
66
102
  ```js
103
+ const Emittery = require('emittery');
104
+
105
+ const emitter = new Emittery();
106
+
67
107
  emitter.once('🦄').then(data => {
68
108
  console.log(data);
69
109
  //=> '🌈'
@@ -72,13 +112,65 @@ emitter.once('🦄').then(data => {
72
112
  emitter.emit('🦄', '🌈');
73
113
  ```
74
114
 
75
- #### emit(eventName, [data])
115
+ #### events(eventName)
76
116
 
77
- Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but execute concurrently.
117
+ Get an async iterator which buffers data each time an event is emitted.
78
118
 
79
- Returns a promise for when all the event listeners are done. *Done* meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
119
+ Call `return()` on the iterator to remove the subscription.
80
120
 
81
- #### emitSerial(eventName, [data])
121
+ ```js
122
+ const Emittery = require('emittery');
123
+
124
+ const emitter = new Emittery();
125
+ const iterator = emitter.events('🦄');
126
+
127
+ emitter.emit('🦄', '🌈1'); // Buffered
128
+ emitter.emit('🦄', '🌈2'); // Buffered
129
+
130
+ iterator
131
+ .next()
132
+ .then(({value, done}) => {
133
+ // done === false
134
+ // value === '🌈1'
135
+ return iterator.next();
136
+ })
137
+ .then(({value, done}) => {
138
+ // done === false
139
+ // value === '🌈2'
140
+ // Revoke subscription
141
+ return iterator.return();
142
+ })
143
+ .then(({done}) => {
144
+ // done === true
145
+ });
146
+ ```
147
+
148
+ 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.
149
+
150
+ ```js
151
+ const Emittery = require('emittery');
152
+
153
+ const emitter = new Emittery();
154
+ const iterator = emitter.events('🦄');
155
+
156
+ emitter.emit('🦄', '🌈1'); // Buffered
157
+ emitter.emit('🦄', '🌈2'); // Buffered
158
+
159
+ // In an async context.
160
+ for await (const data of iterator) {
161
+ if (data === '🌈2') {
162
+ break; // Revoke the subscription when we see the value '🌈2'.
163
+ }
164
+ }
165
+ ```
166
+
167
+ #### emit(eventName, data?)
168
+
169
+ Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
170
+
171
+ Returns a promise that resolves when all the event listeners are done. *Done* meaning executed if synchronous or resolved when an async/promise-returning function. You usually wouldn't want to wait for this, but you could for example catch possible errors. If any of the listeners throw/reject, the returned promise will be rejected with the error, but the other listeners will not be affected.
172
+
173
+ #### emitSerial(eventName, data?)
82
174
 
83
175
  Same as above, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer `emit()` whenever possible.
84
176
 
@@ -96,33 +188,93 @@ Returns a method to unsubscribe.
96
188
 
97
189
  Remove an `onAny` subscription.
98
190
 
191
+ #### anyEvent()
192
+
193
+ Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
194
+
195
+ Call `return()` on the iterator to remove the subscription.
196
+
197
+ ```js
198
+ const Emittery = require('emittery');
199
+
200
+ const emitter = new Emittery();
201
+ const iterator = emitter.anyEvent();
202
+
203
+ emitter.emit('🦄', '🌈1'); // Buffered
204
+ emitter.emit('🌟', '🌈2'); // Buffered
205
+
206
+ iterator.next()
207
+ .then(({value, done}) => {
208
+ // done === false
209
+ // value is ['🦄', '🌈1']
210
+ return iterator.next();
211
+ })
212
+ .then(({value, done}) => {
213
+ // done === false
214
+ // value is ['🌟', '🌈2']
215
+ // Revoke subscription
216
+ return iterator.return();
217
+ })
218
+ .then(({done}) => {
219
+ // done === true
220
+ });
221
+ ```
222
+
223
+ In the same way as for `events`, you can subscribe by using the `for await` statement
224
+
99
225
  #### clearListeners()
100
226
 
101
227
  Clear all event listeners on the instance.
102
228
 
103
229
  If `eventName` is given, only the listeners for that event are cleared.
104
230
 
105
- #### listenerCount([eventName])
231
+ #### listenerCount(eventName?)
106
232
 
107
233
  The number of listeners for the `eventName` or all events if not specified.
108
234
 
109
- ## TypeScript
235
+ #### bindMethods(target, methodNames?)
110
236
 
111
- Definition for `emittery` and `emittery/legacy` are included. Use `import Emittery = require('emittery')` or `import Emittery = require('emittery/legacy')` to load the desired implementation.
237
+ Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
112
238
 
113
- The default `Emittery` class does not let you type allowed event names and their associated data. However you can use `Emittery.Typed` with generics:
239
+ ```js
240
+ import Emittery = require('emittery');
241
+
242
+ const object = {};
243
+
244
+ new Emittery().bindMethods(object);
245
+
246
+ object.emit('event');
247
+ ```
248
+
249
+ ## TypeScript
250
+
251
+ The default `Emittery` class does not let you type allowed event names and their associated data. However, you can use `Emittery.Typed` with generics:
114
252
 
115
253
  ```ts
116
254
  import Emittery = require('emittery');
117
255
 
118
- const ee = new Emittery.Typed<{value: string}, 'open' | 'close'>();
256
+ const emitter = new Emittery.Typed<{value: string}, 'open' | 'close'>();
119
257
 
120
- ee.emit('open');
121
- ee.emit('value', 'foo\n');
122
- ee.emit('value', 1); // TS compilation error
123
- ee.emit('end'); // TS compilation error
258
+ emitter.emit('open');
259
+ emitter.emit('value', 'foo\n');
260
+ emitter.emit('value', 1); // TS compilation error
261
+ emitter.emit('end'); // TS compilation error
124
262
  ```
125
263
 
264
+ ### Emittery.mixin(emitteryPropertyName, methodNames?)
265
+
266
+ A decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the target class.
267
+
268
+ ```ts
269
+ import Emittery = require('emittery');
270
+
271
+ @Emittery.mixin('emittery')
272
+ class MyClass {}
273
+
274
+ const instance = new MyClass();
275
+
276
+ instance.emit('event');
277
+ ```
126
278
 
127
279
  ## Scheduling details
128
280
 
@@ -130,7 +282,6 @@ Listeners are not invoked for events emitted *before* the listener was added. Re
130
282
 
131
283
  Note that when using `.emitSerial()`, a slow listener will delay invocation of subsequent listeners. It's possible for newer events to overtake older ones.
132
284
 
133
-
134
285
  ## FAQ
135
286
 
136
287
  ### How is this different than the built-in `EventEmitter` in Node.js?
@@ -170,12 +321,6 @@ emitter.on('🦄', ([foo, bar]) => {
170
321
  emitter.emit('🦄', [foo, bar]);
171
322
  ```
172
323
 
173
-
174
324
  ## Related
175
325
 
176
326
  - [p-event](https://github.com/sindresorhus/p-event) - Promisify an event by waiting for it to be emitted
177
-
178
-
179
- ## License
180
-
181
- MIT © [Sindre Sorhus](https://sindresorhus.com)
package/Emittery.d.ts DELETED
@@ -1,131 +0,0 @@
1
- export = Emittery;
2
-
3
- declare class Emittery {
4
- /**
5
- * Subscribe to an event.
6
- *
7
- * Returns an unsubscribe method.
8
- *
9
- * Using the same listener multiple times for the same event will result
10
- * in only one method call per emitted event.
11
- */
12
- on(eventName: string, listener: (eventData?: any) => any): Emittery.UnsubscribeFn;
13
-
14
- /**
15
- * Remove an event subscription.
16
- */
17
- off(eventName: string, listener: (eventData?: any) => any): void;
18
-
19
- /**
20
- * Subscribe to an event only once. It will be unsubscribed after the first
21
- * event.
22
- *
23
- * Returns a promise for the event data when `eventName` is emitted.
24
- */
25
- once(eventName: string): Promise<any>;
26
-
27
- /**
28
- * Trigger an event asynchronously, optionally with some data. Listeners
29
- * are called in the order they were added, but execute concurrently.
30
- *
31
- * Returns a promise for when all the event listeners are done. *Done*
32
- * meaning executed if synchronous or resolved when an
33
- * async/promise-returning function. You usually wouldn't want to wait for
34
- * this, but you could for example catch possible errors. If any of the
35
- * listeners throw/reject, the returned promise will be rejected with the
36
- * error, but the other listeners will not be affected.
37
- *
38
- * Returns a promise for when all the event listeners are done.
39
- */
40
- emit(eventName: string, eventData?: any): Promise<void>;
41
-
42
- /**
43
- * Same as `emit()`, but it waits for each listener to resolve before
44
- * triggering the next one. This can be useful if your events depend on each
45
- * other. Although ideally they should not. Prefer `emit()` whenever
46
- * possible.
47
- *
48
- * If any of the listeners throw/reject, the returned promise will be
49
- * rejected with the error and the remaining listeners will *not* be called.
50
- *
51
- * Returns a promise for when all the event listeners are done.
52
- */
53
- emitSerial(eventName: string, eventData?: any): Promise<void>;
54
-
55
- /**
56
- * Subscribe to be notified about any event.
57
- *
58
- * Returns a method to unsubscribe.
59
- */
60
- onAny(listener: (eventName: string, eventData?: any) => any): Emittery.UnsubscribeFn;
61
-
62
- /**
63
- * Remove an `onAny` subscription.
64
- */
65
- offAny(listener: (eventName: string, eventData?: any) => any): void;
66
-
67
- /**
68
- * Clear all event listeners on the instance.
69
- *
70
- * If `eventName` is given, only the listeners for that event are cleared.
71
- */
72
- clearListeners(eventName?: string): void;
73
-
74
- /**
75
- * The number of listeners for the `eventName` or all events if not
76
- * specified.
77
- */
78
- listenerCount(eventName?: string): number;
79
- }
80
-
81
- declare namespace Emittery {
82
- /**
83
- * Removes an event subscription.
84
- */
85
- type UnsubscribeFn = () => void;
86
-
87
- /**
88
- * Maps event names to their emitted data type.
89
- */
90
- interface Events {
91
- [eventName: string]: any;
92
- }
93
-
94
- /**
95
- * Async event emitter.
96
- *
97
- * Must list supported events and the data type they emit, if any.
98
- *
99
- * For example:
100
- *
101
- * ```ts
102
- * import Emittery = require('emittery');
103
- *
104
- * const ee = new Emittery.Typed<{value: string}, 'open' | 'close'>();
105
- *
106
- * ee.emit('open');
107
- * ee.emit('value', 'foo\n');
108
- * ee.emit('value', 1); // TS compilation error
109
- * ee.emit('end'); // TS compilation error
110
- * ```
111
- */
112
- class Typed<EventDataMap extends Events, EmptyEvents extends string = never> extends Emittery {
113
- on<Name extends Extract<keyof EventDataMap, string>>(eventName: Name, listener: (eventData: EventDataMap[Name]) => any): Emittery.UnsubscribeFn;
114
- on<Name extends EmptyEvents>(eventName: Name, listener: () => any): Emittery.UnsubscribeFn;
115
-
116
- once<Name extends Extract<keyof EventDataMap, string>>(eventName: Name): Promise<EventDataMap[Name]>;
117
- once<Name extends EmptyEvents>(eventName: Name): Promise<void>;
118
-
119
- off<Name extends Extract<keyof EventDataMap, string>>(eventName: Name, listener: (eventData: EventDataMap[Name]) => any): void;
120
- off<Name extends EmptyEvents>(eventName: Name, listener: () => any): void;
121
-
122
- onAny(listener: (eventName: Extract<keyof EventDataMap, string> | EmptyEvents, eventData?: EventDataMap[Extract<keyof EventDataMap, string>]) => any): Emittery.UnsubscribeFn;
123
- offAny(listener: (eventName: Extract<keyof EventDataMap, string> | EmptyEvents, eventData?: EventDataMap[Extract<keyof EventDataMap, string>]) => any): void;
124
-
125
- emit<Name extends Extract<keyof EventDataMap, string>>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
126
- emit<Name extends EmptyEvents>(eventName: Name): Promise<void>;
127
-
128
- emitSerial<Name extends Extract<keyof EventDataMap, string>>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
129
- emitSerial<Name extends EmptyEvents>(eventName: Name): Promise<void>;
130
- }
131
- }
package/legacy.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import Emittery = require('./Emittery')
2
- export = Emittery
package/legacy.js DELETED
@@ -1,171 +0,0 @@
1
- 'use strict';
2
-
3
- function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
4
-
5
- const anyMap = new WeakMap();
6
- const eventsMap = new WeakMap();
7
- const resolvedPromise = Promise.resolve();
8
-
9
- function assertEventName(eventName) {
10
- if (typeof eventName !== 'string') {
11
- throw new TypeError('eventName must be a string');
12
- }
13
- }
14
-
15
- function assertListener(listener) {
16
- if (typeof listener !== 'function') {
17
- throw new TypeError('listener must be a function');
18
- }
19
- }
20
-
21
- function getListeners(instance, eventName) {
22
- const events = eventsMap.get(instance);
23
- if (!events.has(eventName)) {
24
- events.set(eventName, new Set());
25
- }
26
-
27
- return events.get(eventName);
28
- }
29
-
30
- class Emittery {
31
- constructor() {
32
- anyMap.set(this, new Set());
33
- eventsMap.set(this, new Map());
34
- }
35
-
36
- on(eventName, listener) {
37
- assertEventName(eventName);
38
- assertListener(listener);
39
- getListeners(this, eventName).add(listener);
40
- return this.off.bind(this, eventName, listener);
41
- }
42
-
43
- off(eventName, listener) {
44
- assertEventName(eventName);
45
- assertListener(listener);
46
- getListeners(this, eventName).delete(listener);
47
- }
48
-
49
- once(eventName) {
50
- return new Promise(resolve => {
51
- assertEventName(eventName);
52
- const off = this.on(eventName, data => {
53
- off();
54
- resolve(data);
55
- });
56
- });
57
- }
58
-
59
- emit(eventName, eventData) {
60
- var _this = this;
61
-
62
- return _asyncToGenerator(function* () {
63
- assertEventName(eventName);
64
-
65
- const listeners = getListeners(_this, eventName);
66
- const anyListeners = anyMap.get(_this);
67
- const staticListeners = [...listeners];
68
- const staticAnyListeners = [...anyListeners];
69
-
70
- yield resolvedPromise;
71
- return Promise.all([...staticListeners.map((() => {
72
- var _ref = _asyncToGenerator(function* (listener) {
73
- if (listeners.has(listener)) {
74
- return listener(eventData);
75
- }
76
- });
77
-
78
- return function (_x) {
79
- return _ref.apply(this, arguments);
80
- };
81
- })()), ...staticAnyListeners.map((() => {
82
- var _ref2 = _asyncToGenerator(function* (listener) {
83
- if (anyListeners.has(listener)) {
84
- return listener(eventName, eventData);
85
- }
86
- });
87
-
88
- return function (_x2) {
89
- return _ref2.apply(this, arguments);
90
- };
91
- })())]);
92
- })();
93
- }
94
-
95
- emitSerial(eventName, eventData) {
96
- var _this2 = this;
97
-
98
- return _asyncToGenerator(function* () {
99
- assertEventName(eventName);
100
-
101
- const listeners = getListeners(_this2, eventName);
102
- const anyListeners = anyMap.get(_this2);
103
- const staticListeners = [...listeners];
104
- const staticAnyListeners = [...anyListeners];
105
-
106
- yield resolvedPromise;
107
- /* eslint-disable no-await-in-loop */
108
- for (const listener of staticListeners) {
109
- if (listeners.has(listener)) {
110
- yield listener(eventData);
111
- }
112
- }
113
-
114
- for (const listener of staticAnyListeners) {
115
- if (anyListeners.has(listener)) {
116
- yield listener(eventName, eventData);
117
- }
118
- }
119
- /* eslint-enable no-await-in-loop */
120
- })();
121
- }
122
-
123
- onAny(listener) {
124
- assertListener(listener);
125
- anyMap.get(this).add(listener);
126
- return this.offAny.bind(this, listener);
127
- }
128
-
129
- offAny(listener) {
130
- assertListener(listener);
131
- anyMap.get(this).delete(listener);
132
- }
133
-
134
- clearListeners(eventName) {
135
- if (typeof eventName === 'string') {
136
- getListeners(this, eventName).clear();
137
- } else {
138
- anyMap.get(this).clear();
139
- for (const listeners of eventsMap.get(this).values()) {
140
- listeners.clear();
141
- }
142
- }
143
- }
144
-
145
- listenerCount(eventName) {
146
- if (typeof eventName === 'string') {
147
- return anyMap.get(this).size + getListeners(this, eventName).size;
148
- }
149
-
150
- if (typeof eventName !== 'undefined') {
151
- assertEventName(eventName);
152
- }
153
-
154
- let count = anyMap.get(this).size;
155
-
156
- for (const value of eventsMap.get(this).values()) {
157
- count += value.size;
158
- }
159
-
160
- return count;
161
- }
162
- }
163
-
164
- // Subclass used to encourage TS users to type their events.
165
- Emittery.Typed = class extends Emittery {};
166
- Object.defineProperty(Emittery.Typed, 'Typed', {
167
- enumerable: false,
168
- value: undefined
169
- });
170
-
171
- module.exports = Emittery;