@tsed/hooks 8.19.2 → 8.19.3
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/lib/esm/Hooks.js +186 -12
- package/lib/types/Hooks.d.ts +399 -43
- package/package.json +3 -3
package/lib/esm/Hooks.js
CHANGED
|
@@ -1,11 +1,42 @@
|
|
|
1
1
|
function match(ref, item) {
|
|
2
2
|
return !ref || !item.ref || (ref && item.ref === ref);
|
|
3
3
|
}
|
|
4
|
+
/**
|
|
5
|
+
* A lightweight event management system for registering, emitting, and managing hook listeners.
|
|
6
|
+
*
|
|
7
|
+
* The Hooks class provides a flexible event system that supports:
|
|
8
|
+
* - Registering listeners with optional references for targeted management
|
|
9
|
+
* - One-time event listeners
|
|
10
|
+
* - Synchronous and asynchronous event emission
|
|
11
|
+
* - Value transformation through alter hooks
|
|
12
|
+
* - Listener removal by callback or reference
|
|
13
|
+
*
|
|
14
|
+
* ### Usage
|
|
15
|
+
*
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const hooks = new Hooks();
|
|
18
|
+
*
|
|
19
|
+
* // Register a listener
|
|
20
|
+
* hooks.on("user:created", (user) => {
|
|
21
|
+
* console.log("User created:", user);
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Emit an event
|
|
25
|
+
* hooks.emit("user:created", [{name: "John"}]);
|
|
26
|
+
*
|
|
27
|
+
* // Alter a value through hooks
|
|
28
|
+
* const modified = hooks.alter("transform:data", {value: 10}, []);
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
4
33
|
export class Hooks {
|
|
5
34
|
#listeners = new Map();
|
|
6
35
|
/**
|
|
7
|
-
* Check if an event has listeners
|
|
8
|
-
*
|
|
36
|
+
* Check if an event has registered listeners.
|
|
37
|
+
*
|
|
38
|
+
* @param event - The event name to check
|
|
39
|
+
* @returns `true` if the event has at least one listener, `false` otherwise
|
|
9
40
|
*/
|
|
10
41
|
has(event) {
|
|
11
42
|
return !!this.#listeners.get(event)?.length;
|
|
@@ -85,11 +116,29 @@ export class Hooks {
|
|
|
85
116
|
}
|
|
86
117
|
}
|
|
87
118
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
119
|
+
* Transform a value by passing it through all registered listeners sequentially.
|
|
120
|
+
*
|
|
121
|
+
* Each listener receives the value (potentially modified by previous listeners) and returns
|
|
122
|
+
* a new or modified value. The final transformed value is returned after all listeners
|
|
123
|
+
* have been applied. This is useful for implementing middleware-like transformation chains.
|
|
124
|
+
*
|
|
125
|
+
* ### Usage
|
|
126
|
+
*
|
|
127
|
+
* ```typescript
|
|
128
|
+
* hooks.on("format:name", (name) => name.trim());
|
|
129
|
+
* hooks.on("format:name", (name) => name.toUpperCase());
|
|
130
|
+
*
|
|
131
|
+
* const result = hooks.alter("format:name", " john ");
|
|
132
|
+
* // result: "JOHN"
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* @typeParam Arg - The type of the input value
|
|
136
|
+
* @typeParam AlteredArg - The type of the returned value (can be different from input)
|
|
137
|
+
* @param event - The event name
|
|
138
|
+
* @param value - The initial value to transform
|
|
139
|
+
* @param args - Additional arguments passed to each listener
|
|
140
|
+
* @param callThis - The context (`this`) to use when calling listeners
|
|
141
|
+
* @returns The final transformed value after all listeners have been applied
|
|
93
142
|
*/
|
|
94
143
|
alter(event, value, args = [], callThis = null) {
|
|
95
144
|
const items = this.#listeners.get(event);
|
|
@@ -101,11 +150,34 @@ export class Hooks {
|
|
|
101
150
|
return value;
|
|
102
151
|
}
|
|
103
152
|
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
153
|
+
* Transform a value asynchronously by passing it through all registered listeners sequentially.
|
|
154
|
+
*
|
|
155
|
+
* Similar to `alter`, but each listener can be async and is awaited before the next listener
|
|
156
|
+
* is called. The value is transformed sequentially through the chain of async listeners.
|
|
157
|
+
* This is useful for async transformation pipelines like data validation, sanitization, or enrichment.
|
|
158
|
+
*
|
|
159
|
+
* ### Usage
|
|
160
|
+
*
|
|
161
|
+
* ```typescript
|
|
162
|
+
* hooks.on("process:data", async (data) => {
|
|
163
|
+
* return await validateData(data);
|
|
164
|
+
* });
|
|
165
|
+
*
|
|
166
|
+
* hooks.on("process:data", async (data) => {
|
|
167
|
+
* return await enrichData(data);
|
|
168
|
+
* });
|
|
169
|
+
*
|
|
170
|
+
* const result = await hooks.asyncAlter("process:data", rawData);
|
|
171
|
+
* // result: enriched and validated data
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* @typeParam Arg - The type of the input value
|
|
175
|
+
* @typeParam AlteredArg - The type of the returned value (can be different from input)
|
|
176
|
+
* @param event - The event name
|
|
177
|
+
* @param value - The initial value to transform
|
|
178
|
+
* @param args - Additional arguments passed to each listener
|
|
179
|
+
* @param callThis - The context (`this`) to use when calling listeners
|
|
180
|
+
* @returns A promise resolving to the final transformed value after all listeners have been applied
|
|
109
181
|
*/
|
|
110
182
|
async asyncAlter(event, value, args = [], callThis = null) {
|
|
111
183
|
const items = this.#listeners.get(event);
|
|
@@ -116,15 +188,117 @@ export class Hooks {
|
|
|
116
188
|
}
|
|
117
189
|
return value;
|
|
118
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Remove all registered listeners and clean up the hooks instance.
|
|
193
|
+
*
|
|
194
|
+
* This method clears all event listeners from the instance, effectively resetting it
|
|
195
|
+
* to an empty state. Useful for cleanup operations, testing, or when disposing of
|
|
196
|
+
* a Hooks instance.
|
|
197
|
+
*
|
|
198
|
+
* ### Usage
|
|
199
|
+
*
|
|
200
|
+
* ```typescript
|
|
201
|
+
* const hooks = new Hooks();
|
|
202
|
+
* hooks.on("event1", callback1);
|
|
203
|
+
* hooks.on("event2", callback2);
|
|
204
|
+
*
|
|
205
|
+
* hooks.destroy();
|
|
206
|
+
* // All listeners are now removed
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
119
209
|
destroy() {
|
|
120
210
|
this.#listeners.clear();
|
|
121
211
|
}
|
|
122
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Global singleton instance of the Hooks class.
|
|
215
|
+
*
|
|
216
|
+
* This instance provides a centralized event management system that can be used
|
|
217
|
+
* throughout an application without needing to create and pass around separate
|
|
218
|
+
* Hooks instances.
|
|
219
|
+
*
|
|
220
|
+
* ### Usage
|
|
221
|
+
*
|
|
222
|
+
* ```typescript
|
|
223
|
+
* import {hooks} from "@tsed/hooks";
|
|
224
|
+
*
|
|
225
|
+
* hooks.on("app:started", () => {
|
|
226
|
+
* console.log("Application started");
|
|
227
|
+
* });
|
|
228
|
+
*
|
|
229
|
+
* hooks.emit("app:started", []);
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* @public
|
|
233
|
+
*/
|
|
123
234
|
export const hooks = new Hooks();
|
|
235
|
+
/**
|
|
236
|
+
* Functional helper to register a hook listener on the global hooks instance.
|
|
237
|
+
*
|
|
238
|
+
* This is a convenience function bound to the global `hooks.on` method,
|
|
239
|
+
* allowing for a more concise functional programming style.
|
|
240
|
+
*
|
|
241
|
+
* @public
|
|
242
|
+
* @see {@link Hooks.on}
|
|
243
|
+
*/
|
|
124
244
|
export const $on = hooks.on.bind(hooks);
|
|
245
|
+
/**
|
|
246
|
+
* Functional helper to register a one-time hook listener on the global hooks instance.
|
|
247
|
+
*
|
|
248
|
+
* This is a convenience function bound to the global `hooks.once` method.
|
|
249
|
+
* The listener will be automatically removed after being invoked once.
|
|
250
|
+
*
|
|
251
|
+
* @public
|
|
252
|
+
* @see {@link Hooks.once}
|
|
253
|
+
*/
|
|
125
254
|
export const $once = hooks.once.bind(hooks);
|
|
255
|
+
/**
|
|
256
|
+
* Functional helper to remove hook listeners from the global hooks instance.
|
|
257
|
+
*
|
|
258
|
+
* This is a convenience function bound to the global `hooks.off` method,
|
|
259
|
+
* supporting removal by event name and callback, or by reference.
|
|
260
|
+
*
|
|
261
|
+
* @public
|
|
262
|
+
* @see {@link Hooks.off}
|
|
263
|
+
*/
|
|
126
264
|
export const $off = hooks.off.bind(hooks);
|
|
265
|
+
/**
|
|
266
|
+
* Functional helper to emit synchronous events on the global hooks instance.
|
|
267
|
+
*
|
|
268
|
+
* This is a convenience function bound to the global `hooks.emit` method,
|
|
269
|
+
* triggering all registered listeners for the specified event.
|
|
270
|
+
*
|
|
271
|
+
* @public
|
|
272
|
+
* @see {@link Hooks.emit}
|
|
273
|
+
*/
|
|
127
274
|
export const $emit = hooks.emit.bind(hooks);
|
|
275
|
+
/**
|
|
276
|
+
* Functional helper to emit asynchronous events on the global hooks instance.
|
|
277
|
+
*
|
|
278
|
+
* This is a convenience function bound to the global `hooks.asyncEmit` method,
|
|
279
|
+
* awaiting all listener promises in parallel.
|
|
280
|
+
*
|
|
281
|
+
* @public
|
|
282
|
+
* @see {@link Hooks.asyncEmit}
|
|
283
|
+
*/
|
|
128
284
|
export const $asyncEmit = hooks.asyncEmit.bind(hooks);
|
|
285
|
+
/**
|
|
286
|
+
* Functional helper to alter values through synchronous hooks on the global hooks instance.
|
|
287
|
+
*
|
|
288
|
+
* This is a convenience function bound to the global `hooks.alter` method,
|
|
289
|
+
* allowing listeners to transform a value sequentially.
|
|
290
|
+
*
|
|
291
|
+
* @public
|
|
292
|
+
* @see {@link Hooks.alter}
|
|
293
|
+
*/
|
|
129
294
|
export const $alter = hooks.alter.bind(hooks);
|
|
295
|
+
/**
|
|
296
|
+
* Functional helper to alter values through asynchronous hooks on the global hooks instance.
|
|
297
|
+
*
|
|
298
|
+
* This is a convenience function bound to the global `hooks.asyncAlter` method,
|
|
299
|
+
* allowing async listeners to transform a value sequentially.
|
|
300
|
+
*
|
|
301
|
+
* @public
|
|
302
|
+
* @see {@link Hooks.asyncAlter}
|
|
303
|
+
*/
|
|
130
304
|
export const $asyncAlter = hooks.asyncAlter.bind(hooks);
|
package/lib/types/Hooks.d.ts
CHANGED
|
@@ -1,99 +1,455 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A reference that can be used to identify and manage hook listeners.
|
|
3
|
+
*
|
|
4
|
+
* This type allows various kinds of values to be used as references when registering
|
|
5
|
+
* hook listeners, enabling flexible listener management and targeted event emission.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
1
9
|
export type HookRef = string | symbol | any | Function;
|
|
10
|
+
/**
|
|
11
|
+
* A function that handles hook events.
|
|
12
|
+
*
|
|
13
|
+
* Hook listeners are invoked when their associated event is emitted or altered,
|
|
14
|
+
* receiving the event arguments as parameters.
|
|
15
|
+
*
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
2
18
|
export type HookListener = Function;
|
|
19
|
+
/**
|
|
20
|
+
* A lightweight event management system for registering, emitting, and managing hook listeners.
|
|
21
|
+
*
|
|
22
|
+
* The Hooks class provides a flexible event system that supports:
|
|
23
|
+
* - Registering listeners with optional references for targeted management
|
|
24
|
+
* - One-time event listeners
|
|
25
|
+
* - Synchronous and asynchronous event emission
|
|
26
|
+
* - Value transformation through alter hooks
|
|
27
|
+
* - Listener removal by callback or reference
|
|
28
|
+
*
|
|
29
|
+
* ### Usage
|
|
30
|
+
*
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const hooks = new Hooks();
|
|
33
|
+
*
|
|
34
|
+
* // Register a listener
|
|
35
|
+
* hooks.on("user:created", (user) => {
|
|
36
|
+
* console.log("User created:", user);
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* // Emit an event
|
|
40
|
+
* hooks.emit("user:created", [{name: "John"}]);
|
|
41
|
+
*
|
|
42
|
+
* // Alter a value through hooks
|
|
43
|
+
* const modified = hooks.alter("transform:data", {value: 10}, []);
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
3
48
|
export declare class Hooks {
|
|
4
49
|
#private;
|
|
5
50
|
/**
|
|
6
|
-
* Check if an event has listeners
|
|
7
|
-
*
|
|
51
|
+
* Check if an event has registered listeners.
|
|
52
|
+
*
|
|
53
|
+
* @param event - The event name to check
|
|
54
|
+
* @returns `true` if the event has at least one listener, `false` otherwise
|
|
8
55
|
*/
|
|
9
56
|
has(event: string): boolean;
|
|
10
57
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
58
|
+
* Register a listener for a hook event with an optional reference.
|
|
59
|
+
*
|
|
60
|
+
* The reference can be used later to remove all listeners associated with it,
|
|
61
|
+
* or to emit events targeting only listeners with a specific reference.
|
|
62
|
+
*
|
|
63
|
+
* ### Usage
|
|
64
|
+
*
|
|
65
|
+
* ```typescript
|
|
66
|
+
* // Register a listener without reference
|
|
67
|
+
* hooks.on("user:created", (user) => {
|
|
68
|
+
* console.log("User created:", user);
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* // Register a listener with reference
|
|
72
|
+
* hooks.on("user:created", MyController, (user) => {
|
|
73
|
+
* console.log("Controller notified:", user);
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @param event - The event name to listen to
|
|
78
|
+
* @param ref - Optional reference to associate with this listener
|
|
79
|
+
* @param cb - The callback function to invoke when the event is emitted
|
|
80
|
+
* @returns The Hooks instance for method chaining
|
|
15
81
|
*/
|
|
16
82
|
on(event: string, ref: HookRef, cb: HookListener): this;
|
|
17
83
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @param
|
|
84
|
+
* Register a listener for a hook event.
|
|
85
|
+
*
|
|
86
|
+
* @param event - The event name to listen to
|
|
87
|
+
* @param cb - The callback function to invoke when the event is emitted
|
|
88
|
+
* @returns The Hooks instance for method chaining
|
|
21
89
|
*/
|
|
22
90
|
on(event: string, cb: HookListener): this;
|
|
23
91
|
/**
|
|
24
|
-
*
|
|
92
|
+
* Register a one-time listener for a hook event with an optional reference.
|
|
93
|
+
*
|
|
94
|
+
* The listener will be automatically removed after being invoked once.
|
|
95
|
+
* This is useful for handling events that should only trigger a single action.
|
|
96
|
+
*
|
|
97
|
+
* ### Usage
|
|
98
|
+
*
|
|
99
|
+
* ```typescript
|
|
100
|
+
* // Listen once without reference
|
|
101
|
+
* hooks.once("app:ready", () => {
|
|
102
|
+
* console.log("App is ready!");
|
|
103
|
+
* });
|
|
25
104
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
105
|
+
* // Listen once with reference
|
|
106
|
+
* hooks.once("initialization:complete", MyService, () => {
|
|
107
|
+
* console.log("Service initialized");
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @param event - The event name to listen to
|
|
112
|
+
* @param ref - Optional reference to associate with this listener
|
|
113
|
+
* @param cb - The callback function to invoke when the event is emitted
|
|
114
|
+
* @returns The Hooks instance for method chaining
|
|
29
115
|
*/
|
|
30
116
|
once(event: string, ref: HookRef, cb: HookListener): this;
|
|
31
117
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
118
|
+
* Register a one-time listener for a hook event.
|
|
119
|
+
*
|
|
120
|
+
* The listener will be automatically removed after being invoked once.
|
|
121
|
+
*
|
|
122
|
+
* @param event - The event name to listen to
|
|
123
|
+
* @param cb - The callback function to invoke when the event is emitted
|
|
124
|
+
* @returns The Hooks instance for method chaining
|
|
35
125
|
*/
|
|
36
126
|
once(event: string, cb: HookListener): this;
|
|
37
127
|
/**
|
|
38
|
-
* Remove
|
|
39
|
-
*
|
|
128
|
+
* Remove all listeners associated with a specific reference.
|
|
129
|
+
*
|
|
130
|
+
* This will remove all listeners across all events that were registered
|
|
131
|
+
* with the given reference, regardless of the event name.
|
|
132
|
+
*
|
|
133
|
+
* ### Usage
|
|
134
|
+
*
|
|
135
|
+
* ```typescript
|
|
136
|
+
* // Register listeners with a reference
|
|
137
|
+
* hooks.on("event1", MyController, callback1);
|
|
138
|
+
* hooks.on("event2", MyController, callback2);
|
|
139
|
+
*
|
|
140
|
+
* // Remove all listeners for MyController
|
|
141
|
+
* hooks.off(MyController);
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* @param ref - The reference to remove all associated listeners
|
|
145
|
+
* @returns The Hooks instance for method chaining
|
|
40
146
|
*/
|
|
41
147
|
off(ref: HookRef): this;
|
|
42
148
|
/**
|
|
43
|
-
* Remove a listener
|
|
44
|
-
*
|
|
45
|
-
*
|
|
149
|
+
* Remove a specific listener from an event.
|
|
150
|
+
*
|
|
151
|
+
* This will only remove the exact callback function from the specified event.
|
|
152
|
+
*
|
|
153
|
+
* ### Usage
|
|
154
|
+
*
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const callback = (data) => console.log(data);
|
|
157
|
+
* hooks.on("my:event", callback);
|
|
158
|
+
*
|
|
159
|
+
* // Remove the specific callback
|
|
160
|
+
* hooks.off("my:event", callback);
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* @param event - The event name
|
|
164
|
+
* @param cb - The callback function to remove
|
|
165
|
+
* @returns The Hooks instance for method chaining
|
|
46
166
|
*/
|
|
47
167
|
off(event: string, cb: HookListener): this;
|
|
48
168
|
/**
|
|
49
|
-
* Trigger an event
|
|
50
|
-
*
|
|
169
|
+
* Trigger an event and invoke all registered listeners synchronously.
|
|
170
|
+
*
|
|
171
|
+
* This method calls all listeners registered for the specified event without any arguments.
|
|
172
|
+
*
|
|
173
|
+
* ### Usage
|
|
174
|
+
*
|
|
175
|
+
* ```typescript
|
|
176
|
+
* hooks.on("app:started", () => console.log("App started"));
|
|
177
|
+
* hooks.emit("app:started");
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @param event - The event name to emit
|
|
51
181
|
*/
|
|
52
182
|
emit(event: string): void;
|
|
53
183
|
/**
|
|
54
|
-
* Trigger an event and
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
184
|
+
* Trigger an event and invoke all registered listeners with arguments.
|
|
185
|
+
*
|
|
186
|
+
* All listeners for the event will be called synchronously with the provided arguments.
|
|
187
|
+
*
|
|
188
|
+
* ### Usage
|
|
189
|
+
*
|
|
190
|
+
* ```typescript
|
|
191
|
+
* hooks.on("user:created", (user, timestamp) => {
|
|
192
|
+
* console.log("User created:", user, "at", timestamp);
|
|
193
|
+
* });
|
|
194
|
+
*
|
|
195
|
+
* hooks.emit("user:created", [{name: "John"}, Date.now()]);
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* @param event - The event name to emit
|
|
199
|
+
* @param args - Array of arguments to pass to the listeners
|
|
58
200
|
*/
|
|
59
201
|
emit(event: string, args: unknown[]): void;
|
|
60
202
|
/**
|
|
61
|
-
* Trigger an event
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
203
|
+
* Trigger an event and invoke only listeners attached to a specific reference.
|
|
204
|
+
*
|
|
205
|
+
* This allows targeted event emission to listeners registered with a particular reference,
|
|
206
|
+
* while ignoring listeners without that reference.
|
|
207
|
+
*
|
|
208
|
+
* ### Usage
|
|
209
|
+
*
|
|
210
|
+
* ```typescript
|
|
211
|
+
* hooks.on("data:update", MyController, (data) => {
|
|
212
|
+
* console.log("Controller notified:", data);
|
|
213
|
+
* });
|
|
214
|
+
*
|
|
215
|
+
* // Only triggers listeners with MyController reference
|
|
216
|
+
* hooks.emit("data:update", MyController, [{id: 1}]);
|
|
217
|
+
* ```
|
|
218
|
+
*
|
|
219
|
+
* @param event - The event name to emit
|
|
220
|
+
* @param ref - The reference to target specific listeners
|
|
221
|
+
* @param args - Optional array of arguments to pass to the listeners
|
|
65
222
|
*/
|
|
66
223
|
emit(event: string, ref: HookRef, args?: unknown[]): void;
|
|
67
224
|
/**
|
|
68
|
-
* Trigger an event and
|
|
69
|
-
*
|
|
225
|
+
* Trigger an event and invoke all registered listeners asynchronously.
|
|
226
|
+
*
|
|
227
|
+
* All listeners are called in parallel and the method waits for all promises to resolve.
|
|
228
|
+
* This is useful when listeners perform async operations like database queries or API calls.
|
|
229
|
+
*
|
|
230
|
+
* ### Usage
|
|
231
|
+
*
|
|
232
|
+
* ```typescript
|
|
233
|
+
* hooks.on("user:created", async (user) => {
|
|
234
|
+
* await sendWelcomeEmail(user);
|
|
235
|
+
* });
|
|
236
|
+
*
|
|
237
|
+
* await hooks.asyncEmit("user:created");
|
|
238
|
+
* ```
|
|
239
|
+
*
|
|
240
|
+
* @param event - The event name to emit
|
|
241
|
+
* @returns A promise that resolves when all listeners have completed
|
|
70
242
|
*/
|
|
71
243
|
asyncEmit(event: string): Promise<void>;
|
|
244
|
+
/**
|
|
245
|
+
* Trigger an event and invoke all registered listeners asynchronously with arguments.
|
|
246
|
+
*
|
|
247
|
+
* All listeners are called in parallel with the provided arguments,
|
|
248
|
+
* and the method waits for all promises to resolve.
|
|
249
|
+
*
|
|
250
|
+
* ### Usage
|
|
251
|
+
*
|
|
252
|
+
* ```typescript
|
|
253
|
+
* hooks.on("order:created", async (order, userId) => {
|
|
254
|
+
* await notifyUser(userId, order);
|
|
255
|
+
* });
|
|
256
|
+
*
|
|
257
|
+
* await hooks.asyncEmit("order:created", [orderData, 123]);
|
|
258
|
+
* ```
|
|
259
|
+
*
|
|
260
|
+
* @param event - The event name to emit
|
|
261
|
+
* @param args - Array of arguments to pass to the listeners
|
|
262
|
+
* @returns A promise that resolves when all listeners have completed
|
|
263
|
+
*/
|
|
72
264
|
asyncEmit(event: string, args: unknown[]): Promise<void>;
|
|
265
|
+
/**
|
|
266
|
+
* Trigger an event asynchronously for listeners with a specific reference.
|
|
267
|
+
*
|
|
268
|
+
* Only listeners registered with the specified reference will be invoked.
|
|
269
|
+
* All matching listeners are called in parallel.
|
|
270
|
+
*
|
|
271
|
+
* ### Usage
|
|
272
|
+
*
|
|
273
|
+
* ```typescript
|
|
274
|
+
* hooks.on("data:sync", MyService, async (data) => {
|
|
275
|
+
* await syncToDatabase(data);
|
|
276
|
+
* });
|
|
277
|
+
*
|
|
278
|
+
* await hooks.asyncEmit("data:sync", MyService, [newData]);
|
|
279
|
+
* ```
|
|
280
|
+
*
|
|
281
|
+
* @param event - The event name to emit
|
|
282
|
+
* @param ref - The reference to target specific listeners
|
|
283
|
+
* @param args - Optional array of arguments to pass to the listeners
|
|
284
|
+
* @returns A promise that resolves when all listeners have completed
|
|
285
|
+
*/
|
|
73
286
|
asyncEmit(event: string, ref: HookRef, args?: unknown[]): Promise<void>;
|
|
74
287
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
288
|
+
* Transform a value by passing it through all registered listeners sequentially.
|
|
289
|
+
*
|
|
290
|
+
* Each listener receives the value (potentially modified by previous listeners) and returns
|
|
291
|
+
* a new or modified value. The final transformed value is returned after all listeners
|
|
292
|
+
* have been applied. This is useful for implementing middleware-like transformation chains.
|
|
293
|
+
*
|
|
294
|
+
* ### Usage
|
|
295
|
+
*
|
|
296
|
+
* ```typescript
|
|
297
|
+
* hooks.on("format:name", (name) => name.trim());
|
|
298
|
+
* hooks.on("format:name", (name) => name.toUpperCase());
|
|
299
|
+
*
|
|
300
|
+
* const result = hooks.alter("format:name", " john ");
|
|
301
|
+
* // result: "JOHN"
|
|
302
|
+
* ```
|
|
303
|
+
*
|
|
304
|
+
* @typeParam Arg - The type of the input value
|
|
305
|
+
* @typeParam AlteredArg - The type of the returned value (can be different from input)
|
|
306
|
+
* @param event - The event name
|
|
307
|
+
* @param value - The initial value to transform
|
|
308
|
+
* @param args - Additional arguments passed to each listener
|
|
309
|
+
* @param callThis - The context (`this`) to use when calling listeners
|
|
310
|
+
* @returns The final transformed value after all listeners have been applied
|
|
80
311
|
*/
|
|
81
312
|
alter<Arg = unknown, AlteredArg = Arg>(event: string, value: Arg, args?: unknown[], callThis?: unknown): AlteredArg;
|
|
82
313
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
314
|
+
* Transform a value asynchronously by passing it through all registered listeners sequentially.
|
|
315
|
+
*
|
|
316
|
+
* Similar to `alter`, but each listener can be async and is awaited before the next listener
|
|
317
|
+
* is called. The value is transformed sequentially through the chain of async listeners.
|
|
318
|
+
* This is useful for async transformation pipelines like data validation, sanitization, or enrichment.
|
|
319
|
+
*
|
|
320
|
+
* ### Usage
|
|
321
|
+
*
|
|
322
|
+
* ```typescript
|
|
323
|
+
* hooks.on("process:data", async (data) => {
|
|
324
|
+
* return await validateData(data);
|
|
325
|
+
* });
|
|
326
|
+
*
|
|
327
|
+
* hooks.on("process:data", async (data) => {
|
|
328
|
+
* return await enrichData(data);
|
|
329
|
+
* });
|
|
330
|
+
*
|
|
331
|
+
* const result = await hooks.asyncAlter("process:data", rawData);
|
|
332
|
+
* // result: enriched and validated data
|
|
333
|
+
* ```
|
|
334
|
+
*
|
|
335
|
+
* @typeParam Arg - The type of the input value
|
|
336
|
+
* @typeParam AlteredArg - The type of the returned value (can be different from input)
|
|
337
|
+
* @param event - The event name
|
|
338
|
+
* @param value - The initial value to transform
|
|
339
|
+
* @param args - Additional arguments passed to each listener
|
|
340
|
+
* @param callThis - The context (`this`) to use when calling listeners
|
|
341
|
+
* @returns A promise resolving to the final transformed value after all listeners have been applied
|
|
88
342
|
*/
|
|
89
343
|
asyncAlter<Arg = unknown, AlteredArg = Arg>(event: string, value: Arg, args?: unknown[], callThis?: unknown): Promise<AlteredArg>;
|
|
344
|
+
/**
|
|
345
|
+
* Remove all registered listeners and clean up the hooks instance.
|
|
346
|
+
*
|
|
347
|
+
* This method clears all event listeners from the instance, effectively resetting it
|
|
348
|
+
* to an empty state. Useful for cleanup operations, testing, or when disposing of
|
|
349
|
+
* a Hooks instance.
|
|
350
|
+
*
|
|
351
|
+
* ### Usage
|
|
352
|
+
*
|
|
353
|
+
* ```typescript
|
|
354
|
+
* const hooks = new Hooks();
|
|
355
|
+
* hooks.on("event1", callback1);
|
|
356
|
+
* hooks.on("event2", callback2);
|
|
357
|
+
*
|
|
358
|
+
* hooks.destroy();
|
|
359
|
+
* // All listeners are now removed
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
90
362
|
destroy(): void;
|
|
91
363
|
}
|
|
364
|
+
/**
|
|
365
|
+
* Global singleton instance of the Hooks class.
|
|
366
|
+
*
|
|
367
|
+
* This instance provides a centralized event management system that can be used
|
|
368
|
+
* throughout an application without needing to create and pass around separate
|
|
369
|
+
* Hooks instances.
|
|
370
|
+
*
|
|
371
|
+
* ### Usage
|
|
372
|
+
*
|
|
373
|
+
* ```typescript
|
|
374
|
+
* import {hooks} from "@tsed/hooks";
|
|
375
|
+
*
|
|
376
|
+
* hooks.on("app:started", () => {
|
|
377
|
+
* console.log("Application started");
|
|
378
|
+
* });
|
|
379
|
+
*
|
|
380
|
+
* hooks.emit("app:started", []);
|
|
381
|
+
* ```
|
|
382
|
+
*
|
|
383
|
+
* @public
|
|
384
|
+
*/
|
|
92
385
|
export declare const hooks: Hooks;
|
|
386
|
+
/**
|
|
387
|
+
* Functional helper to register a hook listener on the global hooks instance.
|
|
388
|
+
*
|
|
389
|
+
* This is a convenience function bound to the global `hooks.on` method,
|
|
390
|
+
* allowing for a more concise functional programming style.
|
|
391
|
+
*
|
|
392
|
+
* @public
|
|
393
|
+
* @see {@link Hooks.on}
|
|
394
|
+
*/
|
|
93
395
|
export declare const $on: typeof hooks.on;
|
|
396
|
+
/**
|
|
397
|
+
* Functional helper to register a one-time hook listener on the global hooks instance.
|
|
398
|
+
*
|
|
399
|
+
* This is a convenience function bound to the global `hooks.once` method.
|
|
400
|
+
* The listener will be automatically removed after being invoked once.
|
|
401
|
+
*
|
|
402
|
+
* @public
|
|
403
|
+
* @see {@link Hooks.once}
|
|
404
|
+
*/
|
|
94
405
|
export declare const $once: typeof hooks.once;
|
|
406
|
+
/**
|
|
407
|
+
* Functional helper to remove hook listeners from the global hooks instance.
|
|
408
|
+
*
|
|
409
|
+
* This is a convenience function bound to the global `hooks.off` method,
|
|
410
|
+
* supporting removal by event name and callback, or by reference.
|
|
411
|
+
*
|
|
412
|
+
* @public
|
|
413
|
+
* @see {@link Hooks.off}
|
|
414
|
+
*/
|
|
95
415
|
export declare const $off: typeof hooks.off;
|
|
416
|
+
/**
|
|
417
|
+
* Functional helper to emit synchronous events on the global hooks instance.
|
|
418
|
+
*
|
|
419
|
+
* This is a convenience function bound to the global `hooks.emit` method,
|
|
420
|
+
* triggering all registered listeners for the specified event.
|
|
421
|
+
*
|
|
422
|
+
* @public
|
|
423
|
+
* @see {@link Hooks.emit}
|
|
424
|
+
*/
|
|
96
425
|
export declare const $emit: typeof hooks.emit;
|
|
426
|
+
/**
|
|
427
|
+
* Functional helper to emit asynchronous events on the global hooks instance.
|
|
428
|
+
*
|
|
429
|
+
* This is a convenience function bound to the global `hooks.asyncEmit` method,
|
|
430
|
+
* awaiting all listener promises in parallel.
|
|
431
|
+
*
|
|
432
|
+
* @public
|
|
433
|
+
* @see {@link Hooks.asyncEmit}
|
|
434
|
+
*/
|
|
97
435
|
export declare const $asyncEmit: typeof hooks.asyncEmit;
|
|
436
|
+
/**
|
|
437
|
+
* Functional helper to alter values through synchronous hooks on the global hooks instance.
|
|
438
|
+
*
|
|
439
|
+
* This is a convenience function bound to the global `hooks.alter` method,
|
|
440
|
+
* allowing listeners to transform a value sequentially.
|
|
441
|
+
*
|
|
442
|
+
* @public
|
|
443
|
+
* @see {@link Hooks.alter}
|
|
444
|
+
*/
|
|
98
445
|
export declare const $alter: typeof hooks.alter;
|
|
446
|
+
/**
|
|
447
|
+
* Functional helper to alter values through asynchronous hooks on the global hooks instance.
|
|
448
|
+
*
|
|
449
|
+
* This is a convenience function bound to the global `hooks.asyncAlter` method,
|
|
450
|
+
* allowing async listeners to transform a value sequentially.
|
|
451
|
+
*
|
|
452
|
+
* @public
|
|
453
|
+
* @see {@link Hooks.asyncAlter}
|
|
454
|
+
*/
|
|
99
455
|
export declare const $asyncAlter: typeof hooks.asyncAlter;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@tsed/hooks",
|
|
3
3
|
"description": "Hooks module for Ts.ED Framework",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.19.
|
|
5
|
+
"version": "8.19.3",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@tsed/monorepo-utils": "3.0.0",
|
|
30
|
-
"@tsed/typescript": "8.19.
|
|
31
|
-
"@tsed/vitest": "8.19.
|
|
30
|
+
"@tsed/typescript": "8.19.3",
|
|
31
|
+
"@tsed/vitest": "8.19.3",
|
|
32
32
|
"eslint": "9.12.0",
|
|
33
33
|
"typescript": "5.9.3",
|
|
34
34
|
"vite": "7.1.11",
|