hookified 1.3.0 → 1.5.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/README.md +476 -3
- package/dist/browser/index.global.js +1 -1
- package/dist/browser/index.global.js.map +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +181 -10
- package/dist/node/index.d.cts +282 -8
- package/dist/node/index.d.ts +282 -8
- package/dist/node/index.js +181 -10
- package/package.json +5 -5
package/dist/node/index.cjs
CHANGED
|
@@ -33,13 +33,99 @@ var Eventified = class {
|
|
|
33
33
|
this._eventListeners = /* @__PURE__ */ new Map();
|
|
34
34
|
this._maxListeners = 100;
|
|
35
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Adds a handler function for a specific event that will run only once
|
|
38
|
+
* @param {string | symbol} eventName
|
|
39
|
+
* @param {EventListener} listener
|
|
40
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
41
|
+
*/
|
|
42
|
+
once(eventName, listener) {
|
|
43
|
+
const onceListener = (...arguments_) => {
|
|
44
|
+
this.off(eventName, onceListener);
|
|
45
|
+
listener(...arguments_);
|
|
46
|
+
};
|
|
47
|
+
this.on(eventName, onceListener);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Gets the number of listeners for a specific event. If no event is provided, it returns the total number of listeners
|
|
52
|
+
* @param {string} eventName The event name. Not required
|
|
53
|
+
* @returns {number} The number of listeners
|
|
54
|
+
*/
|
|
55
|
+
listenerCount(eventName) {
|
|
56
|
+
if (!eventName) {
|
|
57
|
+
return this.getAllListeners().length;
|
|
58
|
+
}
|
|
59
|
+
const listeners = this._eventListeners.get(eventName);
|
|
60
|
+
return listeners ? listeners.length : 0;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Gets an array of event names
|
|
64
|
+
* @returns {Array<string | symbol>} An array of event names
|
|
65
|
+
*/
|
|
66
|
+
eventNames() {
|
|
67
|
+
return Array.from(this._eventListeners.keys());
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Gets an array of listeners for a specific event. If no event is provided, it returns all listeners
|
|
71
|
+
* @param {string} [event] (Optional) The event name
|
|
72
|
+
* @returns {EventListener[]} An array of listeners
|
|
73
|
+
*/
|
|
74
|
+
rawListeners(event) {
|
|
75
|
+
if (!event) {
|
|
76
|
+
return this.getAllListeners();
|
|
77
|
+
}
|
|
78
|
+
return this._eventListeners.get(event) ?? [];
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Prepends a listener to the beginning of the listeners array for the specified event
|
|
82
|
+
* @param {string | symbol} eventName
|
|
83
|
+
* @param {EventListener} listener
|
|
84
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
85
|
+
*/
|
|
86
|
+
prependListener(eventName, listener) {
|
|
87
|
+
const listeners = this._eventListeners.get(eventName) ?? [];
|
|
88
|
+
listeners.unshift(listener);
|
|
89
|
+
this._eventListeners.set(eventName, listeners);
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Prepends a one-time listener to the beginning of the listeners array for the specified event
|
|
94
|
+
* @param {string | symbol} eventName
|
|
95
|
+
* @param {EventListener} listener
|
|
96
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
97
|
+
*/
|
|
98
|
+
prependOnceListener(eventName, listener) {
|
|
99
|
+
const onceListener = (...arguments_) => {
|
|
100
|
+
this.off(eventName, onceListener);
|
|
101
|
+
listener(...arguments_);
|
|
102
|
+
};
|
|
103
|
+
this.prependListener(eventName, onceListener);
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Gets the maximum number of listeners that can be added for a single event
|
|
108
|
+
* @returns {number} The maximum number of listeners
|
|
109
|
+
*/
|
|
36
110
|
maxListeners() {
|
|
37
111
|
return this._maxListeners;
|
|
38
112
|
}
|
|
39
|
-
|
|
113
|
+
/**
|
|
114
|
+
* Adds a listener for a specific event. It is an alias for the on() method
|
|
115
|
+
* @param {string | symbol} event
|
|
116
|
+
* @param {EventListener} listener
|
|
117
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
118
|
+
*/
|
|
40
119
|
addListener(event, listener) {
|
|
41
120
|
this.on(event, listener);
|
|
121
|
+
return this;
|
|
42
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Adds a listener for a specific event
|
|
125
|
+
* @param {string | symbol} event
|
|
126
|
+
* @param {EventListener} listener
|
|
127
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
128
|
+
*/
|
|
43
129
|
on(event, listener) {
|
|
44
130
|
if (!this._eventListeners.has(event)) {
|
|
45
131
|
this._eventListeners.set(event, []);
|
|
@@ -51,11 +137,24 @@ var Eventified = class {
|
|
|
51
137
|
}
|
|
52
138
|
listeners.push(listener);
|
|
53
139
|
}
|
|
140
|
+
return this;
|
|
54
141
|
}
|
|
55
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Removes a listener for a specific event. It is an alias for the off() method
|
|
144
|
+
* @param {string | symbol} event
|
|
145
|
+
* @param {EventListener} listener
|
|
146
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
147
|
+
*/
|
|
56
148
|
removeListener(event, listener) {
|
|
57
149
|
this.off(event, listener);
|
|
150
|
+
return this;
|
|
58
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Removes a listener for a specific event
|
|
154
|
+
* @param {string | symbol} event
|
|
155
|
+
* @param {EventListener} listener
|
|
156
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
157
|
+
*/
|
|
59
158
|
off(event, listener) {
|
|
60
159
|
const listeners = this._eventListeners.get(event) ?? [];
|
|
61
160
|
const index = listeners.indexOf(listener);
|
|
@@ -65,29 +164,51 @@ var Eventified = class {
|
|
|
65
164
|
if (listeners.length === 0) {
|
|
66
165
|
this._eventListeners.delete(event);
|
|
67
166
|
}
|
|
167
|
+
return this;
|
|
68
168
|
}
|
|
69
|
-
|
|
169
|
+
/**
|
|
170
|
+
* Calls all listeners for a specific event
|
|
171
|
+
* @param {string | symbol} event
|
|
172
|
+
* @param arguments_ The arguments to pass to the listeners
|
|
173
|
+
* @returns {boolean} Returns true if the event had listeners, false otherwise
|
|
174
|
+
*/
|
|
70
175
|
emit(event, ...arguments_) {
|
|
176
|
+
let result = false;
|
|
71
177
|
const listeners = this._eventListeners.get(event);
|
|
72
178
|
if (listeners && listeners.length > 0) {
|
|
73
179
|
for (const listener of listeners) {
|
|
74
180
|
listener(...arguments_);
|
|
181
|
+
result = true;
|
|
75
182
|
}
|
|
76
183
|
}
|
|
184
|
+
return result;
|
|
77
185
|
}
|
|
78
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Gets all listeners for a specific event. If no event is provided, it returns all listeners
|
|
188
|
+
* @param {string} [event] (Optional) The event name
|
|
189
|
+
* @returns {EventListener[]} An array of listeners
|
|
190
|
+
*/
|
|
79
191
|
listeners(event) {
|
|
80
192
|
return this._eventListeners.get(event) ?? [];
|
|
81
193
|
}
|
|
82
|
-
|
|
194
|
+
/**
|
|
195
|
+
* Removes all listeners for a specific event. If no event is provided, it removes all listeners
|
|
196
|
+
* @param {string} [event] (Optional) The event name
|
|
197
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
198
|
+
*/
|
|
83
199
|
removeAllListeners(event) {
|
|
84
200
|
if (event) {
|
|
85
201
|
this._eventListeners.delete(event);
|
|
86
202
|
} else {
|
|
87
203
|
this._eventListeners.clear();
|
|
88
204
|
}
|
|
205
|
+
return this;
|
|
89
206
|
}
|
|
90
|
-
|
|
207
|
+
/**
|
|
208
|
+
* Sets the maximum number of listeners that can be added for a single event
|
|
209
|
+
* @param {number} n The maximum number of listeners
|
|
210
|
+
* @returns {void}
|
|
211
|
+
*/
|
|
91
212
|
setMaxListeners(n) {
|
|
92
213
|
this._maxListeners = n;
|
|
93
214
|
for (const listeners of this._eventListeners.values()) {
|
|
@@ -96,6 +217,17 @@ var Eventified = class {
|
|
|
96
217
|
}
|
|
97
218
|
}
|
|
98
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Gets all listeners
|
|
222
|
+
* @returns {EventListener[]} An array of listeners
|
|
223
|
+
*/
|
|
224
|
+
getAllListeners() {
|
|
225
|
+
let result = new Array();
|
|
226
|
+
for (const listeners of this._eventListeners.values()) {
|
|
227
|
+
result = result.concat(listeners);
|
|
228
|
+
}
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
99
231
|
};
|
|
100
232
|
|
|
101
233
|
// src/index.ts
|
|
@@ -105,7 +237,12 @@ var Hookified = class extends Eventified {
|
|
|
105
237
|
super();
|
|
106
238
|
this._hooks = /* @__PURE__ */ new Map();
|
|
107
239
|
}
|
|
108
|
-
|
|
240
|
+
/**
|
|
241
|
+
* Adds a handler function for a specific event
|
|
242
|
+
* @param {string} event
|
|
243
|
+
* @param {Hook} handler - this can be async or sync
|
|
244
|
+
* @returns {void}
|
|
245
|
+
*/
|
|
109
246
|
onHook(event, handler) {
|
|
110
247
|
const eventHandlers = this._hooks.get(event);
|
|
111
248
|
if (eventHandlers) {
|
|
@@ -114,7 +251,24 @@ var Hookified = class extends Eventified {
|
|
|
114
251
|
this._hooks.set(event, [handler]);
|
|
115
252
|
}
|
|
116
253
|
}
|
|
117
|
-
|
|
254
|
+
/**
|
|
255
|
+
* Adds a handler that only executes once for a specific event
|
|
256
|
+
* @param event
|
|
257
|
+
* @param handler
|
|
258
|
+
*/
|
|
259
|
+
onceHook(event, handler) {
|
|
260
|
+
const hook = async (...arguments_) => {
|
|
261
|
+
this.removeHook(event, hook);
|
|
262
|
+
return handler(...arguments_);
|
|
263
|
+
};
|
|
264
|
+
this.onHook(event, hook);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Removes a handler function for a specific event
|
|
268
|
+
* @param {string} event
|
|
269
|
+
* @param {Hook} handler
|
|
270
|
+
* @returns {void}
|
|
271
|
+
*/
|
|
118
272
|
removeHook(event, handler) {
|
|
119
273
|
const eventHandlers = this._hooks.get(event);
|
|
120
274
|
if (eventHandlers) {
|
|
@@ -124,7 +278,12 @@ var Hookified = class extends Eventified {
|
|
|
124
278
|
}
|
|
125
279
|
}
|
|
126
280
|
}
|
|
127
|
-
|
|
281
|
+
/**
|
|
282
|
+
* Calls all handlers for a specific event
|
|
283
|
+
* @param {string} event
|
|
284
|
+
* @param {T[]} arguments_
|
|
285
|
+
* @returns {Promise<void>}
|
|
286
|
+
*/
|
|
128
287
|
async hook(event, ...arguments_) {
|
|
129
288
|
const eventHandlers = this._hooks.get(event);
|
|
130
289
|
if (eventHandlers) {
|
|
@@ -137,13 +296,25 @@ var Hookified = class extends Eventified {
|
|
|
137
296
|
}
|
|
138
297
|
}
|
|
139
298
|
}
|
|
140
|
-
|
|
299
|
+
/**
|
|
300
|
+
* Gets all hooks
|
|
301
|
+
* @returns {Map<string, Hook[]>}
|
|
302
|
+
*/
|
|
141
303
|
get hooks() {
|
|
142
304
|
return this._hooks;
|
|
143
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* Gets all hooks for a specific event
|
|
308
|
+
* @param {string} event
|
|
309
|
+
* @returns {Hook[]}
|
|
310
|
+
*/
|
|
144
311
|
getHooks(event) {
|
|
145
312
|
return this._hooks.get(event);
|
|
146
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Removes all hooks
|
|
316
|
+
* @returns {void}
|
|
317
|
+
*/
|
|
147
318
|
clearHooks() {
|
|
148
319
|
this._hooks.clear();
|
|
149
320
|
}
|
package/dist/node/index.d.cts
CHANGED
|
@@ -1,28 +1,302 @@
|
|
|
1
|
+
type IEventEmitter = {
|
|
2
|
+
/**
|
|
3
|
+
* Registers a listener for the specified event.
|
|
4
|
+
*
|
|
5
|
+
* @param eventName - The name (or symbol) of the event to listen for.
|
|
6
|
+
* @param listener - A callback function that will be invoked when the event is emitted.
|
|
7
|
+
* @returns The current instance of EventEmitter for method chaining.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* emitter.on('data', (message) => {
|
|
11
|
+
* console.log(message);
|
|
12
|
+
* });
|
|
13
|
+
*/
|
|
14
|
+
on(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
15
|
+
/**
|
|
16
|
+
* Alias for `on`. Registers a listener for the specified event.
|
|
17
|
+
*
|
|
18
|
+
* @param eventName - The name (or symbol) of the event to listen for.
|
|
19
|
+
* @param listener - A callback function that will be invoked when the event is emitted.
|
|
20
|
+
* @returns The current instance of EventEmitter for method chaining.
|
|
21
|
+
*/
|
|
22
|
+
addListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
23
|
+
/**
|
|
24
|
+
* Registers a one-time listener for the specified event. The listener is removed after it is called once.
|
|
25
|
+
*
|
|
26
|
+
* @param eventName - The name (or symbol) of the event to listen for.
|
|
27
|
+
* @param listener - A callback function that will be invoked once when the event is emitted.
|
|
28
|
+
* @returns The current instance of EventEmitter for method chaining.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* emitter.once('close', () => {
|
|
32
|
+
* console.log('The connection was closed.');
|
|
33
|
+
* });
|
|
34
|
+
*/
|
|
35
|
+
once(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
36
|
+
/**
|
|
37
|
+
* Removes a previously registered listener for the specified event.
|
|
38
|
+
*
|
|
39
|
+
* @param eventName - The name (or symbol) of the event to stop listening for.
|
|
40
|
+
* @param listener - The specific callback function to remove.
|
|
41
|
+
* @returns The current instance of EventEmitter for method chaining.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* emitter.off('data', myListener);
|
|
45
|
+
*/
|
|
46
|
+
off(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
47
|
+
/**
|
|
48
|
+
* Alias for `off`. Removes a previously registered listener for the specified event.
|
|
49
|
+
*
|
|
50
|
+
* @param eventName - The name (or symbol) of the event to stop listening for.
|
|
51
|
+
* @param listener - The specific callback function to remove.
|
|
52
|
+
* @returns The current instance of EventEmitter for method chaining.
|
|
53
|
+
*/
|
|
54
|
+
removeListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
55
|
+
/**
|
|
56
|
+
* Emits the specified event, invoking all registered listeners with the provided arguments.
|
|
57
|
+
*
|
|
58
|
+
* @param eventName - The name (or symbol) of the event to emit.
|
|
59
|
+
* @param args - Arguments passed to each listener.
|
|
60
|
+
* @returns `true` if the event had listeners, `false` otherwise.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* emitter.emit('data', 'Hello World');
|
|
64
|
+
*/
|
|
65
|
+
emit(eventName: string | symbol, ...arguments_: any[]): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Returns the number of listeners registered for the specified event.
|
|
68
|
+
*
|
|
69
|
+
* @param eventName - The name (or symbol) of the event.
|
|
70
|
+
* @returns The number of registered listeners.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* const count = emitter.listenerCount('data');
|
|
74
|
+
* console.log(count); // e.g., 2
|
|
75
|
+
*/
|
|
76
|
+
listenerCount(eventName: string | symbol): number;
|
|
77
|
+
/**
|
|
78
|
+
* Removes all listeners for the specified event. If no event is specified, it removes all listeners for all events.
|
|
79
|
+
*
|
|
80
|
+
* @param eventName - (Optional) The name (or symbol) of the event.
|
|
81
|
+
* @returns The current instance of EventEmitter for method chaining.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* emitter.removeAllListeners('data');
|
|
85
|
+
*/
|
|
86
|
+
removeAllListeners(eventName?: string | symbol): IEventEmitter;
|
|
87
|
+
/**
|
|
88
|
+
* Returns an array of event names for which listeners have been registered.
|
|
89
|
+
*
|
|
90
|
+
* @returns An array of event names (or symbols).
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* const events = emitter.eventNames();
|
|
94
|
+
* console.log(events); // e.g., ['data', 'close']
|
|
95
|
+
*/
|
|
96
|
+
eventNames(): Array<string | symbol>;
|
|
97
|
+
/**
|
|
98
|
+
* Returns an array of listeners registered for the specified event.
|
|
99
|
+
*
|
|
100
|
+
* @param eventName - The name (or symbol) of the event.
|
|
101
|
+
* @returns An array of listener functions.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* const listeners = emitter.listeners('data');
|
|
105
|
+
* console.log(listeners.length); // e.g., 2
|
|
106
|
+
*/
|
|
107
|
+
listeners(eventName: string | symbol): Array<(...arguments_: any[]) => void>;
|
|
108
|
+
/**
|
|
109
|
+
* Returns an array of raw listeners for the specified event. This includes listeners wrapped by internal mechanisms (e.g., once-only listeners).
|
|
110
|
+
*
|
|
111
|
+
* @param eventName - The name (or symbol) of the event.
|
|
112
|
+
* @returns An array of raw listener functions.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* const rawListeners = emitter.rawListeners('data');
|
|
116
|
+
*/
|
|
117
|
+
rawListeners(eventName: string | symbol): Array<(...arguments_: any[]) => void>;
|
|
118
|
+
/**
|
|
119
|
+
* Adds a listener to the beginning of the listeners array for the specified event.
|
|
120
|
+
*
|
|
121
|
+
* @param eventName - The name (or symbol) of the event to listen for.
|
|
122
|
+
* @param listener - A callback function that will be invoked when the event is emitted.
|
|
123
|
+
* @returns The current instance of EventEmitter for method chaining.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* emitter.prependListener('data', (message) => {
|
|
127
|
+
* console.log('This will run first.');
|
|
128
|
+
* });
|
|
129
|
+
*/
|
|
130
|
+
prependListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
131
|
+
/**
|
|
132
|
+
* Adds a one-time listener to the beginning of the listeners array for the specified event.
|
|
133
|
+
*
|
|
134
|
+
* @param eventName - The name (or symbol) of the event to listen for.
|
|
135
|
+
* @param listener - A callback function that will be invoked once when the event is emitted.
|
|
136
|
+
* @returns The current instance of EventEmitter for method chaining.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* emitter.prependOnceListener('data', (message) => {
|
|
140
|
+
* console.log('This will run first and only once.');
|
|
141
|
+
* });
|
|
142
|
+
*/
|
|
143
|
+
prependOnceListener(eventName: string | symbol, listener: (...arguments_: any[]) => void): IEventEmitter;
|
|
144
|
+
};
|
|
145
|
+
|
|
1
146
|
type EventListener = (...arguments_: any[]) => void;
|
|
2
|
-
declare class Eventified {
|
|
3
|
-
_eventListeners: Map<string, EventListener[]>;
|
|
147
|
+
declare class Eventified implements IEventEmitter {
|
|
148
|
+
_eventListeners: Map<string | symbol, EventListener[]>;
|
|
4
149
|
_maxListeners: number;
|
|
5
150
|
constructor();
|
|
151
|
+
/**
|
|
152
|
+
* Adds a handler function for a specific event that will run only once
|
|
153
|
+
* @param {string | symbol} eventName
|
|
154
|
+
* @param {EventListener} listener
|
|
155
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
156
|
+
*/
|
|
157
|
+
once(eventName: string | symbol, listener: EventListener): IEventEmitter;
|
|
158
|
+
/**
|
|
159
|
+
* Gets the number of listeners for a specific event. If no event is provided, it returns the total number of listeners
|
|
160
|
+
* @param {string} eventName The event name. Not required
|
|
161
|
+
* @returns {number} The number of listeners
|
|
162
|
+
*/
|
|
163
|
+
listenerCount(eventName?: string | symbol): number;
|
|
164
|
+
/**
|
|
165
|
+
* Gets an array of event names
|
|
166
|
+
* @returns {Array<string | symbol>} An array of event names
|
|
167
|
+
*/
|
|
168
|
+
eventNames(): Array<string | symbol>;
|
|
169
|
+
/**
|
|
170
|
+
* Gets an array of listeners for a specific event. If no event is provided, it returns all listeners
|
|
171
|
+
* @param {string} [event] (Optional) The event name
|
|
172
|
+
* @returns {EventListener[]} An array of listeners
|
|
173
|
+
*/
|
|
174
|
+
rawListeners(event?: string | symbol): EventListener[];
|
|
175
|
+
/**
|
|
176
|
+
* Prepends a listener to the beginning of the listeners array for the specified event
|
|
177
|
+
* @param {string | symbol} eventName
|
|
178
|
+
* @param {EventListener} listener
|
|
179
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
180
|
+
*/
|
|
181
|
+
prependListener(eventName: string | symbol, listener: EventListener): IEventEmitter;
|
|
182
|
+
/**
|
|
183
|
+
* Prepends a one-time listener to the beginning of the listeners array for the specified event
|
|
184
|
+
* @param {string | symbol} eventName
|
|
185
|
+
* @param {EventListener} listener
|
|
186
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
187
|
+
*/
|
|
188
|
+
prependOnceListener(eventName: string | symbol, listener: EventListener): IEventEmitter;
|
|
189
|
+
/**
|
|
190
|
+
* Gets the maximum number of listeners that can be added for a single event
|
|
191
|
+
* @returns {number} The maximum number of listeners
|
|
192
|
+
*/
|
|
6
193
|
maxListeners(): number;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
194
|
+
/**
|
|
195
|
+
* Adds a listener for a specific event. It is an alias for the on() method
|
|
196
|
+
* @param {string | symbol} event
|
|
197
|
+
* @param {EventListener} listener
|
|
198
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
199
|
+
*/
|
|
200
|
+
addListener(event: string | symbol, listener: EventListener): IEventEmitter;
|
|
201
|
+
/**
|
|
202
|
+
* Adds a listener for a specific event
|
|
203
|
+
* @param {string | symbol} event
|
|
204
|
+
* @param {EventListener} listener
|
|
205
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
206
|
+
*/
|
|
207
|
+
on(event: string | symbol, listener: EventListener): IEventEmitter;
|
|
208
|
+
/**
|
|
209
|
+
* Removes a listener for a specific event. It is an alias for the off() method
|
|
210
|
+
* @param {string | symbol} event
|
|
211
|
+
* @param {EventListener} listener
|
|
212
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
213
|
+
*/
|
|
214
|
+
removeListener(event: string, listener: EventListener): IEventEmitter;
|
|
215
|
+
/**
|
|
216
|
+
* Removes a listener for a specific event
|
|
217
|
+
* @param {string | symbol} event
|
|
218
|
+
* @param {EventListener} listener
|
|
219
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
220
|
+
*/
|
|
221
|
+
off(event: string | symbol, listener: EventListener): IEventEmitter;
|
|
222
|
+
/**
|
|
223
|
+
* Calls all listeners for a specific event
|
|
224
|
+
* @param {string | symbol} event
|
|
225
|
+
* @param arguments_ The arguments to pass to the listeners
|
|
226
|
+
* @returns {boolean} Returns true if the event had listeners, false otherwise
|
|
227
|
+
*/
|
|
228
|
+
emit(event: string | symbol, ...arguments_: any[]): boolean;
|
|
229
|
+
/**
|
|
230
|
+
* Gets all listeners for a specific event. If no event is provided, it returns all listeners
|
|
231
|
+
* @param {string} [event] (Optional) The event name
|
|
232
|
+
* @returns {EventListener[]} An array of listeners
|
|
233
|
+
*/
|
|
12
234
|
listeners(event: string): EventListener[];
|
|
13
|
-
|
|
235
|
+
/**
|
|
236
|
+
* Removes all listeners for a specific event. If no event is provided, it removes all listeners
|
|
237
|
+
* @param {string} [event] (Optional) The event name
|
|
238
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
239
|
+
*/
|
|
240
|
+
removeAllListeners(event?: string): IEventEmitter;
|
|
241
|
+
/**
|
|
242
|
+
* Sets the maximum number of listeners that can be added for a single event
|
|
243
|
+
* @param {number} n The maximum number of listeners
|
|
244
|
+
* @returns {void}
|
|
245
|
+
*/
|
|
14
246
|
setMaxListeners(n: number): void;
|
|
247
|
+
/**
|
|
248
|
+
* Gets all listeners
|
|
249
|
+
* @returns {EventListener[]} An array of listeners
|
|
250
|
+
*/
|
|
251
|
+
getAllListeners(): EventListener[];
|
|
15
252
|
}
|
|
16
253
|
|
|
17
254
|
type Hook = (...arguments_: any[]) => Promise<void> | void;
|
|
18
255
|
declare class Hookified extends Eventified {
|
|
19
256
|
_hooks: Map<string, Hook[]>;
|
|
20
257
|
constructor();
|
|
258
|
+
/**
|
|
259
|
+
* Adds a handler function for a specific event
|
|
260
|
+
* @param {string} event
|
|
261
|
+
* @param {Hook} handler - this can be async or sync
|
|
262
|
+
* @returns {void}
|
|
263
|
+
*/
|
|
21
264
|
onHook(event: string, handler: Hook): void;
|
|
265
|
+
/**
|
|
266
|
+
* Adds a handler that only executes once for a specific event
|
|
267
|
+
* @param event
|
|
268
|
+
* @param handler
|
|
269
|
+
*/
|
|
270
|
+
onceHook(event: string, handler: Hook): void;
|
|
271
|
+
/**
|
|
272
|
+
* Removes a handler function for a specific event
|
|
273
|
+
* @param {string} event
|
|
274
|
+
* @param {Hook} handler
|
|
275
|
+
* @returns {void}
|
|
276
|
+
*/
|
|
22
277
|
removeHook(event: string, handler: Hook): void;
|
|
278
|
+
/**
|
|
279
|
+
* Calls all handlers for a specific event
|
|
280
|
+
* @param {string} event
|
|
281
|
+
* @param {T[]} arguments_
|
|
282
|
+
* @returns {Promise<void>}
|
|
283
|
+
*/
|
|
23
284
|
hook<T>(event: string, ...arguments_: T[]): Promise<void>;
|
|
285
|
+
/**
|
|
286
|
+
* Gets all hooks
|
|
287
|
+
* @returns {Map<string, Hook[]>}
|
|
288
|
+
*/
|
|
24
289
|
get hooks(): Map<string, Hook[]>;
|
|
290
|
+
/**
|
|
291
|
+
* Gets all hooks for a specific event
|
|
292
|
+
* @param {string} event
|
|
293
|
+
* @returns {Hook[]}
|
|
294
|
+
*/
|
|
25
295
|
getHooks(event: string): Hook[] | undefined;
|
|
296
|
+
/**
|
|
297
|
+
* Removes all hooks
|
|
298
|
+
* @returns {void}
|
|
299
|
+
*/
|
|
26
300
|
clearHooks(): void;
|
|
27
301
|
}
|
|
28
302
|
|