hookified 1.4.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 +451 -4
- 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 +134 -14
- package/dist/node/index.d.cts +125 -3
- package/dist/node/index.d.ts +125 -3
- package/dist/node/index.js +134 -14
- package/package.json +5 -5
package/dist/node/index.cjs
CHANGED
|
@@ -33,6 +33,12 @@ 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
|
+
*/
|
|
36
42
|
once(eventName, listener) {
|
|
37
43
|
const onceListener = (...arguments_) => {
|
|
38
44
|
this.off(eventName, onceListener);
|
|
@@ -41,6 +47,11 @@ var Eventified = class {
|
|
|
41
47
|
this.on(eventName, onceListener);
|
|
42
48
|
return this;
|
|
43
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
|
+
*/
|
|
44
55
|
listenerCount(eventName) {
|
|
45
56
|
if (!eventName) {
|
|
46
57
|
return this.getAllListeners().length;
|
|
@@ -48,21 +59,42 @@ var Eventified = class {
|
|
|
48
59
|
const listeners = this._eventListeners.get(eventName);
|
|
49
60
|
return listeners ? listeners.length : 0;
|
|
50
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Gets an array of event names
|
|
64
|
+
* @returns {Array<string | symbol>} An array of event names
|
|
65
|
+
*/
|
|
51
66
|
eventNames() {
|
|
52
67
|
return Array.from(this._eventListeners.keys());
|
|
53
68
|
}
|
|
54
|
-
|
|
55
|
-
|
|
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) {
|
|
56
76
|
return this.getAllListeners();
|
|
57
77
|
}
|
|
58
|
-
return this._eventListeners.get(
|
|
78
|
+
return this._eventListeners.get(event) ?? [];
|
|
59
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
|
+
*/
|
|
60
86
|
prependListener(eventName, listener) {
|
|
61
87
|
const listeners = this._eventListeners.get(eventName) ?? [];
|
|
62
88
|
listeners.unshift(listener);
|
|
63
89
|
this._eventListeners.set(eventName, listeners);
|
|
64
90
|
return this;
|
|
65
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
|
+
*/
|
|
66
98
|
prependOnceListener(eventName, listener) {
|
|
67
99
|
const onceListener = (...arguments_) => {
|
|
68
100
|
this.off(eventName, onceListener);
|
|
@@ -71,14 +103,29 @@ var Eventified = class {
|
|
|
71
103
|
this.prependListener(eventName, onceListener);
|
|
72
104
|
return this;
|
|
73
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
|
+
*/
|
|
74
110
|
maxListeners() {
|
|
75
111
|
return this._maxListeners;
|
|
76
112
|
}
|
|
77
|
-
|
|
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
|
+
*/
|
|
78
119
|
addListener(event, listener) {
|
|
79
120
|
this.on(event, listener);
|
|
80
121
|
return this;
|
|
81
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
|
+
*/
|
|
82
129
|
on(event, listener) {
|
|
83
130
|
if (!this._eventListeners.has(event)) {
|
|
84
131
|
this._eventListeners.set(event, []);
|
|
@@ -92,11 +139,22 @@ var Eventified = class {
|
|
|
92
139
|
}
|
|
93
140
|
return this;
|
|
94
141
|
}
|
|
95
|
-
|
|
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
|
+
*/
|
|
96
148
|
removeListener(event, listener) {
|
|
97
149
|
this.off(event, listener);
|
|
98
150
|
return this;
|
|
99
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
|
+
*/
|
|
100
158
|
off(event, listener) {
|
|
101
159
|
const listeners = this._eventListeners.get(event) ?? [];
|
|
102
160
|
const index = listeners.indexOf(listener);
|
|
@@ -108,21 +166,36 @@ var Eventified = class {
|
|
|
108
166
|
}
|
|
109
167
|
return this;
|
|
110
168
|
}
|
|
111
|
-
|
|
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
|
+
*/
|
|
112
175
|
emit(event, ...arguments_) {
|
|
176
|
+
let result = false;
|
|
113
177
|
const listeners = this._eventListeners.get(event);
|
|
114
178
|
if (listeners && listeners.length > 0) {
|
|
115
179
|
for (const listener of listeners) {
|
|
116
180
|
listener(...arguments_);
|
|
181
|
+
result = true;
|
|
117
182
|
}
|
|
118
183
|
}
|
|
119
|
-
return
|
|
184
|
+
return result;
|
|
120
185
|
}
|
|
121
|
-
|
|
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
|
+
*/
|
|
122
191
|
listeners(event) {
|
|
123
192
|
return this._eventListeners.get(event) ?? [];
|
|
124
193
|
}
|
|
125
|
-
|
|
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
|
+
*/
|
|
126
199
|
removeAllListeners(event) {
|
|
127
200
|
if (event) {
|
|
128
201
|
this._eventListeners.delete(event);
|
|
@@ -131,7 +204,11 @@ var Eventified = class {
|
|
|
131
204
|
}
|
|
132
205
|
return this;
|
|
133
206
|
}
|
|
134
|
-
|
|
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
|
+
*/
|
|
135
212
|
setMaxListeners(n) {
|
|
136
213
|
this._maxListeners = n;
|
|
137
214
|
for (const listeners of this._eventListeners.values()) {
|
|
@@ -140,6 +217,10 @@ var Eventified = class {
|
|
|
140
217
|
}
|
|
141
218
|
}
|
|
142
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Gets all listeners
|
|
222
|
+
* @returns {EventListener[]} An array of listeners
|
|
223
|
+
*/
|
|
143
224
|
getAllListeners() {
|
|
144
225
|
let result = new Array();
|
|
145
226
|
for (const listeners of this._eventListeners.values()) {
|
|
@@ -156,7 +237,12 @@ var Hookified = class extends Eventified {
|
|
|
156
237
|
super();
|
|
157
238
|
this._hooks = /* @__PURE__ */ new Map();
|
|
158
239
|
}
|
|
159
|
-
|
|
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
|
+
*/
|
|
160
246
|
onHook(event, handler) {
|
|
161
247
|
const eventHandlers = this._hooks.get(event);
|
|
162
248
|
if (eventHandlers) {
|
|
@@ -165,7 +251,24 @@ var Hookified = class extends Eventified {
|
|
|
165
251
|
this._hooks.set(event, [handler]);
|
|
166
252
|
}
|
|
167
253
|
}
|
|
168
|
-
|
|
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
|
+
*/
|
|
169
272
|
removeHook(event, handler) {
|
|
170
273
|
const eventHandlers = this._hooks.get(event);
|
|
171
274
|
if (eventHandlers) {
|
|
@@ -175,7 +278,12 @@ var Hookified = class extends Eventified {
|
|
|
175
278
|
}
|
|
176
279
|
}
|
|
177
280
|
}
|
|
178
|
-
|
|
281
|
+
/**
|
|
282
|
+
* Calls all handlers for a specific event
|
|
283
|
+
* @param {string} event
|
|
284
|
+
* @param {T[]} arguments_
|
|
285
|
+
* @returns {Promise<void>}
|
|
286
|
+
*/
|
|
179
287
|
async hook(event, ...arguments_) {
|
|
180
288
|
const eventHandlers = this._hooks.get(event);
|
|
181
289
|
if (eventHandlers) {
|
|
@@ -188,13 +296,25 @@ var Hookified = class extends Eventified {
|
|
|
188
296
|
}
|
|
189
297
|
}
|
|
190
298
|
}
|
|
191
|
-
|
|
299
|
+
/**
|
|
300
|
+
* Gets all hooks
|
|
301
|
+
* @returns {Map<string, Hook[]>}
|
|
302
|
+
*/
|
|
192
303
|
get hooks() {
|
|
193
304
|
return this._hooks;
|
|
194
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* Gets all hooks for a specific event
|
|
308
|
+
* @param {string} event
|
|
309
|
+
* @returns {Hook[]}
|
|
310
|
+
*/
|
|
195
311
|
getHooks(event) {
|
|
196
312
|
return this._hooks.get(event);
|
|
197
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Removes all hooks
|
|
316
|
+
* @returns {void}
|
|
317
|
+
*/
|
|
198
318
|
clearHooks() {
|
|
199
319
|
this._hooks.clear();
|
|
200
320
|
}
|
package/dist/node/index.d.cts
CHANGED
|
@@ -148,21 +148,106 @@ declare class Eventified implements IEventEmitter {
|
|
|
148
148
|
_eventListeners: Map<string | symbol, EventListener[]>;
|
|
149
149
|
_maxListeners: number;
|
|
150
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
|
+
*/
|
|
151
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
|
+
*/
|
|
152
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
|
+
*/
|
|
153
168
|
eventNames(): Array<string | symbol>;
|
|
154
|
-
|
|
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
|
+
*/
|
|
155
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
|
+
*/
|
|
156
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
|
+
*/
|
|
157
193
|
maxListeners(): number;
|
|
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
|
+
*/
|
|
158
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
|
+
*/
|
|
159
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
|
+
*/
|
|
160
214
|
removeListener(event: string, listener: EventListener): IEventEmitter;
|
|
161
|
-
|
|
162
|
-
|
|
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
|
+
*/
|
|
163
234
|
listeners(event: string): EventListener[];
|
|
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
|
+
*/
|
|
164
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
|
+
*/
|
|
165
246
|
setMaxListeners(n: number): void;
|
|
247
|
+
/**
|
|
248
|
+
* Gets all listeners
|
|
249
|
+
* @returns {EventListener[]} An array of listeners
|
|
250
|
+
*/
|
|
166
251
|
getAllListeners(): EventListener[];
|
|
167
252
|
}
|
|
168
253
|
|
|
@@ -170,11 +255,48 @@ type Hook = (...arguments_: any[]) => Promise<void> | void;
|
|
|
170
255
|
declare class Hookified extends Eventified {
|
|
171
256
|
_hooks: Map<string, Hook[]>;
|
|
172
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
|
+
*/
|
|
173
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
|
+
*/
|
|
174
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
|
+
*/
|
|
175
284
|
hook<T>(event: string, ...arguments_: T[]): Promise<void>;
|
|
285
|
+
/**
|
|
286
|
+
* Gets all hooks
|
|
287
|
+
* @returns {Map<string, Hook[]>}
|
|
288
|
+
*/
|
|
176
289
|
get hooks(): Map<string, Hook[]>;
|
|
290
|
+
/**
|
|
291
|
+
* Gets all hooks for a specific event
|
|
292
|
+
* @param {string} event
|
|
293
|
+
* @returns {Hook[]}
|
|
294
|
+
*/
|
|
177
295
|
getHooks(event: string): Hook[] | undefined;
|
|
296
|
+
/**
|
|
297
|
+
* Removes all hooks
|
|
298
|
+
* @returns {void}
|
|
299
|
+
*/
|
|
178
300
|
clearHooks(): void;
|
|
179
301
|
}
|
|
180
302
|
|
package/dist/node/index.d.ts
CHANGED
|
@@ -148,21 +148,106 @@ declare class Eventified implements IEventEmitter {
|
|
|
148
148
|
_eventListeners: Map<string | symbol, EventListener[]>;
|
|
149
149
|
_maxListeners: number;
|
|
150
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
|
+
*/
|
|
151
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
|
+
*/
|
|
152
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
|
+
*/
|
|
153
168
|
eventNames(): Array<string | symbol>;
|
|
154
|
-
|
|
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
|
+
*/
|
|
155
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
|
+
*/
|
|
156
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
|
+
*/
|
|
157
193
|
maxListeners(): number;
|
|
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
|
+
*/
|
|
158
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
|
+
*/
|
|
159
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
|
+
*/
|
|
160
214
|
removeListener(event: string, listener: EventListener): IEventEmitter;
|
|
161
|
-
|
|
162
|
-
|
|
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
|
+
*/
|
|
163
234
|
listeners(event: string): EventListener[];
|
|
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
|
+
*/
|
|
164
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
|
+
*/
|
|
165
246
|
setMaxListeners(n: number): void;
|
|
247
|
+
/**
|
|
248
|
+
* Gets all listeners
|
|
249
|
+
* @returns {EventListener[]} An array of listeners
|
|
250
|
+
*/
|
|
166
251
|
getAllListeners(): EventListener[];
|
|
167
252
|
}
|
|
168
253
|
|
|
@@ -170,11 +255,48 @@ type Hook = (...arguments_: any[]) => Promise<void> | void;
|
|
|
170
255
|
declare class Hookified extends Eventified {
|
|
171
256
|
_hooks: Map<string, Hook[]>;
|
|
172
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
|
+
*/
|
|
173
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
|
+
*/
|
|
174
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
|
+
*/
|
|
175
284
|
hook<T>(event: string, ...arguments_: T[]): Promise<void>;
|
|
285
|
+
/**
|
|
286
|
+
* Gets all hooks
|
|
287
|
+
* @returns {Map<string, Hook[]>}
|
|
288
|
+
*/
|
|
176
289
|
get hooks(): Map<string, Hook[]>;
|
|
290
|
+
/**
|
|
291
|
+
* Gets all hooks for a specific event
|
|
292
|
+
* @param {string} event
|
|
293
|
+
* @returns {Hook[]}
|
|
294
|
+
*/
|
|
177
295
|
getHooks(event: string): Hook[] | undefined;
|
|
296
|
+
/**
|
|
297
|
+
* Removes all hooks
|
|
298
|
+
* @returns {void}
|
|
299
|
+
*/
|
|
178
300
|
clearHooks(): void;
|
|
179
301
|
}
|
|
180
302
|
|