emittery 0.3.0 → 0.5.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 +244 -0
- package/index.js +206 -1
- package/package.json +15 -30
- package/readme.md +134 -22
- package/Emittery.d.ts +0 -131
- package/legacy.d.ts +0 -2
- package/legacy.js +0 -173
package/index.d.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
declare class Emittery {
|
|
2
|
+
/**
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
@example
|
|
6
|
+
```
|
|
7
|
+
import Emittery = require('emittery');
|
|
8
|
+
|
|
9
|
+
@Emittery.mixin('emittery')
|
|
10
|
+
class MyClass {}
|
|
11
|
+
|
|
12
|
+
const instance = new MyClass();
|
|
13
|
+
|
|
14
|
+
instance.emit('event');
|
|
15
|
+
```
|
|
16
|
+
*/
|
|
17
|
+
static mixin(emitteryPropertyName: string, methodNames?: readonly string[]): Function;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
Subscribe to an event.
|
|
21
|
+
|
|
22
|
+
Using the same listener multiple times for the same event will result in only one method call per emitted event.
|
|
23
|
+
|
|
24
|
+
@returns An unsubscribe method.
|
|
25
|
+
*/
|
|
26
|
+
on(eventName: string, listener: (eventData?: unknown) => void): Emittery.UnsubscribeFn;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
Get an async iterator which buffers data each time an event is emitted.
|
|
30
|
+
|
|
31
|
+
Call `return()` on the iterator to remove the subscription.
|
|
32
|
+
|
|
33
|
+
@example
|
|
34
|
+
```
|
|
35
|
+
import Emittery = require('emittery');
|
|
36
|
+
|
|
37
|
+
const emitter = new Emittery();
|
|
38
|
+
const iterator = emitter.events('🦄');
|
|
39
|
+
|
|
40
|
+
emitter.emit('🦄', '🌈1'); // Buffered
|
|
41
|
+
emitter.emit('🦄', '🌈2'); // Buffered
|
|
42
|
+
|
|
43
|
+
iterator
|
|
44
|
+
.next()
|
|
45
|
+
.then(({value, done}) => {
|
|
46
|
+
// done === false
|
|
47
|
+
// value === '🌈1'
|
|
48
|
+
return iterator.next();
|
|
49
|
+
})
|
|
50
|
+
.then(({value, done}) => {
|
|
51
|
+
// done === false
|
|
52
|
+
// value === '🌈2'
|
|
53
|
+
// Revoke subscription
|
|
54
|
+
return iterator.return();
|
|
55
|
+
})
|
|
56
|
+
.then(({done}) => {
|
|
57
|
+
// done === true
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
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.
|
|
62
|
+
|
|
63
|
+
@example
|
|
64
|
+
```
|
|
65
|
+
import Emittery = require('emittery');
|
|
66
|
+
|
|
67
|
+
const emitter = new Emittery();
|
|
68
|
+
const iterator = emitter.events('🦄');
|
|
69
|
+
|
|
70
|
+
emitter.emit('🦄', '🌈1'); // Buffered
|
|
71
|
+
emitter.emit('🦄', '🌈2'); // Buffered
|
|
72
|
+
|
|
73
|
+
// In an async context.
|
|
74
|
+
for await (const data of iterator) {
|
|
75
|
+
if (data === '🌈2') {
|
|
76
|
+
break; // Revoke the subscription when we see the value `🌈2`.
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
*/
|
|
81
|
+
events(eventName:string): AsyncIterableIterator<unknown>
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
Remove an event subscription.
|
|
85
|
+
*/
|
|
86
|
+
off(eventName: string, listener: (eventData?: unknown) => void): void;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
Subscribe to an event only once. It will be unsubscribed after the first
|
|
90
|
+
event.
|
|
91
|
+
|
|
92
|
+
@returns The event data when `eventName` is emitted.
|
|
93
|
+
*/
|
|
94
|
+
once(eventName: string): Promise<unknown>;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
|
|
98
|
+
|
|
99
|
+
@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.
|
|
100
|
+
*/
|
|
101
|
+
emit(eventName: string, eventData?: unknown): Promise<void>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
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.
|
|
105
|
+
|
|
106
|
+
If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will *not* be called.
|
|
107
|
+
|
|
108
|
+
@returns A promise that resolves when all the event listeners are done.
|
|
109
|
+
*/
|
|
110
|
+
emitSerial(eventName: string, eventData?: unknown): Promise<void>;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
Subscribe to be notified about any event.
|
|
114
|
+
|
|
115
|
+
@returns A method to unsubscribe.
|
|
116
|
+
*/
|
|
117
|
+
onAny(listener: (eventName: string, eventData?: unknown) => unknown): Emittery.UnsubscribeFn;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
|
|
121
|
+
|
|
122
|
+
Call `return()` on the iterator to remove the subscription.
|
|
123
|
+
|
|
124
|
+
In the same way as for `events`, you can subscribe by using the `for await` statement.
|
|
125
|
+
|
|
126
|
+
@example
|
|
127
|
+
```
|
|
128
|
+
import Emittery = require('emittery');
|
|
129
|
+
|
|
130
|
+
const emitter = new Emittery();
|
|
131
|
+
const iterator = emitter.anyEvent();
|
|
132
|
+
|
|
133
|
+
emitter.emit('🦄', '🌈1'); // Buffered
|
|
134
|
+
emitter.emit('🌟', '🌈2'); // Buffered
|
|
135
|
+
|
|
136
|
+
iterator.next()
|
|
137
|
+
.then(({value, done}) => {
|
|
138
|
+
// done is false
|
|
139
|
+
// value is ['🦄', '🌈1']
|
|
140
|
+
return iterator.next();
|
|
141
|
+
})
|
|
142
|
+
.then(({value, done}) => {
|
|
143
|
+
// done is false
|
|
144
|
+
// value is ['🌟', '🌈2']
|
|
145
|
+
// revoke subscription
|
|
146
|
+
return iterator.return();
|
|
147
|
+
})
|
|
148
|
+
.then(({done}) => {
|
|
149
|
+
// done is true
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
*/
|
|
153
|
+
anyEvent(): AsyncIterableIterator<unknown>
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
Remove an `onAny` subscription.
|
|
157
|
+
*/
|
|
158
|
+
offAny(listener: (eventName: string, eventData?: unknown) => void): void;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
Clear all event listeners on the instance.
|
|
162
|
+
|
|
163
|
+
If `eventName` is given, only the listeners for that event are cleared.
|
|
164
|
+
*/
|
|
165
|
+
clearListeners(eventName?: string): void;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
The number of listeners for the `eventName` or all events if not specified.
|
|
169
|
+
*/
|
|
170
|
+
listenerCount(eventName?: string): number;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
|
|
174
|
+
|
|
175
|
+
@example
|
|
176
|
+
```
|
|
177
|
+
import Emittery = require('emittery');
|
|
178
|
+
|
|
179
|
+
const object = {};
|
|
180
|
+
|
|
181
|
+
new Emittery().bindMethods(object);
|
|
182
|
+
|
|
183
|
+
object.emit('event');
|
|
184
|
+
```
|
|
185
|
+
*/
|
|
186
|
+
bindMethods(target: object, methodNames?: readonly string[]): void;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
declare namespace Emittery {
|
|
190
|
+
/**
|
|
191
|
+
Removes an event subscription.
|
|
192
|
+
*/
|
|
193
|
+
type UnsubscribeFn = () => void;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
Maps event names to their emitted data type.
|
|
197
|
+
*/
|
|
198
|
+
interface Events {
|
|
199
|
+
[eventName: string]: any;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
Async event emitter.
|
|
204
|
+
|
|
205
|
+
You must list supported events and the data type they emit, if any.
|
|
206
|
+
|
|
207
|
+
@example
|
|
208
|
+
```
|
|
209
|
+
import Emittery = require('emittery');
|
|
210
|
+
|
|
211
|
+
const emitter = new Emittery.Typed<{value: string}, 'open' | 'close'>();
|
|
212
|
+
|
|
213
|
+
emitter.emit('open');
|
|
214
|
+
emitter.emit('value', 'foo\n');
|
|
215
|
+
emitter.emit('value', 1); // TS compilation error
|
|
216
|
+
emitter.emit('end'); // TS compilation error
|
|
217
|
+
```
|
|
218
|
+
*/
|
|
219
|
+
class Typed<EventDataMap extends Events, EmptyEvents extends string = never> extends Emittery {
|
|
220
|
+
on<Name extends Extract<keyof EventDataMap, string>>(eventName: Name, listener: (eventData: EventDataMap[Name]) => void): Emittery.UnsubscribeFn;
|
|
221
|
+
on<Name extends EmptyEvents>(eventName: Name, listener: () => void): Emittery.UnsubscribeFn;
|
|
222
|
+
|
|
223
|
+
events<Name extends Extract<keyof EventDataMap, string>>(eventName: Name): AsyncIterableIterator<EventDataMap[Name]>;
|
|
224
|
+
|
|
225
|
+
once<Name extends Extract<keyof EventDataMap, string>>(eventName: Name): Promise<EventDataMap[Name]>;
|
|
226
|
+
once<Name extends EmptyEvents>(eventName: Name): Promise<void>;
|
|
227
|
+
|
|
228
|
+
off<Name extends Extract<keyof EventDataMap, string>>(eventName: Name, listener: (eventData: EventDataMap[Name]) => void): void;
|
|
229
|
+
off<Name extends EmptyEvents>(eventName: Name, listener: () => void): void;
|
|
230
|
+
|
|
231
|
+
onAny(listener: (eventName: Extract<keyof EventDataMap, string> | EmptyEvents, eventData?: EventDataMap[Extract<keyof EventDataMap, string>]) => void): Emittery.UnsubscribeFn;
|
|
232
|
+
anyEvent(): AsyncIterableIterator<[Extract<keyof EventDataMap, string>, EventDataMap[Extract<keyof EventDataMap, string>]]>;
|
|
233
|
+
|
|
234
|
+
offAny(listener: (eventName: Extract<keyof EventDataMap, string> | EmptyEvents, eventData?: EventDataMap[Extract<keyof EventDataMap, string>]) => void): void;
|
|
235
|
+
|
|
236
|
+
emit<Name extends Extract<keyof EventDataMap, string>>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
|
|
237
|
+
emit<Name extends EmptyEvents>(eventName: Name): Promise<void>;
|
|
238
|
+
|
|
239
|
+
emitSerial<Name extends Extract<keyof EventDataMap, string>>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
|
|
240
|
+
emitSerial<Name extends EmptyEvents>(eventName: Name): Promise<void>;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export = Emittery;
|
package/index.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
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
|
|
|
7
9
|
function assertEventName(eventName) {
|
|
@@ -25,10 +27,159 @@ function getListeners(instance, eventName) {
|
|
|
25
27
|
return events.get(eventName);
|
|
26
28
|
}
|
|
27
29
|
|
|
30
|
+
function getEventProducers(instance, eventName) {
|
|
31
|
+
const key = typeof eventName === 'string' ? eventName : anyProducer;
|
|
32
|
+
const producers = producersMap.get(instance);
|
|
33
|
+
if (!producers.has(key)) {
|
|
34
|
+
producers.set(key, new Set());
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return producers.get(key);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function enqueueProducers(instance, eventName, eventData) {
|
|
41
|
+
const producers = producersMap.get(instance);
|
|
42
|
+
if (producers.has(eventName)) {
|
|
43
|
+
for (const producer of producers.get(eventName)) {
|
|
44
|
+
producer.enqueue(eventData);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (producers.has(anyProducer)) {
|
|
49
|
+
const item = Promise.all([eventName, eventData]);
|
|
50
|
+
for (const producer of producers.get(anyProducer)) {
|
|
51
|
+
producer.enqueue(item);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function iterator(instance, eventName) {
|
|
57
|
+
let isFinished = false;
|
|
58
|
+
let flush = () => {};
|
|
59
|
+
let queue = [];
|
|
60
|
+
|
|
61
|
+
const producer = {
|
|
62
|
+
enqueue(item) {
|
|
63
|
+
queue.push(item);
|
|
64
|
+
flush();
|
|
65
|
+
},
|
|
66
|
+
finish() {
|
|
67
|
+
isFinished = true;
|
|
68
|
+
flush();
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
getEventProducers(instance, eventName).add(producer);
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
async next() {
|
|
76
|
+
if (!queue) {
|
|
77
|
+
return {done: true};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (queue.length === 0) {
|
|
81
|
+
if (isFinished) {
|
|
82
|
+
queue = undefined;
|
|
83
|
+
return this.next();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
await new Promise(resolve => {
|
|
87
|
+
flush = resolve;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return this.next();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
done: false,
|
|
95
|
+
value: await queue.shift()
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
async return(value) {
|
|
100
|
+
queue = undefined;
|
|
101
|
+
getEventProducers(instance, eventName).delete(producer);
|
|
102
|
+
flush();
|
|
103
|
+
|
|
104
|
+
return arguments.length > 0 ?
|
|
105
|
+
{done: true, value: await value} :
|
|
106
|
+
{done: true};
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
[Symbol.asyncIterator]() {
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function defaultMethodNamesOrAssert(methodNames) {
|
|
116
|
+
if (methodNames === undefined) {
|
|
117
|
+
return allEmitteryMethods;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (!Array.isArray(methodNames)) {
|
|
121
|
+
throw new TypeError('`methodNames` must be an array of strings');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
for (const methodName of methodNames) {
|
|
125
|
+
if (!allEmitteryMethods.includes(methodName)) {
|
|
126
|
+
if (typeof methodName !== 'string') {
|
|
127
|
+
throw new TypeError('`methodNames` element must be a string');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
throw new Error(`${methodName} is not Emittery method`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return methodNames;
|
|
135
|
+
}
|
|
136
|
+
|
|
28
137
|
class Emittery {
|
|
138
|
+
static mixin(emitteryPropertyName, methodNames) {
|
|
139
|
+
methodNames = defaultMethodNamesOrAssert(methodNames);
|
|
140
|
+
return target => {
|
|
141
|
+
if (typeof target !== 'function') {
|
|
142
|
+
throw new TypeError('`target` must be function');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
for (const methodName of methodNames) {
|
|
146
|
+
if (target.prototype[methodName] !== undefined) {
|
|
147
|
+
throw new Error(`The property \`${methodName}\` already exists on \`target\``);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function getEmitteryProperty() {
|
|
152
|
+
Object.defineProperty(this, emitteryPropertyName, {
|
|
153
|
+
enumerable: false,
|
|
154
|
+
value: new Emittery()
|
|
155
|
+
});
|
|
156
|
+
return this[emitteryPropertyName];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
Object.defineProperty(target.prototype, emitteryPropertyName, {
|
|
160
|
+
enumerable: false,
|
|
161
|
+
get: getEmitteryProperty
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const emitteryMethodCaller = methodName => function (...args) {
|
|
165
|
+
return this[emitteryPropertyName][methodName](...args);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
for (const methodName of methodNames) {
|
|
169
|
+
Object.defineProperty(target.prototype, methodName, {
|
|
170
|
+
enumerable: false,
|
|
171
|
+
value: emitteryMethodCaller(methodName)
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return target;
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
29
179
|
constructor() {
|
|
30
180
|
anyMap.set(this, new Set());
|
|
31
181
|
eventsMap.set(this, new Map());
|
|
182
|
+
producersMap.set(this, new Map());
|
|
32
183
|
}
|
|
33
184
|
|
|
34
185
|
on(eventName, listener) {
|
|
@@ -54,9 +205,16 @@ class Emittery {
|
|
|
54
205
|
});
|
|
55
206
|
}
|
|
56
207
|
|
|
208
|
+
events(eventName) {
|
|
209
|
+
assertEventName(eventName);
|
|
210
|
+
return iterator(this, eventName);
|
|
211
|
+
}
|
|
212
|
+
|
|
57
213
|
async emit(eventName, eventData) {
|
|
58
214
|
assertEventName(eventName);
|
|
59
215
|
|
|
216
|
+
enqueueProducers(this, eventName, eventData);
|
|
217
|
+
|
|
60
218
|
const listeners = getListeners(this, eventName);
|
|
61
219
|
const anyListeners = anyMap.get(this);
|
|
62
220
|
const staticListeners = [...listeners];
|
|
@@ -107,6 +265,10 @@ class Emittery {
|
|
|
107
265
|
return this.offAny.bind(this, listener);
|
|
108
266
|
}
|
|
109
267
|
|
|
268
|
+
anyEvent() {
|
|
269
|
+
return iterator(this);
|
|
270
|
+
}
|
|
271
|
+
|
|
110
272
|
offAny(listener) {
|
|
111
273
|
assertListener(listener);
|
|
112
274
|
anyMap.get(this).delete(listener);
|
|
@@ -115,17 +277,35 @@ class Emittery {
|
|
|
115
277
|
clearListeners(eventName) {
|
|
116
278
|
if (typeof eventName === 'string') {
|
|
117
279
|
getListeners(this, eventName).clear();
|
|
280
|
+
|
|
281
|
+
const producers = getEventProducers(this, eventName);
|
|
282
|
+
|
|
283
|
+
for (const producer of producers) {
|
|
284
|
+
producer.finish();
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
producers.clear();
|
|
118
288
|
} else {
|
|
119
289
|
anyMap.get(this).clear();
|
|
290
|
+
|
|
120
291
|
for (const listeners of eventsMap.get(this).values()) {
|
|
121
292
|
listeners.clear();
|
|
122
293
|
}
|
|
294
|
+
|
|
295
|
+
for (const producers of producersMap.get(this).values()) {
|
|
296
|
+
for (const producer of producers) {
|
|
297
|
+
producer.finish();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
producers.clear();
|
|
301
|
+
}
|
|
123
302
|
}
|
|
124
303
|
}
|
|
125
304
|
|
|
126
305
|
listenerCount(eventName) {
|
|
127
306
|
if (typeof eventName === 'string') {
|
|
128
|
-
return anyMap.get(this).size + getListeners(this, eventName).size
|
|
307
|
+
return anyMap.get(this).size + getListeners(this, eventName).size +
|
|
308
|
+
getEventProducers(this, eventName).size + getEventProducers(this).size;
|
|
129
309
|
}
|
|
130
310
|
|
|
131
311
|
if (typeof eventName !== 'undefined') {
|
|
@@ -138,10 +318,35 @@ class Emittery {
|
|
|
138
318
|
count += value.size;
|
|
139
319
|
}
|
|
140
320
|
|
|
321
|
+
for (const value of producersMap.get(this).values()) {
|
|
322
|
+
count += value.size;
|
|
323
|
+
}
|
|
324
|
+
|
|
141
325
|
return count;
|
|
142
326
|
}
|
|
327
|
+
|
|
328
|
+
bindMethods(target, methodNames) {
|
|
329
|
+
if (typeof target !== 'object' || target === null) {
|
|
330
|
+
throw new TypeError('`target` must be an object');
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
methodNames = defaultMethodNamesOrAssert(methodNames);
|
|
334
|
+
|
|
335
|
+
for (const methodName of methodNames) {
|
|
336
|
+
if (target[methodName] !== undefined) {
|
|
337
|
+
throw new Error(`The property \`${methodName}\` already exists on \`target\``);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
Object.defineProperty(target, methodName, {
|
|
341
|
+
enumerable: false,
|
|
342
|
+
value: this[methodName].bind(this)
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}
|
|
143
346
|
}
|
|
144
347
|
|
|
348
|
+
const allEmitteryMethods = Object.getOwnPropertyNames(Emittery.prototype).filter(v => v !== 'constructor');
|
|
349
|
+
|
|
145
350
|
// Subclass used to encourage TS users to type their events.
|
|
146
351
|
Emittery.Typed = class extends Emittery {};
|
|
147
352
|
Object.defineProperty(Emittery.Typed, 'Typed', {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emittery",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Simple and modern async event emitter",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/emittery",
|
|
@@ -10,21 +10,15 @@
|
|
|
10
10
|
"url": "sindresorhus.com"
|
|
11
11
|
},
|
|
12
12
|
"engines": {
|
|
13
|
-
"node": ">=
|
|
13
|
+
"node": ">=8"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"
|
|
17
|
-
"build:watch": "npm run build -- --watch",
|
|
18
|
-
"prepare": "npm run build",
|
|
19
|
-
"test": "xo && tsc --noEmit && nyc ava"
|
|
16
|
+
"test": "xo && nyc ava && tsd"
|
|
20
17
|
},
|
|
21
18
|
"files": [
|
|
22
|
-
"Emittery.d.ts",
|
|
23
19
|
"index.js",
|
|
24
|
-
"
|
|
25
|
-
"legacy.d.ts"
|
|
20
|
+
"index.d.ts"
|
|
26
21
|
],
|
|
27
|
-
"typings": "./Emittery.d.ts",
|
|
28
22
|
"keywords": [
|
|
29
23
|
"event",
|
|
30
24
|
"emitter",
|
|
@@ -47,28 +41,19 @@
|
|
|
47
41
|
"observer",
|
|
48
42
|
"trigger",
|
|
49
43
|
"await",
|
|
50
|
-
"promise"
|
|
44
|
+
"promise",
|
|
45
|
+
"typescript",
|
|
46
|
+
"ts",
|
|
47
|
+
"typed"
|
|
51
48
|
],
|
|
52
49
|
"devDependencies": {
|
|
53
|
-
"@types/node": "^
|
|
54
|
-
"ava": "
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"delay": "^2.0.0",
|
|
61
|
-
"glob": "^7.1.2",
|
|
62
|
-
"nyc": "^11.3.0",
|
|
63
|
-
"ts-node": "^4.1.0",
|
|
64
|
-
"typescript": "^2.7.0",
|
|
65
|
-
"xo": "*"
|
|
66
|
-
},
|
|
67
|
-
"babel": {
|
|
68
|
-
"plugins": [
|
|
69
|
-
"transform-async-to-generator",
|
|
70
|
-
"transform-es2015-spread"
|
|
71
|
-
]
|
|
50
|
+
"@types/node": "^12.7.5",
|
|
51
|
+
"ava": "^2.4.0",
|
|
52
|
+
"codecov": "^3.1.0",
|
|
53
|
+
"delay": "^4.3.0",
|
|
54
|
+
"nyc": "^14.1.1",
|
|
55
|
+
"tsd": "^0.7.4",
|
|
56
|
+
"xo": "^0.24.0"
|
|
72
57
|
},
|
|
73
58
|
"nyc": {
|
|
74
59
|
"reporter": [
|
package/readme.md
CHANGED
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
|
|
7
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
8
|
|
|
9
|
-
|
|
9
|
+
It 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. 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.
|
|
10
12
|
|
|
11
13
|
|
|
12
14
|
## Install
|
|
@@ -20,6 +22,7 @@ $ npm install emittery
|
|
|
20
22
|
|
|
21
23
|
```js
|
|
22
24
|
const Emittery = require('emittery');
|
|
25
|
+
|
|
23
26
|
const emitter = new Emittery();
|
|
24
27
|
|
|
25
28
|
emitter.on('🦄', data => {
|
|
@@ -30,10 +33,6 @@ emitter.on('🦄', data => {
|
|
|
30
33
|
emitter.emit('🦄', '🌈');
|
|
31
34
|
```
|
|
32
35
|
|
|
33
|
-
### Node.js 4 and 6
|
|
34
|
-
|
|
35
|
-
The above only works in Node.js 8 or newer. For older Node.js versions you can use `require('emittery/legacy')`.
|
|
36
|
-
|
|
37
36
|
|
|
38
37
|
## API
|
|
39
38
|
|
|
@@ -62,6 +61,10 @@ Subscribe to an event only once. It will be unsubscribed after the first event.
|
|
|
62
61
|
Returns a promise for the event data when `eventName` is emitted.
|
|
63
62
|
|
|
64
63
|
```js
|
|
64
|
+
const Emittery = require('emittery');
|
|
65
|
+
|
|
66
|
+
const emitter = new Emittery();
|
|
67
|
+
|
|
65
68
|
emitter.once('🦄').then(data => {
|
|
66
69
|
console.log(data);
|
|
67
70
|
//=> '🌈'
|
|
@@ -70,13 +73,65 @@ emitter.once('🦄').then(data => {
|
|
|
70
73
|
emitter.emit('🦄', '🌈');
|
|
71
74
|
```
|
|
72
75
|
|
|
73
|
-
####
|
|
76
|
+
#### events(eventName)
|
|
74
77
|
|
|
75
|
-
|
|
78
|
+
Get an async iterator which buffers data each time an event is emitted.
|
|
76
79
|
|
|
77
|
-
|
|
80
|
+
Call `return()` on the iterator to remove the subscription.
|
|
78
81
|
|
|
79
|
-
|
|
82
|
+
```js
|
|
83
|
+
const Emittery = require('emittery');
|
|
84
|
+
|
|
85
|
+
const emitter = new Emittery();
|
|
86
|
+
const iterator = emitter.events('🦄');
|
|
87
|
+
|
|
88
|
+
emitter.emit('🦄', '🌈1'); // Buffered
|
|
89
|
+
emitter.emit('🦄', '🌈2'); // Buffered
|
|
90
|
+
|
|
91
|
+
iterator
|
|
92
|
+
.next()
|
|
93
|
+
.then(({value, done}) => {
|
|
94
|
+
// done === false
|
|
95
|
+
// value === '🌈1'
|
|
96
|
+
return iterator.next();
|
|
97
|
+
})
|
|
98
|
+
.then(({value, done}) => {
|
|
99
|
+
// done === false
|
|
100
|
+
// value === '🌈2'
|
|
101
|
+
// Revoke subscription
|
|
102
|
+
return iterator.return();
|
|
103
|
+
})
|
|
104
|
+
.then(({done}) => {
|
|
105
|
+
// done === true
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
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.
|
|
110
|
+
|
|
111
|
+
```js
|
|
112
|
+
const Emittery = require('emittery');
|
|
113
|
+
|
|
114
|
+
const emitter = new Emittery();
|
|
115
|
+
const iterator = emitter.events('🦄');
|
|
116
|
+
|
|
117
|
+
emitter.emit('🦄', '🌈1'); // Buffered
|
|
118
|
+
emitter.emit('🦄', '🌈2'); // Buffered
|
|
119
|
+
|
|
120
|
+
// In an async context.
|
|
121
|
+
for await (const data of iterator) {
|
|
122
|
+
if (data === '🌈2') {
|
|
123
|
+
break; // Revoke the subscription when we see the value '🌈2'.
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### emit(eventName, data?)
|
|
129
|
+
|
|
130
|
+
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
|
|
131
|
+
|
|
132
|
+
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.
|
|
133
|
+
|
|
134
|
+
#### emitSerial(eventName, data?)
|
|
80
135
|
|
|
81
136
|
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.
|
|
82
137
|
|
|
@@ -94,31 +149,93 @@ Returns a method to unsubscribe.
|
|
|
94
149
|
|
|
95
150
|
Remove an `onAny` subscription.
|
|
96
151
|
|
|
152
|
+
#### anyEvent()
|
|
153
|
+
|
|
154
|
+
Get an async iterator which buffers a tuple of an event name and data each time an event is emitted.
|
|
155
|
+
|
|
156
|
+
Call `return()` on the iterator to remove the subscription.
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
const Emittery = require('emittery');
|
|
160
|
+
|
|
161
|
+
const emitter = new Emittery();
|
|
162
|
+
const iterator = emitter.anyEvent();
|
|
163
|
+
|
|
164
|
+
emitter.emit('🦄', '🌈1'); // Buffered
|
|
165
|
+
emitter.emit('🌟', '🌈2'); // Buffered
|
|
166
|
+
|
|
167
|
+
iterator.next()
|
|
168
|
+
.then(({value, done}) => {
|
|
169
|
+
// done === false
|
|
170
|
+
// value is ['🦄', '🌈1']
|
|
171
|
+
return iterator.next();
|
|
172
|
+
})
|
|
173
|
+
.then(({value, done}) => {
|
|
174
|
+
// done === false
|
|
175
|
+
// value is ['🌟', '🌈2']
|
|
176
|
+
// Revoke subscription
|
|
177
|
+
return iterator.return();
|
|
178
|
+
})
|
|
179
|
+
.then(({done}) => {
|
|
180
|
+
// done === true
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
In the same way as for `events`, you can subscribe by using the `for await` statement
|
|
185
|
+
|
|
97
186
|
#### clearListeners()
|
|
98
187
|
|
|
99
188
|
Clear all event listeners on the instance.
|
|
100
189
|
|
|
101
190
|
If `eventName` is given, only the listeners for that event are cleared.
|
|
102
191
|
|
|
103
|
-
#### listenerCount(
|
|
192
|
+
#### listenerCount(eventName?)
|
|
104
193
|
|
|
105
194
|
The number of listeners for the `eventName` or all events if not specified.
|
|
106
195
|
|
|
196
|
+
#### bindMethods(target, methodNames?)
|
|
197
|
+
|
|
198
|
+
Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
|
|
199
|
+
|
|
200
|
+
```js
|
|
201
|
+
import Emittery = require('emittery');
|
|
202
|
+
|
|
203
|
+
const object = {};
|
|
204
|
+
|
|
205
|
+
new Emittery().bindMethods(object);
|
|
206
|
+
|
|
207
|
+
object.emit('event');
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
|
|
107
211
|
## TypeScript
|
|
108
212
|
|
|
109
|
-
|
|
213
|
+
The default `Emittery` class does not let you type allowed event names and their associated data. However, you can use `Emittery.Typed` with generics:
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import Emittery = require('emittery');
|
|
217
|
+
|
|
218
|
+
const emitter = new Emittery.Typed<{value: string}, 'open' | 'close'>();
|
|
110
219
|
|
|
111
|
-
|
|
220
|
+
emitter.emit('open');
|
|
221
|
+
emitter.emit('value', 'foo\n');
|
|
222
|
+
emitter.emit('value', 1); // TS compilation error
|
|
223
|
+
emitter.emit('end'); // TS compilation error
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Emittery.mixin(emitteryPropertyName, methodNames?)
|
|
227
|
+
|
|
228
|
+
A decorator which mixins `Emittery` as property `emitteryPropertyName` and `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the target class.
|
|
112
229
|
|
|
113
230
|
```ts
|
|
114
231
|
import Emittery = require('emittery');
|
|
115
232
|
|
|
116
|
-
|
|
233
|
+
@Emittery.mixin('emittery')
|
|
234
|
+
class MyClass {}
|
|
235
|
+
|
|
236
|
+
const instance = new MyClass();
|
|
117
237
|
|
|
118
|
-
|
|
119
|
-
ee.emit('value', 'foo\n');
|
|
120
|
-
ee.emit('value', 1); // TS compilation error
|
|
121
|
-
ee.emit('end'); // TS compilation error
|
|
238
|
+
instance.emit('event');
|
|
122
239
|
```
|
|
123
240
|
|
|
124
241
|
|
|
@@ -172,8 +289,3 @@ emitter.emit('🦄', [foo, bar]);
|
|
|
172
289
|
## Related
|
|
173
290
|
|
|
174
291
|
- [p-event](https://github.com/sindresorhus/p-event) - Promisify an event by waiting for it to be emitted
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
## License
|
|
178
|
-
|
|
179
|
-
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 keyof EventDataMap>(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 keyof EventDataMap>(eventName: Name): Promise<EventDataMap[Name]>;
|
|
117
|
-
once<Name extends EmptyEvents>(eventName: Name): Promise<void>;
|
|
118
|
-
|
|
119
|
-
off<Name extends keyof EventDataMap>(eventName: Name, listener: (eventData: EventDataMap[Name]) => any): void;
|
|
120
|
-
off<Name extends EmptyEvents>(eventName: Name, listener: () => any): void;
|
|
121
|
-
|
|
122
|
-
onAny(listener: (eventName: keyof EventDataMap | EmptyEvents, eventData?: EventDataMap[keyof EventDataMap]) => any): Emittery.UnsubscribeFn;
|
|
123
|
-
offAny(listener: (eventName: keyof EventDataMap | EmptyEvents, eventData?: EventDataMap[keyof EventDataMap]) => any): void;
|
|
124
|
-
|
|
125
|
-
emit<Name extends keyof EventDataMap>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
|
|
126
|
-
emit<Name extends EmptyEvents>(eventName: Name): Promise<void>;
|
|
127
|
-
|
|
128
|
-
emitSerial<Name extends keyof EventDataMap>(eventName: Name, eventData: EventDataMap[Name]): Promise<void>;
|
|
129
|
-
emitSerial<Name extends EmptyEvents>(eventName: Name): Promise<void>;
|
|
130
|
-
}
|
|
131
|
-
}
|
package/legacy.d.ts
DELETED
package/legacy.js
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
|
|
4
|
-
|
|
5
|
-
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"); }); }; }
|
|
6
|
-
|
|
7
|
-
const anyMap = new WeakMap();
|
|
8
|
-
const eventsMap = new WeakMap();
|
|
9
|
-
const resolvedPromise = Promise.resolve();
|
|
10
|
-
|
|
11
|
-
function assertEventName(eventName) {
|
|
12
|
-
if (typeof eventName !== 'string') {
|
|
13
|
-
throw new TypeError('eventName must be a string');
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function assertListener(listener) {
|
|
18
|
-
if (typeof listener !== 'function') {
|
|
19
|
-
throw new TypeError('listener must be a function');
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function getListeners(instance, eventName) {
|
|
24
|
-
const events = eventsMap.get(instance);
|
|
25
|
-
if (!events.has(eventName)) {
|
|
26
|
-
events.set(eventName, new Set());
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return events.get(eventName);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
class Emittery {
|
|
33
|
-
constructor() {
|
|
34
|
-
anyMap.set(this, new Set());
|
|
35
|
-
eventsMap.set(this, new Map());
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
on(eventName, listener) {
|
|
39
|
-
assertEventName(eventName);
|
|
40
|
-
assertListener(listener);
|
|
41
|
-
getListeners(this, eventName).add(listener);
|
|
42
|
-
return this.off.bind(this, eventName, listener);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
off(eventName, listener) {
|
|
46
|
-
assertEventName(eventName);
|
|
47
|
-
assertListener(listener);
|
|
48
|
-
getListeners(this, eventName).delete(listener);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
once(eventName) {
|
|
52
|
-
return new Promise(resolve => {
|
|
53
|
-
assertEventName(eventName);
|
|
54
|
-
const off = this.on(eventName, data => {
|
|
55
|
-
off();
|
|
56
|
-
resolve(data);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
emit(eventName, eventData) {
|
|
62
|
-
var _this = this;
|
|
63
|
-
|
|
64
|
-
return _asyncToGenerator(function* () {
|
|
65
|
-
assertEventName(eventName);
|
|
66
|
-
|
|
67
|
-
const listeners = getListeners(_this, eventName);
|
|
68
|
-
const anyListeners = anyMap.get(_this);
|
|
69
|
-
const staticListeners = [].concat(_toConsumableArray(listeners));
|
|
70
|
-
const staticAnyListeners = [].concat(_toConsumableArray(anyListeners));
|
|
71
|
-
|
|
72
|
-
yield resolvedPromise;
|
|
73
|
-
return Promise.all([].concat(_toConsumableArray(staticListeners.map((() => {
|
|
74
|
-
var _ref = _asyncToGenerator(function* (listener) {
|
|
75
|
-
if (listeners.has(listener)) {
|
|
76
|
-
return listener(eventData);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
return function (_x) {
|
|
81
|
-
return _ref.apply(this, arguments);
|
|
82
|
-
};
|
|
83
|
-
})())), _toConsumableArray(staticAnyListeners.map((() => {
|
|
84
|
-
var _ref2 = _asyncToGenerator(function* (listener) {
|
|
85
|
-
if (anyListeners.has(listener)) {
|
|
86
|
-
return listener(eventName, eventData);
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
return function (_x2) {
|
|
91
|
-
return _ref2.apply(this, arguments);
|
|
92
|
-
};
|
|
93
|
-
})()))));
|
|
94
|
-
})();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
emitSerial(eventName, eventData) {
|
|
98
|
-
var _this2 = this;
|
|
99
|
-
|
|
100
|
-
return _asyncToGenerator(function* () {
|
|
101
|
-
assertEventName(eventName);
|
|
102
|
-
|
|
103
|
-
const listeners = getListeners(_this2, eventName);
|
|
104
|
-
const anyListeners = anyMap.get(_this2);
|
|
105
|
-
const staticListeners = [].concat(_toConsumableArray(listeners));
|
|
106
|
-
const staticAnyListeners = [].concat(_toConsumableArray(anyListeners));
|
|
107
|
-
|
|
108
|
-
yield resolvedPromise;
|
|
109
|
-
/* eslint-disable no-await-in-loop */
|
|
110
|
-
for (const listener of staticListeners) {
|
|
111
|
-
if (listeners.has(listener)) {
|
|
112
|
-
yield listener(eventData);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
for (const listener of staticAnyListeners) {
|
|
117
|
-
if (anyListeners.has(listener)) {
|
|
118
|
-
yield listener(eventName, eventData);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
/* eslint-enable no-await-in-loop */
|
|
122
|
-
})();
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
onAny(listener) {
|
|
126
|
-
assertListener(listener);
|
|
127
|
-
anyMap.get(this).add(listener);
|
|
128
|
-
return this.offAny.bind(this, listener);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
offAny(listener) {
|
|
132
|
-
assertListener(listener);
|
|
133
|
-
anyMap.get(this).delete(listener);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
clearListeners(eventName) {
|
|
137
|
-
if (typeof eventName === 'string') {
|
|
138
|
-
getListeners(this, eventName).clear();
|
|
139
|
-
} else {
|
|
140
|
-
anyMap.get(this).clear();
|
|
141
|
-
for (const listeners of eventsMap.get(this).values()) {
|
|
142
|
-
listeners.clear();
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
listenerCount(eventName) {
|
|
148
|
-
if (typeof eventName === 'string') {
|
|
149
|
-
return anyMap.get(this).size + getListeners(this, eventName).size;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (typeof eventName !== 'undefined') {
|
|
153
|
-
assertEventName(eventName);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
let count = anyMap.get(this).size;
|
|
157
|
-
|
|
158
|
-
for (const value of eventsMap.get(this).values()) {
|
|
159
|
-
count += value.size;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return count;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Subclass used to encourage TS users to type their events.
|
|
167
|
-
Emittery.Typed = class extends Emittery {};
|
|
168
|
-
Object.defineProperty(Emittery.Typed, 'Typed', {
|
|
169
|
-
enumerable: false,
|
|
170
|
-
value: undefined
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
module.exports = Emittery;
|