hookified 1.12.1 → 1.12.2
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/dist/browser/index.global.js +570 -1
- package/dist/browser/index.global.js.map +1 -1
- package/dist/browser/index.js +571 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/node/index.cjs +595 -1
- package/dist/node/index.js +567 -1
- package/package.json +7 -7
|
@@ -1,2 +1,571 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
(() => {
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
5
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
|
|
7
|
+
// src/eventified.ts
|
|
8
|
+
var Eventified = class {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
__publicField(this, "_eventListeners");
|
|
11
|
+
__publicField(this, "_maxListeners");
|
|
12
|
+
__publicField(this, "_logger");
|
|
13
|
+
__publicField(this, "_throwOnEmitError", false);
|
|
14
|
+
this._eventListeners = /* @__PURE__ */ new Map();
|
|
15
|
+
this._maxListeners = 100;
|
|
16
|
+
this._logger = options?.logger;
|
|
17
|
+
if (options?.throwOnEmitError !== void 0) {
|
|
18
|
+
this._throwOnEmitError = options.throwOnEmitError;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Gets the logger
|
|
23
|
+
* @returns {Logger}
|
|
24
|
+
*/
|
|
25
|
+
get logger() {
|
|
26
|
+
return this._logger;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Sets the logger
|
|
30
|
+
* @param {Logger} logger
|
|
31
|
+
*/
|
|
32
|
+
set logger(logger) {
|
|
33
|
+
this._logger = logger;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Gets whether an error should be thrown when an emit throws an error. Default is false and only emits an error event.
|
|
37
|
+
* @returns {boolean}
|
|
38
|
+
*/
|
|
39
|
+
get throwOnEmitError() {
|
|
40
|
+
return this._throwOnEmitError;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Sets whether an error should be thrown when an emit throws an error. Default is false and only emits an error event.
|
|
44
|
+
* @param {boolean} value
|
|
45
|
+
*/
|
|
46
|
+
set throwOnEmitError(value) {
|
|
47
|
+
this._throwOnEmitError = value;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Adds a handler function for a specific event that will run only once
|
|
51
|
+
* @param {string | symbol} eventName
|
|
52
|
+
* @param {EventListener} listener
|
|
53
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
54
|
+
*/
|
|
55
|
+
once(eventName, listener) {
|
|
56
|
+
const onceListener = (...arguments_) => {
|
|
57
|
+
this.off(eventName, onceListener);
|
|
58
|
+
listener(...arguments_);
|
|
59
|
+
};
|
|
60
|
+
this.on(eventName, onceListener);
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Gets the number of listeners for a specific event. If no event is provided, it returns the total number of listeners
|
|
65
|
+
* @param {string} eventName The event name. Not required
|
|
66
|
+
* @returns {number} The number of listeners
|
|
67
|
+
*/
|
|
68
|
+
listenerCount(eventName) {
|
|
69
|
+
if (eventName === void 0) {
|
|
70
|
+
return this.getAllListeners().length;
|
|
71
|
+
}
|
|
72
|
+
const listeners = this._eventListeners.get(eventName);
|
|
73
|
+
return listeners ? listeners.length : 0;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Gets an array of event names
|
|
77
|
+
* @returns {Array<string | symbol>} An array of event names
|
|
78
|
+
*/
|
|
79
|
+
eventNames() {
|
|
80
|
+
return [...this._eventListeners.keys()];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Gets an array of listeners for a specific event. If no event is provided, it returns all listeners
|
|
84
|
+
* @param {string} [event] (Optional) The event name
|
|
85
|
+
* @returns {EventListener[]} An array of listeners
|
|
86
|
+
*/
|
|
87
|
+
rawListeners(event) {
|
|
88
|
+
if (event === void 0) {
|
|
89
|
+
return this.getAllListeners();
|
|
90
|
+
}
|
|
91
|
+
return this._eventListeners.get(event) ?? [];
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Prepends a listener to the beginning of the listeners array for the specified event
|
|
95
|
+
* @param {string | symbol} eventName
|
|
96
|
+
* @param {EventListener} listener
|
|
97
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
98
|
+
*/
|
|
99
|
+
prependListener(eventName, listener) {
|
|
100
|
+
const listeners = this._eventListeners.get(eventName) ?? [];
|
|
101
|
+
listeners.unshift(listener);
|
|
102
|
+
this._eventListeners.set(eventName, listeners);
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Prepends a one-time listener to the beginning of the listeners array for the specified event
|
|
107
|
+
* @param {string | symbol} eventName
|
|
108
|
+
* @param {EventListener} listener
|
|
109
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
110
|
+
*/
|
|
111
|
+
prependOnceListener(eventName, listener) {
|
|
112
|
+
const onceListener = (...arguments_) => {
|
|
113
|
+
this.off(eventName, onceListener);
|
|
114
|
+
listener(...arguments_);
|
|
115
|
+
};
|
|
116
|
+
this.prependListener(eventName, onceListener);
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Gets the maximum number of listeners that can be added for a single event
|
|
121
|
+
* @returns {number} The maximum number of listeners
|
|
122
|
+
*/
|
|
123
|
+
maxListeners() {
|
|
124
|
+
return this._maxListeners;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Adds a listener for a specific event. It is an alias for the on() method
|
|
128
|
+
* @param {string | symbol} event
|
|
129
|
+
* @param {EventListener} listener
|
|
130
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
131
|
+
*/
|
|
132
|
+
addListener(event, listener) {
|
|
133
|
+
this.on(event, listener);
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Adds a listener for a specific event
|
|
138
|
+
* @param {string | symbol} event
|
|
139
|
+
* @param {EventListener} listener
|
|
140
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
141
|
+
*/
|
|
142
|
+
on(event, listener) {
|
|
143
|
+
if (!this._eventListeners.has(event)) {
|
|
144
|
+
this._eventListeners.set(event, []);
|
|
145
|
+
}
|
|
146
|
+
const listeners = this._eventListeners.get(event);
|
|
147
|
+
if (listeners) {
|
|
148
|
+
if (listeners.length >= this._maxListeners) {
|
|
149
|
+
console.warn(
|
|
150
|
+
`MaxListenersExceededWarning: Possible event memory leak detected. ${listeners.length + 1} ${event} listeners added. Use setMaxListeners() to increase limit.`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
listeners.push(listener);
|
|
154
|
+
}
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Removes a listener for a specific event. It is an alias for the off() method
|
|
159
|
+
* @param {string | symbol} event
|
|
160
|
+
* @param {EventListener} listener
|
|
161
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
162
|
+
*/
|
|
163
|
+
removeListener(event, listener) {
|
|
164
|
+
this.off(event, listener);
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Removes a listener for a specific event
|
|
169
|
+
* @param {string | symbol} event
|
|
170
|
+
* @param {EventListener} listener
|
|
171
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
172
|
+
*/
|
|
173
|
+
off(event, listener) {
|
|
174
|
+
const listeners = this._eventListeners.get(event) ?? [];
|
|
175
|
+
const index = listeners.indexOf(listener);
|
|
176
|
+
if (index !== -1) {
|
|
177
|
+
listeners.splice(index, 1);
|
|
178
|
+
}
|
|
179
|
+
if (listeners.length === 0) {
|
|
180
|
+
this._eventListeners.delete(event);
|
|
181
|
+
}
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Calls all listeners for a specific event
|
|
186
|
+
* @param {string | symbol} event
|
|
187
|
+
* @param arguments_ The arguments to pass to the listeners
|
|
188
|
+
* @returns {boolean} Returns true if the event had listeners, false otherwise
|
|
189
|
+
*/
|
|
190
|
+
emit(event, ...arguments_) {
|
|
191
|
+
let result = false;
|
|
192
|
+
const listeners = this._eventListeners.get(event);
|
|
193
|
+
if (listeners && listeners.length > 0) {
|
|
194
|
+
for (const listener of listeners) {
|
|
195
|
+
listener(...arguments_);
|
|
196
|
+
result = true;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (event === "error") {
|
|
200
|
+
const error = arguments_[0] instanceof Error ? arguments_[0] : new Error(`${arguments_[0]}`);
|
|
201
|
+
if (this._throwOnEmitError && !result) {
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Gets all listeners for a specific event. If no event is provided, it returns all listeners
|
|
209
|
+
* @param {string} [event] (Optional) The event name
|
|
210
|
+
* @returns {EventListener[]} An array of listeners
|
|
211
|
+
*/
|
|
212
|
+
listeners(event) {
|
|
213
|
+
return this._eventListeners.get(event) ?? [];
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Removes all listeners for a specific event. If no event is provided, it removes all listeners
|
|
217
|
+
* @param {string} [event] (Optional) The event name
|
|
218
|
+
* @returns {IEventEmitter} returns the instance of the class for chaining
|
|
219
|
+
*/
|
|
220
|
+
removeAllListeners(event) {
|
|
221
|
+
if (event !== void 0) {
|
|
222
|
+
this._eventListeners.delete(event);
|
|
223
|
+
} else {
|
|
224
|
+
this._eventListeners.clear();
|
|
225
|
+
}
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Sets the maximum number of listeners that can be added for a single event
|
|
230
|
+
* @param {number} n The maximum number of listeners
|
|
231
|
+
* @returns {void}
|
|
232
|
+
*/
|
|
233
|
+
setMaxListeners(n) {
|
|
234
|
+
this._maxListeners = n;
|
|
235
|
+
for (const listeners of this._eventListeners.values()) {
|
|
236
|
+
if (listeners.length > n) {
|
|
237
|
+
listeners.splice(n);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Gets all listeners
|
|
243
|
+
* @returns {EventListener[]} An array of listeners
|
|
244
|
+
*/
|
|
245
|
+
getAllListeners() {
|
|
246
|
+
let result = [];
|
|
247
|
+
for (const listeners of this._eventListeners.values()) {
|
|
248
|
+
result = [...result, ...listeners];
|
|
249
|
+
}
|
|
250
|
+
return result;
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// src/index.ts
|
|
255
|
+
var Hookified = class extends Eventified {
|
|
256
|
+
constructor(options) {
|
|
257
|
+
super({ logger: options?.logger });
|
|
258
|
+
__publicField(this, "_hooks");
|
|
259
|
+
__publicField(this, "_throwHookErrors", false);
|
|
260
|
+
__publicField(this, "_enforceBeforeAfter", false);
|
|
261
|
+
__publicField(this, "_deprecatedHooks");
|
|
262
|
+
__publicField(this, "_allowDeprecated", true);
|
|
263
|
+
this._hooks = /* @__PURE__ */ new Map();
|
|
264
|
+
this._deprecatedHooks = options?.deprecatedHooks ? new Map(options.deprecatedHooks) : /* @__PURE__ */ new Map();
|
|
265
|
+
if (options?.throwHookErrors !== void 0) {
|
|
266
|
+
this._throwHookErrors = options.throwHookErrors;
|
|
267
|
+
}
|
|
268
|
+
if (options?.enforceBeforeAfter !== void 0) {
|
|
269
|
+
this._enforceBeforeAfter = options.enforceBeforeAfter;
|
|
270
|
+
}
|
|
271
|
+
if (options?.allowDeprecated !== void 0) {
|
|
272
|
+
this._allowDeprecated = options.allowDeprecated;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Gets all hooks
|
|
277
|
+
* @returns {Map<string, Hook[]>}
|
|
278
|
+
*/
|
|
279
|
+
get hooks() {
|
|
280
|
+
return this._hooks;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Gets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
284
|
+
* @returns {boolean}
|
|
285
|
+
*/
|
|
286
|
+
get throwHookErrors() {
|
|
287
|
+
return this._throwHookErrors;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Sets whether an error should be thrown when a hook throws an error. Default is false and only emits an error event.
|
|
291
|
+
* @param {boolean} value
|
|
292
|
+
*/
|
|
293
|
+
set throwHookErrors(value) {
|
|
294
|
+
this._throwHookErrors = value;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Gets whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
298
|
+
* @returns {boolean}
|
|
299
|
+
* @default false
|
|
300
|
+
*/
|
|
301
|
+
get enforceBeforeAfter() {
|
|
302
|
+
return this._enforceBeforeAfter;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Sets whether to enforce that all hook names start with 'before' or 'after'. Default is false.
|
|
306
|
+
* @param {boolean} value
|
|
307
|
+
*/
|
|
308
|
+
set enforceBeforeAfter(value) {
|
|
309
|
+
this._enforceBeforeAfter = value;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Gets the map of deprecated hook names to deprecation messages.
|
|
313
|
+
* @returns {Map<string, string>}
|
|
314
|
+
*/
|
|
315
|
+
get deprecatedHooks() {
|
|
316
|
+
return this._deprecatedHooks;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Sets the map of deprecated hook names to deprecation messages.
|
|
320
|
+
* @param {Map<string, string>} value
|
|
321
|
+
*/
|
|
322
|
+
set deprecatedHooks(value) {
|
|
323
|
+
this._deprecatedHooks = value;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Gets whether deprecated hooks are allowed to be registered and executed. Default is true.
|
|
327
|
+
* @returns {boolean}
|
|
328
|
+
*/
|
|
329
|
+
get allowDeprecated() {
|
|
330
|
+
return this._allowDeprecated;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* Sets whether deprecated hooks are allowed to be registered and executed. Default is true.
|
|
334
|
+
* @param {boolean} value
|
|
335
|
+
*/
|
|
336
|
+
set allowDeprecated(value) {
|
|
337
|
+
this._allowDeprecated = value;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Validates hook event name if enforceBeforeAfter is enabled
|
|
341
|
+
* @param {string} event - The event name to validate
|
|
342
|
+
* @throws {Error} If enforceBeforeAfter is true and event doesn't start with 'before' or 'after'
|
|
343
|
+
*/
|
|
344
|
+
validateHookName(event) {
|
|
345
|
+
if (this._enforceBeforeAfter) {
|
|
346
|
+
const eventValue = event.trim().toLocaleLowerCase();
|
|
347
|
+
if (!eventValue.startsWith("before") && !eventValue.startsWith("after")) {
|
|
348
|
+
throw new Error(
|
|
349
|
+
`Hook event "${event}" must start with "before" or "after" when enforceBeforeAfter is enabled`
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Checks if a hook is deprecated and emits a warning if it is
|
|
356
|
+
* @param {string} event - The event name to check
|
|
357
|
+
* @returns {boolean} - Returns true if the hook should proceed, false if it should be blocked
|
|
358
|
+
*/
|
|
359
|
+
checkDeprecatedHook(event) {
|
|
360
|
+
if (this._deprecatedHooks.has(event)) {
|
|
361
|
+
const message = this._deprecatedHooks.get(event);
|
|
362
|
+
const warningMessage = `Hook "${event}" is deprecated${message ? `: ${message}` : ""}`;
|
|
363
|
+
this.emit("warn", { hook: event, message: warningMessage });
|
|
364
|
+
if (this.logger?.warn) {
|
|
365
|
+
this.logger.warn(warningMessage);
|
|
366
|
+
}
|
|
367
|
+
return this._allowDeprecated;
|
|
368
|
+
}
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Adds a handler function for a specific event
|
|
373
|
+
* @param {string} event
|
|
374
|
+
* @param {Hook} handler - this can be async or sync
|
|
375
|
+
* @returns {void}
|
|
376
|
+
*/
|
|
377
|
+
onHook(event, handler) {
|
|
378
|
+
this.validateHookName(event);
|
|
379
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const eventHandlers = this._hooks.get(event);
|
|
383
|
+
if (eventHandlers) {
|
|
384
|
+
eventHandlers.push(handler);
|
|
385
|
+
} else {
|
|
386
|
+
this._hooks.set(event, [handler]);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Adds a handler function for a specific event that runs before all other handlers
|
|
391
|
+
* @param {HookEntry} hookEntry
|
|
392
|
+
* @returns {void}
|
|
393
|
+
*/
|
|
394
|
+
onHookEntry(hookEntry) {
|
|
395
|
+
this.onHook(hookEntry.event, hookEntry.handler);
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Alias for onHook. This is provided for compatibility with other libraries that use the `addHook` method.
|
|
399
|
+
* @param {string} event
|
|
400
|
+
* @param {Hook} handler - this can be async or sync
|
|
401
|
+
* @returns {void}
|
|
402
|
+
*/
|
|
403
|
+
addHook(event, handler) {
|
|
404
|
+
this.onHook(event, handler);
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Adds a handler function for a specific event
|
|
408
|
+
* @param {Array<HookEntry>} hooks
|
|
409
|
+
* @returns {void}
|
|
410
|
+
*/
|
|
411
|
+
onHooks(hooks) {
|
|
412
|
+
for (const hook of hooks) {
|
|
413
|
+
this.onHook(hook.event, hook.handler);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Adds a handler function for a specific event that runs before all other handlers
|
|
418
|
+
* @param {string} event
|
|
419
|
+
* @param {Hook} handler - this can be async or sync
|
|
420
|
+
* @returns {void}
|
|
421
|
+
*/
|
|
422
|
+
prependHook(event, handler) {
|
|
423
|
+
this.validateHookName(event);
|
|
424
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const eventHandlers = this._hooks.get(event);
|
|
428
|
+
if (eventHandlers) {
|
|
429
|
+
eventHandlers.unshift(handler);
|
|
430
|
+
} else {
|
|
431
|
+
this._hooks.set(event, [handler]);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Adds a handler that only executes once for a specific event before all other handlers
|
|
436
|
+
* @param event
|
|
437
|
+
* @param handler
|
|
438
|
+
*/
|
|
439
|
+
prependOnceHook(event, handler) {
|
|
440
|
+
this.validateHookName(event);
|
|
441
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const hook = async (...arguments_) => {
|
|
445
|
+
this.removeHook(event, hook);
|
|
446
|
+
return handler(...arguments_);
|
|
447
|
+
};
|
|
448
|
+
this.prependHook(event, hook);
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Adds a handler that only executes once for a specific event
|
|
452
|
+
* @param event
|
|
453
|
+
* @param handler
|
|
454
|
+
*/
|
|
455
|
+
onceHook(event, handler) {
|
|
456
|
+
this.validateHookName(event);
|
|
457
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
const hook = async (...arguments_) => {
|
|
461
|
+
this.removeHook(event, hook);
|
|
462
|
+
return handler(...arguments_);
|
|
463
|
+
};
|
|
464
|
+
this.onHook(event, hook);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Removes a handler function for a specific event
|
|
468
|
+
* @param {string} event
|
|
469
|
+
* @param {Hook} handler
|
|
470
|
+
* @returns {void}
|
|
471
|
+
*/
|
|
472
|
+
removeHook(event, handler) {
|
|
473
|
+
this.validateHookName(event);
|
|
474
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
const eventHandlers = this._hooks.get(event);
|
|
478
|
+
if (eventHandlers) {
|
|
479
|
+
const index = eventHandlers.indexOf(handler);
|
|
480
|
+
if (index !== -1) {
|
|
481
|
+
eventHandlers.splice(index, 1);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Removes all handlers for a specific event
|
|
487
|
+
* @param {Array<HookEntry>} hooks
|
|
488
|
+
* @returns {void}
|
|
489
|
+
*/
|
|
490
|
+
removeHooks(hooks) {
|
|
491
|
+
for (const hook of hooks) {
|
|
492
|
+
this.removeHook(hook.event, hook.handler);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Calls all handlers for a specific event
|
|
497
|
+
* @param {string} event
|
|
498
|
+
* @param {T[]} arguments_
|
|
499
|
+
* @returns {Promise<void>}
|
|
500
|
+
*/
|
|
501
|
+
async hook(event, ...arguments_) {
|
|
502
|
+
this.validateHookName(event);
|
|
503
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
const eventHandlers = this._hooks.get(event);
|
|
507
|
+
if (eventHandlers) {
|
|
508
|
+
for (const handler of eventHandlers) {
|
|
509
|
+
try {
|
|
510
|
+
await handler(...arguments_);
|
|
511
|
+
} catch (error) {
|
|
512
|
+
const message = `${event}: ${error.message}`;
|
|
513
|
+
this.emit("error", new Error(message));
|
|
514
|
+
if (this.logger) {
|
|
515
|
+
this.logger.error(message);
|
|
516
|
+
}
|
|
517
|
+
if (this._throwHookErrors) {
|
|
518
|
+
throw new Error(message);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Prepends the word `before` to your hook. Example is event is `test`, the before hook is `before:test`.
|
|
526
|
+
* @param {string} event - The event name
|
|
527
|
+
* @param {T[]} arguments_ - The arguments to pass to the hook
|
|
528
|
+
*/
|
|
529
|
+
async beforeHook(event, ...arguments_) {
|
|
530
|
+
await this.hook(`before:${event}`, ...arguments_);
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Prepends the word `after` to your hook. Example is event is `test`, the after hook is `after:test`.
|
|
534
|
+
* @param {string} event - The event name
|
|
535
|
+
* @param {T[]} arguments_ - The arguments to pass to the hook
|
|
536
|
+
*/
|
|
537
|
+
async afterHook(event, ...arguments_) {
|
|
538
|
+
await this.hook(`after:${event}`, ...arguments_);
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Calls all handlers for a specific event. This is an alias for `hook` and is provided for
|
|
542
|
+
* compatibility with other libraries that use the `callHook` method.
|
|
543
|
+
* @param {string} event
|
|
544
|
+
* @param {T[]} arguments_
|
|
545
|
+
* @returns {Promise<void>}
|
|
546
|
+
*/
|
|
547
|
+
async callHook(event, ...arguments_) {
|
|
548
|
+
await this.hook(event, ...arguments_);
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Gets all hooks for a specific event
|
|
552
|
+
* @param {string} event
|
|
553
|
+
* @returns {Hook[]}
|
|
554
|
+
*/
|
|
555
|
+
getHooks(event) {
|
|
556
|
+
this.validateHookName(event);
|
|
557
|
+
if (!this.checkDeprecatedHook(event)) {
|
|
558
|
+
return void 0;
|
|
559
|
+
}
|
|
560
|
+
return this._hooks.get(event);
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Removes all hooks
|
|
564
|
+
* @returns {void}
|
|
565
|
+
*/
|
|
566
|
+
clearHooks() {
|
|
567
|
+
this._hooks.clear();
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
})();
|
|
2
571
|
//# sourceMappingURL=index.global.js.map
|