emittery 0.1.2 → 0.4.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/Emittery.d.ts +131 -0
- package/index.js +99 -42
- package/legacy.d.ts +2 -0
- package/legacy.js +171 -0
- package/package.json +28 -5
- package/readme.md +45 -12
package/Emittery.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
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/index.js
CHANGED
|
@@ -1,37 +1,52 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const anyMap = new WeakMap();
|
|
4
|
+
const eventsMap = new WeakMap();
|
|
3
5
|
const resolvedPromise = Promise.resolve();
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
this._anyEvents = new Set();
|
|
7
|
+
function assertEventName(eventName) {
|
|
8
|
+
if (typeof eventName !== 'string') {
|
|
9
|
+
throw new TypeError('eventName must be a string');
|
|
9
10
|
}
|
|
11
|
+
}
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
function assertListener(listener) {
|
|
14
|
+
if (typeof listener !== 'function') {
|
|
15
|
+
throw new TypeError('listener must be a function');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
15
18
|
|
|
16
|
-
|
|
19
|
+
function getListeners(instance, eventName) {
|
|
20
|
+
const events = eventsMap.get(instance);
|
|
21
|
+
if (!events.has(eventName)) {
|
|
22
|
+
events.set(eventName, new Set());
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return events.get(eventName);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
class Emittery {
|
|
29
|
+
constructor() {
|
|
30
|
+
anyMap.set(this, new Set());
|
|
31
|
+
eventsMap.set(this, new Map());
|
|
17
32
|
}
|
|
18
33
|
|
|
19
34
|
on(eventName, listener) {
|
|
20
|
-
|
|
35
|
+
assertEventName(eventName);
|
|
36
|
+
assertListener(listener);
|
|
37
|
+
getListeners(this, eventName).add(listener);
|
|
21
38
|
return this.off.bind(this, eventName, listener);
|
|
22
39
|
}
|
|
23
40
|
|
|
24
41
|
off(eventName, listener) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
this._getListeners(eventName).delete(listener);
|
|
42
|
+
assertEventName(eventName);
|
|
43
|
+
assertListener(listener);
|
|
44
|
+
getListeners(this, eventName).delete(listener);
|
|
31
45
|
}
|
|
32
46
|
|
|
33
47
|
once(eventName) {
|
|
34
48
|
return new Promise(resolve => {
|
|
49
|
+
assertEventName(eventName);
|
|
35
50
|
const off = this.on(eventName, data => {
|
|
36
51
|
off();
|
|
37
52
|
resolve(data);
|
|
@@ -40,56 +55,98 @@ module.exports = class Emittery {
|
|
|
40
55
|
}
|
|
41
56
|
|
|
42
57
|
async emit(eventName, eventData) {
|
|
58
|
+
assertEventName(eventName);
|
|
59
|
+
|
|
60
|
+
const listeners = getListeners(this, eventName);
|
|
61
|
+
const anyListeners = anyMap.get(this);
|
|
62
|
+
const staticListeners = [...listeners];
|
|
63
|
+
const staticAnyListeners = [...anyListeners];
|
|
64
|
+
|
|
43
65
|
await resolvedPromise;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
66
|
+
return Promise.all([
|
|
67
|
+
...staticListeners.map(async listener => {
|
|
68
|
+
if (listeners.has(listener)) {
|
|
69
|
+
return listener(eventData);
|
|
70
|
+
}
|
|
71
|
+
}),
|
|
72
|
+
...staticAnyListeners.map(async listener => {
|
|
73
|
+
if (anyListeners.has(listener)) {
|
|
74
|
+
return listener(eventName, eventData);
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
]);
|
|
47
78
|
}
|
|
48
79
|
|
|
49
80
|
async emitSerial(eventName, eventData) {
|
|
50
|
-
|
|
81
|
+
assertEventName(eventName);
|
|
82
|
+
|
|
83
|
+
const listeners = getListeners(this, eventName);
|
|
84
|
+
const anyListeners = anyMap.get(this);
|
|
85
|
+
const staticListeners = [...listeners];
|
|
86
|
+
const staticAnyListeners = [...anyListeners];
|
|
51
87
|
|
|
88
|
+
await resolvedPromise;
|
|
52
89
|
/* eslint-disable no-await-in-loop */
|
|
53
|
-
for (const listener of
|
|
54
|
-
|
|
90
|
+
for (const listener of staticListeners) {
|
|
91
|
+
if (listeners.has(listener)) {
|
|
92
|
+
await listener(eventData);
|
|
93
|
+
}
|
|
55
94
|
}
|
|
56
95
|
|
|
57
|
-
for (const listener of
|
|
58
|
-
|
|
96
|
+
for (const listener of staticAnyListeners) {
|
|
97
|
+
if (anyListeners.has(listener)) {
|
|
98
|
+
await listener(eventName, eventData);
|
|
99
|
+
}
|
|
59
100
|
}
|
|
60
101
|
/* eslint-enable no-await-in-loop */
|
|
61
102
|
}
|
|
62
103
|
|
|
63
104
|
onAny(listener) {
|
|
64
|
-
|
|
105
|
+
assertListener(listener);
|
|
106
|
+
anyMap.get(this).add(listener);
|
|
65
107
|
return this.offAny.bind(this, listener);
|
|
66
108
|
}
|
|
67
109
|
|
|
68
110
|
offAny(listener) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
this._anyEvents.delete(listener);
|
|
111
|
+
assertListener(listener);
|
|
112
|
+
anyMap.get(this).delete(listener);
|
|
75
113
|
}
|
|
76
114
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
115
|
+
clearListeners(eventName) {
|
|
116
|
+
if (typeof eventName === 'string') {
|
|
117
|
+
getListeners(this, eventName).clear();
|
|
118
|
+
} else {
|
|
119
|
+
anyMap.get(this).clear();
|
|
120
|
+
for (const listeners of eventsMap.get(this).values()) {
|
|
121
|
+
listeners.clear();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
80
124
|
}
|
|
81
125
|
|
|
82
126
|
listenerCount(eventName) {
|
|
83
|
-
if (
|
|
84
|
-
|
|
127
|
+
if (typeof eventName === 'string') {
|
|
128
|
+
return anyMap.get(this).size + getListeners(this, eventName).size;
|
|
129
|
+
}
|
|
85
130
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
131
|
+
if (typeof eventName !== 'undefined') {
|
|
132
|
+
assertEventName(eventName);
|
|
133
|
+
}
|
|
89
134
|
|
|
90
|
-
|
|
135
|
+
let count = anyMap.get(this).size;
|
|
136
|
+
|
|
137
|
+
for (const value of eventsMap.get(this).values()) {
|
|
138
|
+
count += value.size;
|
|
91
139
|
}
|
|
92
140
|
|
|
93
|
-
return
|
|
141
|
+
return count;
|
|
94
142
|
}
|
|
95
|
-
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Subclass used to encourage TS users to type their events.
|
|
146
|
+
Emittery.Typed = class extends Emittery {};
|
|
147
|
+
Object.defineProperty(Emittery.Typed, 'Typed', {
|
|
148
|
+
enumerable: false,
|
|
149
|
+
value: undefined
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
module.exports = Emittery;
|
package/legacy.d.ts
ADDED
package/legacy.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emittery",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Simple and modern async event emitter",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/emittery",
|
|
@@ -10,14 +10,21 @@
|
|
|
10
10
|
"url": "sindresorhus.com"
|
|
11
11
|
},
|
|
12
12
|
"engines": {
|
|
13
|
-
"node": ">=
|
|
13
|
+
"node": ">=6"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"
|
|
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
20
|
},
|
|
18
21
|
"files": [
|
|
19
|
-
"
|
|
22
|
+
"Emittery.d.ts",
|
|
23
|
+
"index.js",
|
|
24
|
+
"legacy.js",
|
|
25
|
+
"legacy.d.ts"
|
|
20
26
|
],
|
|
27
|
+
"typings": "./Emittery.d.ts",
|
|
21
28
|
"keywords": [
|
|
22
29
|
"event",
|
|
23
30
|
"emitter",
|
|
@@ -43,8 +50,24 @@
|
|
|
43
50
|
"promise"
|
|
44
51
|
],
|
|
45
52
|
"devDependencies": {
|
|
53
|
+
"@types/node": "^10.1.2",
|
|
46
54
|
"ava": "*",
|
|
47
|
-
"
|
|
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",
|
|
48
64
|
"xo": "*"
|
|
65
|
+
},
|
|
66
|
+
"nyc": {
|
|
67
|
+
"reporter": [
|
|
68
|
+
"html",
|
|
69
|
+
"lcov",
|
|
70
|
+
"text"
|
|
71
|
+
]
|
|
49
72
|
}
|
|
50
73
|
}
|
package/readme.md
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
> Simple and modern async event emitter
|
|
4
4
|
|
|
5
|
-
[](https://travis-ci.org/sindresorhus/emittery)
|
|
5
|
+
[](https://travis-ci.org/sindresorhus/emittery) [](https://codecov.io/gh/sindresorhus/emittery)
|
|
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
|
+
It's works in Node.js and the browser (using a bundler).
|
|
10
|
+
|
|
9
11
|
Emitting events asynchronously is important for production code where you want the least amount of synchronous operations.
|
|
10
12
|
|
|
11
13
|
|
|
@@ -30,6 +32,10 @@ emitter.on('🦄', data => {
|
|
|
30
32
|
emitter.emit('🦄', '🌈');
|
|
31
33
|
```
|
|
32
34
|
|
|
35
|
+
### Node.js 6
|
|
36
|
+
|
|
37
|
+
The above only works in Node.js 8 or newer. For older Node.js versions you can use `require('emittery/legacy')`.
|
|
38
|
+
|
|
33
39
|
|
|
34
40
|
## API
|
|
35
41
|
|
|
@@ -45,11 +51,11 @@ Using the same listener multiple times for the same event will result in only on
|
|
|
45
51
|
|
|
46
52
|
##### listener(data)
|
|
47
53
|
|
|
48
|
-
#### off(eventName,
|
|
54
|
+
#### off(eventName, listener)
|
|
49
55
|
|
|
50
|
-
|
|
56
|
+
Remove an event subscription.
|
|
51
57
|
|
|
52
|
-
|
|
58
|
+
##### listener(data)
|
|
53
59
|
|
|
54
60
|
#### once(eventName)
|
|
55
61
|
|
|
@@ -78,7 +84,7 @@ Same as above, but it waits for each listener to resolve before triggering the n
|
|
|
78
84
|
|
|
79
85
|
If any of the listeners throw/reject, the returned promise will be rejected with the error and the remaining listeners will *not* be called.
|
|
80
86
|
|
|
81
|
-
####
|
|
87
|
+
#### onAny(listener)
|
|
82
88
|
|
|
83
89
|
Subscribe to be notified about any event.
|
|
84
90
|
|
|
@@ -86,20 +92,44 @@ Returns a method to unsubscribe.
|
|
|
86
92
|
|
|
87
93
|
##### listener(eventName, data)
|
|
88
94
|
|
|
89
|
-
#### offAny(
|
|
90
|
-
|
|
91
|
-
Unsubscribe an `onAny` listener.
|
|
95
|
+
#### offAny(listener)
|
|
92
96
|
|
|
93
|
-
|
|
97
|
+
Remove an `onAny` subscription.
|
|
94
98
|
|
|
95
|
-
####
|
|
99
|
+
#### clearListeners()
|
|
96
100
|
|
|
97
101
|
Clear all event listeners on the instance.
|
|
98
102
|
|
|
103
|
+
If `eventName` is given, only the listeners for that event are cleared.
|
|
104
|
+
|
|
99
105
|
#### listenerCount([eventName])
|
|
100
106
|
|
|
101
107
|
The number of listeners for the `eventName` or all events if not specified.
|
|
102
108
|
|
|
109
|
+
## TypeScript
|
|
110
|
+
|
|
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.
|
|
112
|
+
|
|
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:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import Emittery = require('emittery');
|
|
117
|
+
|
|
118
|
+
const ee = new Emittery.Typed<{value: string}, 'open' | 'close'>();
|
|
119
|
+
|
|
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
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
## Scheduling details
|
|
128
|
+
|
|
129
|
+
Listeners are not invoked for events emitted *before* the listener was added. Removing a listener will prevent that listener from being invoked, even if events are in the process of being (asynchronously!) emitted. This also applies to `.clearListeners()`, which removes all listeners. Listeners will be called in the order they were added. So-called *any* listeners are called *after* event-specific listeners.
|
|
130
|
+
|
|
131
|
+
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
|
+
|
|
103
133
|
|
|
104
134
|
## FAQ
|
|
105
135
|
|
|
@@ -130,11 +160,14 @@ But I would argue doing that shows a deeper lack of Node.js and async comprehens
|
|
|
130
160
|
|
|
131
161
|
### Can you support multiple arguments for `emit()`?
|
|
132
162
|
|
|
133
|
-
No, just use
|
|
163
|
+
No, just use [destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment):
|
|
134
164
|
|
|
135
165
|
```js
|
|
166
|
+
emitter.on('🦄', ([foo, bar]) => {
|
|
167
|
+
console.log(foo, bar);
|
|
168
|
+
});
|
|
169
|
+
|
|
136
170
|
emitter.emit('🦄', [foo, bar]);
|
|
137
|
-
emitter.on('🦄', data => console.log(...data));
|
|
138
171
|
```
|
|
139
172
|
|
|
140
173
|
|