@supercat1337/event-emitter 1.0.7 → 1.0.8
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/README.md +4 -0
- package/dist/event-emitter.esm.d.ts +49 -49
- package/dist/event-emitter.esm.js +3 -3
- package/dist/event-emitter.esm.min.js +1 -1
- package/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/index.js +45 -4
package/README.md
CHANGED
|
@@ -17,6 +17,10 @@ $ npm install @supercat1337/event-emitter
|
|
|
17
17
|
- removeListener(event, listener) - Remove an event listener.
|
|
18
18
|
- waitForEvent(event, max_wait_ms = 0) - Wait for an event to be emitted. If max_wait_ms is set to 0, the function will wait indefinitely.
|
|
19
19
|
- waitForAnyEvent(events, max_wait_ms = 0) - Wait for any of the specified events to be emitted. If max_wait_ms is set to 0, the function will wait indefinitely.
|
|
20
|
+
- clear() - Remove all event listeners
|
|
21
|
+
- clearEventListeners(event) - Remove all listeners for a specified event
|
|
22
|
+
- onHasEventListeners(callback) - Subscribe to the "#has-listeners" event. This event is emitted when the number of listeners for any event (except "#has-listeners" and "#no-listeners") goes from 0 to 1.
|
|
23
|
+
- onNoEventListeners(callback) - Subscribe to the "#no-listeners" event. This event is emitted when the number of listeners for any event (except "#has-listeners" and "#no-listeners") goes from 1 to 0.
|
|
20
24
|
|
|
21
25
|
### Usage
|
|
22
26
|
|
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
/** @module EventEmitter */
|
|
2
|
-
/**
|
|
3
|
-
* @template {string} T
|
|
4
|
-
*/
|
|
5
|
-
export class EventEmitter<T extends string> {
|
|
6
|
-
/** @type {Object.<string, Function[]>} */
|
|
7
|
-
events: {
|
|
8
|
-
[x: string]: Function[];
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* on is used to add a callback function that's going to be executed when the event is triggered
|
|
12
|
-
* @param {T} event
|
|
13
|
-
* @param {Function} listener
|
|
14
|
-
* @returns {()=>void}
|
|
15
|
-
*/
|
|
16
|
-
on(event: T, listener: Function): () => void;
|
|
17
|
-
/**
|
|
18
|
-
* Remove an event listener from an event
|
|
19
|
-
* @param {T} event
|
|
20
|
-
* @param {Function} listener
|
|
21
|
-
*/
|
|
22
|
-
removeListener(event: T, listener: Function): void;
|
|
23
|
-
/**
|
|
24
|
-
* emit is used to trigger an event
|
|
25
|
-
* @param {T} event
|
|
26
|
-
*/
|
|
27
|
-
emit(event: T, ...args: any[]): void;
|
|
28
|
-
/**
|
|
29
|
-
* Add a one-time listener
|
|
30
|
-
* @param {T} event
|
|
31
|
-
* @param {Function} listener
|
|
32
|
-
* @returns {()=>void}
|
|
33
|
-
*/
|
|
34
|
-
once(event: T, listener: Function): () => void;
|
|
35
|
-
/**
|
|
36
|
-
* Wait for an event to be emitted
|
|
37
|
-
* @param {T} event
|
|
38
|
-
* @param {number} [max_wait_ms=0] - Maximum time to wait in ms. If 0, the function will wait indefinitely.
|
|
39
|
-
* @returns {Promise<boolean>} - Resolves with true if the event was emitted, false if the time ran out.
|
|
40
|
-
*/
|
|
41
|
-
waitForEvent(event: T, max_wait_ms?: number): Promise<boolean>;
|
|
42
|
-
/**
|
|
43
|
-
* Wait for any of the specified events to be emitted
|
|
44
|
-
* @param {T[]} events - Array of event names to wait for
|
|
45
|
-
* @param {number} [max_wait_ms=0] - Maximum time to wait in ms. If 0, the function will wait indefinitely.
|
|
46
|
-
* @returns {Promise<boolean>} - Resolves with true if any event was emitted, false if the time ran out.
|
|
47
|
-
*/
|
|
48
|
-
waitForAnyEvent(events: T[], max_wait_ms?: number): Promise<boolean>;
|
|
49
|
-
}
|
|
1
|
+
/** @module EventEmitter */
|
|
2
|
+
/**
|
|
3
|
+
* @template {string} T
|
|
4
|
+
*/
|
|
5
|
+
export class EventEmitter<T extends string> {
|
|
6
|
+
/** @type {Object.<string, Function[]>} */
|
|
7
|
+
events: {
|
|
8
|
+
[x: string]: Function[];
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* on is used to add a callback function that's going to be executed when the event is triggered
|
|
12
|
+
* @param {T} event
|
|
13
|
+
* @param {Function} listener
|
|
14
|
+
* @returns {()=>void}
|
|
15
|
+
*/
|
|
16
|
+
on(event: T, listener: Function): () => void;
|
|
17
|
+
/**
|
|
18
|
+
* Remove an event listener from an event
|
|
19
|
+
* @param {T} event
|
|
20
|
+
* @param {Function} listener
|
|
21
|
+
*/
|
|
22
|
+
removeListener(event: T, listener: Function): void;
|
|
23
|
+
/**
|
|
24
|
+
* emit is used to trigger an event
|
|
25
|
+
* @param {T} event
|
|
26
|
+
*/
|
|
27
|
+
emit(event: T, ...args: any[]): void;
|
|
28
|
+
/**
|
|
29
|
+
* Add a one-time listener
|
|
30
|
+
* @param {T} event
|
|
31
|
+
* @param {Function} listener
|
|
32
|
+
* @returns {()=>void}
|
|
33
|
+
*/
|
|
34
|
+
once(event: T, listener: Function): () => void;
|
|
35
|
+
/**
|
|
36
|
+
* Wait for an event to be emitted
|
|
37
|
+
* @param {T} event
|
|
38
|
+
* @param {number} [max_wait_ms=0] - Maximum time to wait in ms. If 0, the function will wait indefinitely.
|
|
39
|
+
* @returns {Promise<boolean>} - Resolves with true if the event was emitted, false if the time ran out.
|
|
40
|
+
*/
|
|
41
|
+
waitForEvent(event: T, max_wait_ms?: number): Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* Wait for any of the specified events to be emitted
|
|
44
|
+
* @param {T[]} events - Array of event names to wait for
|
|
45
|
+
* @param {number} [max_wait_ms=0] - Maximum time to wait in ms. If 0, the function will wait indefinitely.
|
|
46
|
+
* @returns {Promise<boolean>} - Resolves with true if any event was emitted, false if the time ran out.
|
|
47
|
+
*/
|
|
48
|
+
waitForAnyEvent(events: T[], max_wait_ms?: number): Promise<boolean>;
|
|
49
|
+
}
|
|
50
50
|
//# sourceMappingURL=event-emitter.esm.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var u=class{events={};on(e,t){typeof this.events[e]!="object"&&(this.events[e]=[]),this.events[e].push(t);let s=this;return function(){s.removeListener(e,t)}}removeListener(e,t){var s;typeof this.events[e]=="object"&&(s=this.events[e].indexOf(t),s>-1&&this.events[e].splice(s,1))}emit(e){if(typeof this.events[e]=="object"){var t,s,r,i=[].slice.call(arguments,1);for(s=this.events[e].slice(),r=s.length,t=0;t<r;t++)try{s[t].apply(this,i)}catch(n){console.error(e,i),console.error(n)}}}once(e,t){return this.on(e,function s(){this.removeListener(e,s),t.apply(this,arguments)})}waitForEvent(e,t=0){return new Promise(s=>{let r,i=this.on(e,()=>{t>0&&clearTimeout(r),i(),s(!0)});t>0&&(r=setTimeout(()=>{i(),s(!1)},t))})}waitForAnyEvent(e,t=0){return new Promise(s=>{let r,i=[],n=()=>{t>0&&clearTimeout(r),i.forEach(o=>{o()}),s(!0)};e.forEach(o=>{i.push(this.on(o,n))}),t>0&&(r=setTimeout(()=>{n(),s(!1)},t))})}};export{u as EventEmitter};
|
|
1
|
+
var u=class{events={};on(e,t){typeof this.events[e]!="object"&&(this.events[e]=[]),this.events[e].push(t);let s=this;return function(){s.removeListener(e,t)}}removeListener(e,t){var s;typeof this.events[e]=="object"&&(s=this.events[e].indexOf(t),s>-1&&this.events[e].splice(s,1))}emit(e){if(typeof this.events[e]=="object"){var t,s,r,i=[].slice.call(arguments,1);for(s=this.events[e].slice(),r=s.length,t=0;t<r;t++)try{s[t].apply(this,i)}catch(n){console.error(e,i),console.error(n)}}}once(e,t){return this.on(e,function s(){this.removeListener(e,s),t.apply(this,arguments)})}waitForEvent(e,t=0){return new Promise(s=>{let r,i=this.on(e,()=>{t>0&&clearTimeout(r),i(),s(!0)});t>0&&(r=setTimeout(()=>{i(),s(!1)},t))})}waitForAnyEvent(e,t=0){return new Promise(s=>{let r,i=[],n=()=>{t>0&&clearTimeout(r),i.forEach(o=>{o()}),s(!0)};e.forEach(o=>{i.push(this.on(o,n))}),t>0&&(r=setTimeout(()=>{n(),s(!1)},t))})}};export{u as EventEmitter};
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { EventEmitter } from "./dist/event-emitter.esm.js";
|
|
1
|
+
export { EventEmitter } from "./dist/event-emitter.esm.js";
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -10,7 +10,7 @@ class EventEmitter {
|
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* on is used to add a callback function that's going to be executed when the event is triggered
|
|
13
|
-
* @param {T} event
|
|
13
|
+
* @param {T|"#has-listeners"|"#no-listeners"} event
|
|
14
14
|
* @param {Function} listener
|
|
15
15
|
* @returns {()=>void}
|
|
16
16
|
*/
|
|
@@ -28,11 +28,15 @@ class EventEmitter {
|
|
|
28
28
|
that.removeListener(event, listener);
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
if (this.events[event].length == 1) {
|
|
32
|
+
this.emit("#has-listeners", event, listener);
|
|
33
|
+
}
|
|
34
|
+
|
|
31
35
|
return unsubscriber;
|
|
32
36
|
}
|
|
33
37
|
/**
|
|
34
38
|
* Remove an event listener from an event
|
|
35
|
-
* @param {T} event
|
|
39
|
+
* @param {T|"#has-listeners"|"#no-listeners"} event
|
|
36
40
|
* @param {Function} listener
|
|
37
41
|
*/
|
|
38
42
|
removeListener(event, listener) {
|
|
@@ -43,13 +47,17 @@ class EventEmitter {
|
|
|
43
47
|
|
|
44
48
|
if (idx > -1) {
|
|
45
49
|
this.events[event].splice(idx, 1);
|
|
50
|
+
|
|
51
|
+
if (this.events[event].length == 0) {
|
|
52
|
+
this.emit("#no-listeners", event, listener);
|
|
53
|
+
}
|
|
46
54
|
}
|
|
47
55
|
}
|
|
48
56
|
|
|
49
57
|
}
|
|
50
58
|
/**
|
|
51
59
|
* emit is used to trigger an event
|
|
52
|
-
* @param {T} event
|
|
60
|
+
* @param {T|"#has-listeners"|"#no-listeners"} event
|
|
53
61
|
*/
|
|
54
62
|
emit(event) {
|
|
55
63
|
if (typeof this.events[event] !== 'object') return;
|
|
@@ -74,7 +82,7 @@ class EventEmitter {
|
|
|
74
82
|
|
|
75
83
|
/**
|
|
76
84
|
* Add a one-time listener
|
|
77
|
-
* @param {T} event
|
|
85
|
+
* @param {T|"#has-listeners"|"#no-listeners"} event
|
|
78
86
|
* @param {Function} listener
|
|
79
87
|
* @returns {()=>void}
|
|
80
88
|
*/
|
|
@@ -159,6 +167,39 @@ class EventEmitter {
|
|
|
159
167
|
|
|
160
168
|
});
|
|
161
169
|
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Clear all events
|
|
173
|
+
*/
|
|
174
|
+
clear() {
|
|
175
|
+
this.events = {};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Clears all listeners for a specified event.
|
|
180
|
+
* @param {T|"#has-listeners"|"#no-listeners"} event - The event for which to clear all listeners.
|
|
181
|
+
*/
|
|
182
|
+
clearEventListeners(event) {
|
|
183
|
+
this.events[event] = [];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* onHasEventListeners() is used to subscribe to the "#has-listeners" event. This event is emitted when the number of listeners for any event (except "#has-listeners" and "#no-listeners") goes from 0 to 1.
|
|
188
|
+
* @param {Function} callback
|
|
189
|
+
* @returns {()=>void}
|
|
190
|
+
*/
|
|
191
|
+
onHasEventListeners(callback) {
|
|
192
|
+
return this.on("#has-listeners", callback);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* onNoEventListeners() is used to subscribe to the "#no-listeners" event. This event is emitted when the number of listeners for any event (except "#has-listeners" and "#no-listeners") goes from 1 to 0.
|
|
197
|
+
* @param {Function} callback
|
|
198
|
+
* @returns {()=>void}
|
|
199
|
+
*/
|
|
200
|
+
onNoEventListeners(callback) {
|
|
201
|
+
return this.on("#no-listeners", callback);
|
|
202
|
+
}
|
|
162
203
|
}
|
|
163
204
|
|
|
164
205
|
export { EventEmitter };
|