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.d.ts
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
|
|
package/dist/node/index.js
CHANGED
|
@@ -6,13 +6,99 @@ var Eventified = class {
|
|
|
6
6
|
this._eventListeners = /* @__PURE__ */ new Map();
|
|
7
7
|
this._maxListeners = 100;
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Adds a handler function for a specific event that will run only once
|
|
11
|
+
* @param {string | symbol} eventName
|
|
12
|
+
* @param {EventListener} listener
|
|
13
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
14
|
+
*/
|
|
15
|
+
once(eventName, listener) {
|
|
16
|
+
const onceListener = (...arguments_) => {
|
|
17
|
+
this.off(eventName, onceListener);
|
|
18
|
+
listener(...arguments_);
|
|
19
|
+
};
|
|
20
|
+
this.on(eventName, onceListener);
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Gets the number of listeners for a specific event. If no event is provided, it returns the total number of listeners
|
|
25
|
+
* @param {string} eventName The event name. Not required
|
|
26
|
+
* @returns {number} The number of listeners
|
|
27
|
+
*/
|
|
28
|
+
listenerCount(eventName) {
|
|
29
|
+
if (!eventName) {
|
|
30
|
+
return this.getAllListeners().length;
|
|
31
|
+
}
|
|
32
|
+
const listeners = this._eventListeners.get(eventName);
|
|
33
|
+
return listeners ? listeners.length : 0;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Gets an array of event names
|
|
37
|
+
* @returns {Array<string | symbol>} An array of event names
|
|
38
|
+
*/
|
|
39
|
+
eventNames() {
|
|
40
|
+
return Array.from(this._eventListeners.keys());
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Gets an array of listeners for a specific event. If no event is provided, it returns all listeners
|
|
44
|
+
* @param {string} [event] (Optional) The event name
|
|
45
|
+
* @returns {EventListener[]} An array of listeners
|
|
46
|
+
*/
|
|
47
|
+
rawListeners(event) {
|
|
48
|
+
if (!event) {
|
|
49
|
+
return this.getAllListeners();
|
|
50
|
+
}
|
|
51
|
+
return this._eventListeners.get(event) ?? [];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Prepends a listener to the beginning of the listeners array for the specified event
|
|
55
|
+
* @param {string | symbol} eventName
|
|
56
|
+
* @param {EventListener} listener
|
|
57
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
58
|
+
*/
|
|
59
|
+
prependListener(eventName, listener) {
|
|
60
|
+
const listeners = this._eventListeners.get(eventName) ?? [];
|
|
61
|
+
listeners.unshift(listener);
|
|
62
|
+
this._eventListeners.set(eventName, listeners);
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Prepends a one-time listener to the beginning of the listeners array for the specified event
|
|
67
|
+
* @param {string | symbol} eventName
|
|
68
|
+
* @param {EventListener} listener
|
|
69
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
70
|
+
*/
|
|
71
|
+
prependOnceListener(eventName, listener) {
|
|
72
|
+
const onceListener = (...arguments_) => {
|
|
73
|
+
this.off(eventName, onceListener);
|
|
74
|
+
listener(...arguments_);
|
|
75
|
+
};
|
|
76
|
+
this.prependListener(eventName, onceListener);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Gets the maximum number of listeners that can be added for a single event
|
|
81
|
+
* @returns {number} The maximum number of listeners
|
|
82
|
+
*/
|
|
9
83
|
maxListeners() {
|
|
10
84
|
return this._maxListeners;
|
|
11
85
|
}
|
|
12
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Adds a listener for a specific event. It is an alias for the on() method
|
|
88
|
+
* @param {string | symbol} event
|
|
89
|
+
* @param {EventListener} listener
|
|
90
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
91
|
+
*/
|
|
13
92
|
addListener(event, listener) {
|
|
14
93
|
this.on(event, listener);
|
|
94
|
+
return this;
|
|
15
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Adds a listener for a specific event
|
|
98
|
+
* @param {string | symbol} event
|
|
99
|
+
* @param {EventListener} listener
|
|
100
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
101
|
+
*/
|
|
16
102
|
on(event, listener) {
|
|
17
103
|
if (!this._eventListeners.has(event)) {
|
|
18
104
|
this._eventListeners.set(event, []);
|
|
@@ -24,11 +110,24 @@ var Eventified = class {
|
|
|
24
110
|
}
|
|
25
111
|
listeners.push(listener);
|
|
26
112
|
}
|
|
113
|
+
return this;
|
|
27
114
|
}
|
|
28
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Removes a listener for a specific event. It is an alias for the off() method
|
|
117
|
+
* @param {string | symbol} event
|
|
118
|
+
* @param {EventListener} listener
|
|
119
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
120
|
+
*/
|
|
29
121
|
removeListener(event, listener) {
|
|
30
122
|
this.off(event, listener);
|
|
123
|
+
return this;
|
|
31
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Removes a listener for a specific event
|
|
127
|
+
* @param {string | symbol} event
|
|
128
|
+
* @param {EventListener} listener
|
|
129
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
130
|
+
*/
|
|
32
131
|
off(event, listener) {
|
|
33
132
|
const listeners = this._eventListeners.get(event) ?? [];
|
|
34
133
|
const index = listeners.indexOf(listener);
|
|
@@ -38,29 +137,51 @@ var Eventified = class {
|
|
|
38
137
|
if (listeners.length === 0) {
|
|
39
138
|
this._eventListeners.delete(event);
|
|
40
139
|
}
|
|
140
|
+
return this;
|
|
41
141
|
}
|
|
42
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Calls all listeners for a specific event
|
|
144
|
+
* @param {string | symbol} event
|
|
145
|
+
* @param arguments_ The arguments to pass to the listeners
|
|
146
|
+
* @returns {boolean} Returns true if the event had listeners, false otherwise
|
|
147
|
+
*/
|
|
43
148
|
emit(event, ...arguments_) {
|
|
149
|
+
let result = false;
|
|
44
150
|
const listeners = this._eventListeners.get(event);
|
|
45
151
|
if (listeners && listeners.length > 0) {
|
|
46
152
|
for (const listener of listeners) {
|
|
47
153
|
listener(...arguments_);
|
|
154
|
+
result = true;
|
|
48
155
|
}
|
|
49
156
|
}
|
|
157
|
+
return result;
|
|
50
158
|
}
|
|
51
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Gets all listeners for a specific event. If no event is provided, it returns all listeners
|
|
161
|
+
* @param {string} [event] (Optional) The event name
|
|
162
|
+
* @returns {EventListener[]} An array of listeners
|
|
163
|
+
*/
|
|
52
164
|
listeners(event) {
|
|
53
165
|
return this._eventListeners.get(event) ?? [];
|
|
54
166
|
}
|
|
55
|
-
|
|
167
|
+
/**
|
|
168
|
+
* Removes all listeners for a specific event. If no event is provided, it removes all listeners
|
|
169
|
+
* @param {string} [event] (Optional) The event name
|
|
170
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
171
|
+
*/
|
|
56
172
|
removeAllListeners(event) {
|
|
57
173
|
if (event) {
|
|
58
174
|
this._eventListeners.delete(event);
|
|
59
175
|
} else {
|
|
60
176
|
this._eventListeners.clear();
|
|
61
177
|
}
|
|
178
|
+
return this;
|
|
62
179
|
}
|
|
63
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Sets the maximum number of listeners that can be added for a single event
|
|
182
|
+
* @param {number} n The maximum number of listeners
|
|
183
|
+
* @returns {void}
|
|
184
|
+
*/
|
|
64
185
|
setMaxListeners(n) {
|
|
65
186
|
this._maxListeners = n;
|
|
66
187
|
for (const listeners of this._eventListeners.values()) {
|
|
@@ -69,6 +190,17 @@ var Eventified = class {
|
|
|
69
190
|
}
|
|
70
191
|
}
|
|
71
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Gets all listeners
|
|
195
|
+
* @returns {EventListener[]} An array of listeners
|
|
196
|
+
*/
|
|
197
|
+
getAllListeners() {
|
|
198
|
+
let result = new Array();
|
|
199
|
+
for (const listeners of this._eventListeners.values()) {
|
|
200
|
+
result = result.concat(listeners);
|
|
201
|
+
}
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
72
204
|
};
|
|
73
205
|
|
|
74
206
|
// src/index.ts
|
|
@@ -78,7 +210,12 @@ var Hookified = class extends Eventified {
|
|
|
78
210
|
super();
|
|
79
211
|
this._hooks = /* @__PURE__ */ new Map();
|
|
80
212
|
}
|
|
81
|
-
|
|
213
|
+
/**
|
|
214
|
+
* Adds a handler function for a specific event
|
|
215
|
+
* @param {string} event
|
|
216
|
+
* @param {Hook} handler - this can be async or sync
|
|
217
|
+
* @returns {void}
|
|
218
|
+
*/
|
|
82
219
|
onHook(event, handler) {
|
|
83
220
|
const eventHandlers = this._hooks.get(event);
|
|
84
221
|
if (eventHandlers) {
|
|
@@ -87,7 +224,24 @@ var Hookified = class extends Eventified {
|
|
|
87
224
|
this._hooks.set(event, [handler]);
|
|
88
225
|
}
|
|
89
226
|
}
|
|
90
|
-
|
|
227
|
+
/**
|
|
228
|
+
* Adds a handler that only executes once for a specific event
|
|
229
|
+
* @param event
|
|
230
|
+
* @param handler
|
|
231
|
+
*/
|
|
232
|
+
onceHook(event, handler) {
|
|
233
|
+
const hook = async (...arguments_) => {
|
|
234
|
+
this.removeHook(event, hook);
|
|
235
|
+
return handler(...arguments_);
|
|
236
|
+
};
|
|
237
|
+
this.onHook(event, hook);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Removes a handler function for a specific event
|
|
241
|
+
* @param {string} event
|
|
242
|
+
* @param {Hook} handler
|
|
243
|
+
* @returns {void}
|
|
244
|
+
*/
|
|
91
245
|
removeHook(event, handler) {
|
|
92
246
|
const eventHandlers = this._hooks.get(event);
|
|
93
247
|
if (eventHandlers) {
|
|
@@ -97,7 +251,12 @@ var Hookified = class extends Eventified {
|
|
|
97
251
|
}
|
|
98
252
|
}
|
|
99
253
|
}
|
|
100
|
-
|
|
254
|
+
/**
|
|
255
|
+
* Calls all handlers for a specific event
|
|
256
|
+
* @param {string} event
|
|
257
|
+
* @param {T[]} arguments_
|
|
258
|
+
* @returns {Promise<void>}
|
|
259
|
+
*/
|
|
101
260
|
async hook(event, ...arguments_) {
|
|
102
261
|
const eventHandlers = this._hooks.get(event);
|
|
103
262
|
if (eventHandlers) {
|
|
@@ -110,13 +269,25 @@ var Hookified = class extends Eventified {
|
|
|
110
269
|
}
|
|
111
270
|
}
|
|
112
271
|
}
|
|
113
|
-
|
|
272
|
+
/**
|
|
273
|
+
* Gets all hooks
|
|
274
|
+
* @returns {Map<string, Hook[]>}
|
|
275
|
+
*/
|
|
114
276
|
get hooks() {
|
|
115
277
|
return this._hooks;
|
|
116
278
|
}
|
|
279
|
+
/**
|
|
280
|
+
* Gets all hooks for a specific event
|
|
281
|
+
* @param {string} event
|
|
282
|
+
* @returns {Hook[]}
|
|
283
|
+
*/
|
|
117
284
|
getHooks(event) {
|
|
118
285
|
return this._hooks.get(event);
|
|
119
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* Removes all hooks
|
|
289
|
+
* @returns {void}
|
|
290
|
+
*/
|
|
120
291
|
clearHooks() {
|
|
121
292
|
this._hooks.clear();
|
|
122
293
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hookified",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Event and Middleware Hooks",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/node/index.cjs",
|
|
@@ -57,12 +57,12 @@
|
|
|
57
57
|
},
|
|
58
58
|
"homepage": "https://github.com/jaredwray/hookified#readme",
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@vitest/coverage-v8": "^2.1.
|
|
61
|
-
"docula": "^0.9.
|
|
60
|
+
"@vitest/coverage-v8": "^2.1.4",
|
|
61
|
+
"docula": "^0.9.4",
|
|
62
62
|
"rimraf": "^6.0.1",
|
|
63
|
-
"tsup": "^8.3.
|
|
63
|
+
"tsup": "^8.3.5",
|
|
64
64
|
"typescript": "^5.6.3",
|
|
65
|
-
"vitest": "^2.1.
|
|
65
|
+
"vitest": "^2.1.4",
|
|
66
66
|
"xo": "^0.59.3"
|
|
67
67
|
},
|
|
68
68
|
"files": [
|