@adonisjs/events 10.1.0-next.0 → 10.1.0-next.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/README.md +2 -2
- package/build/{chunk-26H7HMEN.js → chunk-R74HJGSQ.js} +119 -3
- package/build/factories/emitter.d.ts +1 -1
- package/build/factories/main.d.ts +1 -1
- package/build/factories/main.js +1 -1
- package/build/index.d.ts +3 -2
- package/build/index.js +15 -3
- package/build/src/base_event.d.ts +11 -1
- package/build/src/emitter.d.ts +55 -2
- package/build/src/events_buffer.d.ts +31 -2
- package/build/src/tracing_channels.d.ts +6 -0
- package/build/src/types.d.ts +43 -3
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -21,8 +21,8 @@ In order to ensure that the AdonisJS community is welcoming to all, please revie
|
|
|
21
21
|
## License
|
|
22
22
|
AdonisJS events is open-sourced software licensed under the [MIT license](LICENSE.md).
|
|
23
23
|
|
|
24
|
-
[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/events/
|
|
25
|
-
[gh-workflow-url]: https://github.com/adonisjs/events/actions/workflows/
|
|
24
|
+
[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/events/checks.yml?style=for-the-badge
|
|
25
|
+
[gh-workflow-url]: https://github.com/adonisjs/events/actions/workflows/checks.yml "Github action"
|
|
26
26
|
|
|
27
27
|
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
|
|
28
28
|
[typescript-url]: "typescript"
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/tracing_channels.ts
|
|
8
|
+
var tracing_channels_exports = {};
|
|
9
|
+
__export(tracing_channels_exports, {
|
|
10
|
+
eventDispatch: () => eventDispatch
|
|
11
|
+
});
|
|
12
|
+
import diagnostics_channel from "diagnostics_channel";
|
|
13
|
+
var eventDispatch = diagnostics_channel.tracingChannel("adonisjs.event.dispatch");
|
|
14
|
+
|
|
1
15
|
// src/emitter.ts
|
|
2
16
|
import is2 from "@sindresorhus/is";
|
|
3
17
|
import Emittery from "emittery";
|
|
@@ -18,30 +32,45 @@ var EventsBuffer = class {
|
|
|
18
32
|
#events = [];
|
|
19
33
|
/**
|
|
20
34
|
* Track emitted event
|
|
35
|
+
*
|
|
36
|
+
* @param event - The event that was emitted
|
|
37
|
+
* @param data - The data passed with the event
|
|
21
38
|
*/
|
|
22
39
|
add(event, data) {
|
|
23
40
|
this.#events.push({ event, data });
|
|
24
41
|
}
|
|
25
42
|
/**
|
|
26
43
|
* Get all the emitted events
|
|
44
|
+
*
|
|
45
|
+
* @returns Array of all buffered events
|
|
27
46
|
*/
|
|
28
47
|
all() {
|
|
29
48
|
return this.#events;
|
|
30
49
|
}
|
|
31
50
|
/**
|
|
32
51
|
* Returns the size of captured events
|
|
52
|
+
*
|
|
53
|
+
* @returns The number of captured events
|
|
33
54
|
*/
|
|
34
55
|
size() {
|
|
35
56
|
return this.#events.length;
|
|
36
57
|
}
|
|
37
58
|
/**
|
|
38
59
|
* Find if an event was emitted
|
|
60
|
+
*
|
|
61
|
+
* @param event - The event to check for
|
|
62
|
+
* @param finder - Optional callback to filter specific event instances
|
|
63
|
+
* @returns True if the event was emitted
|
|
39
64
|
*/
|
|
40
65
|
exists(event, finder) {
|
|
41
66
|
return !!this.find(event, finder);
|
|
42
67
|
}
|
|
43
68
|
/**
|
|
44
69
|
* Find a specific event
|
|
70
|
+
*
|
|
71
|
+
* @param event - The event to find
|
|
72
|
+
* @param finder - Optional callback to filter specific event instances
|
|
73
|
+
* @returns The found event or null
|
|
45
74
|
*/
|
|
46
75
|
find(event, finder) {
|
|
47
76
|
return this.#events.find((bufferedEvent) => {
|
|
@@ -53,6 +82,10 @@ var EventsBuffer = class {
|
|
|
53
82
|
}
|
|
54
83
|
/**
|
|
55
84
|
* Assert a given event has been emitted
|
|
85
|
+
*
|
|
86
|
+
* @param event - The event to assert was emitted
|
|
87
|
+
* @param finder - Optional callback to filter specific event instances
|
|
88
|
+
* @throws AssertionError if the event was not emitted
|
|
56
89
|
*/
|
|
57
90
|
assertEmitted(event, finder) {
|
|
58
91
|
const hasEvent = this.exists(event, finder);
|
|
@@ -69,6 +102,10 @@ var EventsBuffer = class {
|
|
|
69
102
|
}
|
|
70
103
|
/**
|
|
71
104
|
* Assert number of times an event has been emitted
|
|
105
|
+
*
|
|
106
|
+
* @param event - The event to check emission count for
|
|
107
|
+
* @param count - The expected number of emissions
|
|
108
|
+
* @throws AssertionError if the count doesn't match
|
|
72
109
|
*/
|
|
73
110
|
assertEmittedCount(event, count) {
|
|
74
111
|
const actual = this.all().filter((bufferedEvent) => bufferedEvent.event === event).length;
|
|
@@ -86,6 +123,10 @@ var EventsBuffer = class {
|
|
|
86
123
|
}
|
|
87
124
|
/**
|
|
88
125
|
* Assert a given event has been not been emitted
|
|
126
|
+
*
|
|
127
|
+
* @param event - The event to assert was not emitted
|
|
128
|
+
* @param finder - Optional callback to filter specific event instances
|
|
129
|
+
* @throws AssertionError if the event was emitted
|
|
89
130
|
*/
|
|
90
131
|
assertNotEmitted(event, finder) {
|
|
91
132
|
const hasEvent = this.exists(event, finder);
|
|
@@ -102,7 +143,9 @@ var EventsBuffer = class {
|
|
|
102
143
|
}
|
|
103
144
|
}
|
|
104
145
|
/**
|
|
105
|
-
* Assert
|
|
146
|
+
* Assert no events have been emitted
|
|
147
|
+
*
|
|
148
|
+
* @throws AssertionError if any events were emitted
|
|
106
149
|
*/
|
|
107
150
|
assertNoneEmitted() {
|
|
108
151
|
const eventsSize = this.size();
|
|
@@ -175,21 +218,34 @@ var Emitter = class {
|
|
|
175
218
|
*
|
|
176
219
|
* The listeners map key is the original binding listener
|
|
177
220
|
* and the value is a callback function.
|
|
221
|
+
*
|
|
222
|
+
* @returns The events listeners map
|
|
178
223
|
*/
|
|
179
224
|
get eventsListeners() {
|
|
180
225
|
return this.#eventsListeners;
|
|
181
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* Creates a new Emitter instance
|
|
229
|
+
*
|
|
230
|
+
* @param app - The AdonisJS application instance
|
|
231
|
+
*/
|
|
182
232
|
constructor(app) {
|
|
183
233
|
this.#app = app;
|
|
184
234
|
}
|
|
185
235
|
/**
|
|
186
236
|
* Check if the value is a constructor and narrow down its types
|
|
237
|
+
*
|
|
238
|
+
* @param value - The value to check
|
|
239
|
+
* @returns True if the value is a constructor
|
|
187
240
|
*/
|
|
188
241
|
#isConstructor(value) {
|
|
189
242
|
return is2.class(value);
|
|
190
243
|
}
|
|
191
244
|
/**
|
|
192
245
|
* Returns the symbol for a class based event.
|
|
246
|
+
*
|
|
247
|
+
* @param event - The event class constructor
|
|
248
|
+
* @returns The symbol for the event class
|
|
193
249
|
*/
|
|
194
250
|
#getEventClassSymbol(event) {
|
|
195
251
|
if (!this.#eventsClassSymbols.has(event)) {
|
|
@@ -200,6 +256,9 @@ var Emitter = class {
|
|
|
200
256
|
/**
|
|
201
257
|
* Normalizes the event to emittery supported data types. The class
|
|
202
258
|
* constructors are cached against a unique symbol.
|
|
259
|
+
*
|
|
260
|
+
* @param event - The event to resolve
|
|
261
|
+
* @returns The resolved event as string, symbol, or number
|
|
203
262
|
*/
|
|
204
263
|
#resolveEvent(event) {
|
|
205
264
|
if (this.#isConstructor(event)) {
|
|
@@ -209,6 +268,9 @@ var Emitter = class {
|
|
|
209
268
|
}
|
|
210
269
|
/**
|
|
211
270
|
* Returns the event listeners map
|
|
271
|
+
*
|
|
272
|
+
* @param event - The event to get listeners for
|
|
273
|
+
* @returns The listeners map for the event
|
|
212
274
|
*/
|
|
213
275
|
#getEventListeners(event) {
|
|
214
276
|
if (!this.#eventsListeners.has(event)) {
|
|
@@ -219,6 +281,9 @@ var Emitter = class {
|
|
|
219
281
|
/**
|
|
220
282
|
* Normalizes the event listener to a function that can be passed to
|
|
221
283
|
* emittery.
|
|
284
|
+
*
|
|
285
|
+
* @param listener - The listener to normalize
|
|
286
|
+
* @returns The normalized listener method
|
|
222
287
|
*/
|
|
223
288
|
#normalizeEventListener(listener) {
|
|
224
289
|
if (typeof listener === "string") {
|
|
@@ -242,6 +307,10 @@ var Emitter = class {
|
|
|
242
307
|
/**
|
|
243
308
|
* Resolves the event listener either from the cache or normalizes
|
|
244
309
|
* it and stores it inside the cache
|
|
310
|
+
*
|
|
311
|
+
* @param event - The event to resolve listener for
|
|
312
|
+
* @param listener - The listener to resolve
|
|
313
|
+
* @returns The resolved listener method
|
|
245
314
|
*/
|
|
246
315
|
#resolveEventListener(event, listener) {
|
|
247
316
|
const eventListeners = this.#getEventListeners(event);
|
|
@@ -252,6 +321,9 @@ var Emitter = class {
|
|
|
252
321
|
}
|
|
253
322
|
/**
|
|
254
323
|
* Register a global error handler
|
|
324
|
+
*
|
|
325
|
+
* @param callback - The error handler callback
|
|
326
|
+
* @returns The emitter instance for method chaining
|
|
255
327
|
*/
|
|
256
328
|
onError(callback) {
|
|
257
329
|
this.#errorHandler = callback;
|
|
@@ -261,6 +333,9 @@ var Emitter = class {
|
|
|
261
333
|
* Bind multiple listeners to listen for a single event. The listen
|
|
262
334
|
* method is a convenience helper to be used with class based
|
|
263
335
|
* events and listeners.
|
|
336
|
+
*
|
|
337
|
+
* @param event - The event class to listen for
|
|
338
|
+
* @param listeners - Array of listener classes with handle methods
|
|
264
339
|
*/
|
|
265
340
|
listen(event, listeners) {
|
|
266
341
|
listeners.forEach((listener) => this.on(event, [listener, "handle"]));
|
|
@@ -296,6 +371,9 @@ var Emitter = class {
|
|
|
296
371
|
/**
|
|
297
372
|
* Attach a listener to listen for all the events. Wildcard listeners
|
|
298
373
|
* can only be defined as inline callbacks.
|
|
374
|
+
*
|
|
375
|
+
* @param listener - The wildcard listener callback
|
|
376
|
+
* @returns The unsubscribe function
|
|
299
377
|
*/
|
|
300
378
|
onAny(listener) {
|
|
301
379
|
return this.#transport.onAny(listener);
|
|
@@ -308,7 +386,16 @@ var Emitter = class {
|
|
|
308
386
|
}
|
|
309
387
|
try {
|
|
310
388
|
const normalizedEvent = this.#resolveEvent(event);
|
|
311
|
-
await
|
|
389
|
+
await eventDispatch.tracePromise(
|
|
390
|
+
this.#transport.emit,
|
|
391
|
+
eventDispatch.hasSubscribers ? {
|
|
392
|
+
event,
|
|
393
|
+
data
|
|
394
|
+
} : void 0,
|
|
395
|
+
this.#transport,
|
|
396
|
+
normalizedEvent,
|
|
397
|
+
data
|
|
398
|
+
);
|
|
312
399
|
} catch (error) {
|
|
313
400
|
if (this.#errorHandler) {
|
|
314
401
|
this.#errorHandler(event, error, data);
|
|
@@ -325,7 +412,16 @@ var Emitter = class {
|
|
|
325
412
|
}
|
|
326
413
|
try {
|
|
327
414
|
const normalizedEvent = this.#resolveEvent(event);
|
|
328
|
-
await
|
|
415
|
+
await eventDispatch.tracePromise(
|
|
416
|
+
this.#transport.emitSerial,
|
|
417
|
+
eventDispatch.hasSubscribers ? {
|
|
418
|
+
event,
|
|
419
|
+
data
|
|
420
|
+
} : void 0,
|
|
421
|
+
this.#transport,
|
|
422
|
+
normalizedEvent,
|
|
423
|
+
data
|
|
424
|
+
);
|
|
329
425
|
} catch (error) {
|
|
330
426
|
if (this.#errorHandler) {
|
|
331
427
|
this.#errorHandler(event, error, data);
|
|
@@ -336,6 +432,9 @@ var Emitter = class {
|
|
|
336
432
|
}
|
|
337
433
|
/**
|
|
338
434
|
* Remove a specific listener for an event
|
|
435
|
+
*
|
|
436
|
+
* @param event - The event to remove listener from
|
|
437
|
+
* @param listener - The listener to remove
|
|
339
438
|
*/
|
|
340
439
|
off(event, listener) {
|
|
341
440
|
if (debug_default.enabled) {
|
|
@@ -352,6 +451,9 @@ var Emitter = class {
|
|
|
352
451
|
}
|
|
353
452
|
/**
|
|
354
453
|
* Remove a specific listener listening for all the events
|
|
454
|
+
*
|
|
455
|
+
* @param listener - The wildcard listener to remove
|
|
456
|
+
* @returns The emitter instance for method chaining
|
|
355
457
|
*/
|
|
356
458
|
offAny(listener) {
|
|
357
459
|
this.#transport.offAny(listener);
|
|
@@ -361,12 +463,16 @@ var Emitter = class {
|
|
|
361
463
|
* Remove a specific listener for an event
|
|
362
464
|
*
|
|
363
465
|
* @alias "off"
|
|
466
|
+
* @param event - The event to remove listener from
|
|
467
|
+
* @param listener - The listener to remove
|
|
364
468
|
*/
|
|
365
469
|
clearListener(event, listener) {
|
|
366
470
|
return this.off(event, listener);
|
|
367
471
|
}
|
|
368
472
|
/**
|
|
369
473
|
* Clear all listeners for a specific event
|
|
474
|
+
*
|
|
475
|
+
* @param event - The event to clear listeners for
|
|
370
476
|
*/
|
|
371
477
|
clearListeners(event) {
|
|
372
478
|
debug_default("clearing all listeners for event %O", event);
|
|
@@ -383,12 +489,18 @@ var Emitter = class {
|
|
|
383
489
|
}
|
|
384
490
|
/**
|
|
385
491
|
* Get count of listeners for a given event or all the events
|
|
492
|
+
*
|
|
493
|
+
* @param event - The event to count listeners for (optional)
|
|
494
|
+
* @returns The number of listeners
|
|
386
495
|
*/
|
|
387
496
|
listenerCount(event) {
|
|
388
497
|
return this.#transport.listenerCount(event ? this.#resolveEvent(event) : void 0);
|
|
389
498
|
}
|
|
390
499
|
/**
|
|
391
500
|
* Find if an event has one or more listeners
|
|
501
|
+
*
|
|
502
|
+
* @param event - The event to check listeners for (optional)
|
|
503
|
+
* @returns True if the event has listeners
|
|
392
504
|
*/
|
|
393
505
|
hasListeners(event) {
|
|
394
506
|
return this.listenerCount(event) > 0;
|
|
@@ -402,6 +514,9 @@ var Emitter = class {
|
|
|
402
514
|
*
|
|
403
515
|
* Calling this method one than once drops the existing fakes and
|
|
404
516
|
* creates new one.
|
|
517
|
+
*
|
|
518
|
+
* @param events - Array of events to fake (optional, defaults to all events)
|
|
519
|
+
* @returns The events buffer for assertions
|
|
405
520
|
*/
|
|
406
521
|
fake(events) {
|
|
407
522
|
this.restore();
|
|
@@ -427,5 +542,6 @@ var Emitter = class {
|
|
|
427
542
|
};
|
|
428
543
|
|
|
429
544
|
export {
|
|
545
|
+
tracing_channels_exports,
|
|
430
546
|
Emitter
|
|
431
547
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { EmitterFactory } from './emitter.
|
|
1
|
+
export { EmitterFactory } from './emitter.ts';
|
package/build/factories/main.js
CHANGED
package/build/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export { Emitter } from './src/emitter.
|
|
2
|
-
export { BaseEvent } from './src/base_event.
|
|
1
|
+
export { Emitter } from './src/emitter.ts';
|
|
2
|
+
export { BaseEvent } from './src/base_event.ts';
|
|
3
|
+
export * as tracingChannels from './src/tracing_channels.ts';
|
package/build/index.js
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
-
Emitter
|
|
3
|
-
|
|
2
|
+
Emitter,
|
|
3
|
+
tracing_channels_exports
|
|
4
|
+
} from "./chunk-R74HJGSQ.js";
|
|
4
5
|
|
|
5
6
|
// src/base_event.ts
|
|
6
7
|
import { RuntimeException } from "@poppinss/utils/exception";
|
|
7
8
|
var BaseEvent = class {
|
|
9
|
+
/**
|
|
10
|
+
* Base event constructor
|
|
11
|
+
*
|
|
12
|
+
* @param _ - Any constructor arguments (unused)
|
|
13
|
+
*/
|
|
8
14
|
constructor(..._) {
|
|
9
15
|
}
|
|
10
16
|
/**
|
|
@@ -13,6 +19,8 @@ var BaseEvent = class {
|
|
|
13
19
|
static emitter;
|
|
14
20
|
/**
|
|
15
21
|
* Specify the emitter instance to use for dispatching events
|
|
22
|
+
*
|
|
23
|
+
* @param emitter - The emitter instance to use
|
|
16
24
|
*/
|
|
17
25
|
static useEmitter(emitter) {
|
|
18
26
|
this.emitter = emitter;
|
|
@@ -20,6 +28,9 @@ var BaseEvent = class {
|
|
|
20
28
|
/**
|
|
21
29
|
* Dispatch the current class as an event. The method takes the arguments
|
|
22
30
|
* accepted by the class constructor.
|
|
31
|
+
*
|
|
32
|
+
* @param args - Constructor arguments for the event instance
|
|
33
|
+
* @throws RuntimeException if no emitter is configured
|
|
23
34
|
*/
|
|
24
35
|
static async dispatch(...args) {
|
|
25
36
|
if (!this.emitter) {
|
|
@@ -32,5 +43,6 @@ var BaseEvent = class {
|
|
|
32
43
|
};
|
|
33
44
|
export {
|
|
34
45
|
BaseEvent,
|
|
35
|
-
Emitter
|
|
46
|
+
Emitter,
|
|
47
|
+
tracing_channels_exports as tracingChannels
|
|
36
48
|
};
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import type { Emitter } from './emitter.
|
|
1
|
+
import type { Emitter } from './emitter.ts';
|
|
2
2
|
/**
|
|
3
3
|
* Base event adds ability to a class to act as an event. You can emit the
|
|
4
4
|
* event by calling "Event.dispatch" method.
|
|
5
5
|
*/
|
|
6
6
|
export declare class BaseEvent {
|
|
7
|
+
/**
|
|
8
|
+
* Base event constructor
|
|
9
|
+
*
|
|
10
|
+
* @param _ - Any constructor arguments (unused)
|
|
11
|
+
*/
|
|
7
12
|
constructor(..._: any[]);
|
|
8
13
|
/**
|
|
9
14
|
* The emitter to use for dispatching events
|
|
@@ -11,11 +16,16 @@ export declare class BaseEvent {
|
|
|
11
16
|
static emitter?: Emitter<any>;
|
|
12
17
|
/**
|
|
13
18
|
* Specify the emitter instance to use for dispatching events
|
|
19
|
+
*
|
|
20
|
+
* @param emitter - The emitter instance to use
|
|
14
21
|
*/
|
|
15
22
|
static useEmitter(emitter: Emitter<any>): void;
|
|
16
23
|
/**
|
|
17
24
|
* Dispatch the current class as an event. The method takes the arguments
|
|
18
25
|
* accepted by the class constructor.
|
|
26
|
+
*
|
|
27
|
+
* @param args - Constructor arguments for the event instance
|
|
28
|
+
* @throws RuntimeException if no emitter is configured
|
|
19
29
|
*/
|
|
20
30
|
static dispatch<T extends typeof BaseEvent>(this: T, ...args: ConstructorParameters<T>): Promise<void>;
|
|
21
31
|
}
|
package/build/src/emitter.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Application } from '@adonisjs/application';
|
|
2
2
|
import { type UnsubscribeFunction } from 'emittery';
|
|
3
3
|
import { type LazyImport, type Constructor } from '@poppinss/utils/types';
|
|
4
|
-
import { EventsBuffer } from './events_buffer.
|
|
5
|
-
import type { Listener, EmitterLike, ListenerMethod, AllowedEventTypes, ListenerClassWithHandleMethod } from './types.
|
|
4
|
+
import { EventsBuffer } from './events_buffer.ts';
|
|
5
|
+
import type { Listener, EmitterLike, ListenerMethod, AllowedEventTypes, ListenerClassWithHandleMethod } from './types.ts';
|
|
6
6
|
/**
|
|
7
7
|
* Event emitter is built on top of emittery with support class based
|
|
8
8
|
* events and listeners
|
|
@@ -16,37 +16,65 @@ export declare class Emitter<EventsList extends Record<string | symbol | number,
|
|
|
16
16
|
*
|
|
17
17
|
* The listeners map key is the original binding listener
|
|
18
18
|
* and the value is a callback function.
|
|
19
|
+
*
|
|
20
|
+
* @returns The events listeners map
|
|
19
21
|
*/
|
|
20
22
|
get eventsListeners(): Map<AllowedEventTypes, Map<Listener<any, Constructor<any>>, ListenerMethod<any>>>;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new Emitter instance
|
|
25
|
+
*
|
|
26
|
+
* @param app - The AdonisJS application instance
|
|
27
|
+
*/
|
|
21
28
|
constructor(app: Application<any>);
|
|
22
29
|
/**
|
|
23
30
|
* Register a global error handler
|
|
31
|
+
*
|
|
32
|
+
* @param callback - The error handler callback
|
|
33
|
+
* @returns The emitter instance for method chaining
|
|
24
34
|
*/
|
|
25
35
|
onError(callback: (event: keyof EventsList | Constructor<any>, error: any, data: any) => void): this;
|
|
26
36
|
/**
|
|
27
37
|
* Bind multiple listeners to listen for a single event. The listen
|
|
28
38
|
* method is a convenience helper to be used with class based
|
|
29
39
|
* events and listeners.
|
|
40
|
+
*
|
|
41
|
+
* @param event - The event class to listen for
|
|
42
|
+
* @param listeners - Array of listener classes with handle methods
|
|
30
43
|
*/
|
|
31
44
|
listen<Event extends Constructor<any>>(event: Event, listeners: (ListenerClassWithHandleMethod<InstanceType<Event>> | LazyImport<ListenerClassWithHandleMethod<InstanceType<Event>>>)[]): void;
|
|
32
45
|
/**
|
|
33
46
|
* Listen for an event. The method returns the unsubscribe function.
|
|
47
|
+
*
|
|
48
|
+
* @param event - The event to listen for
|
|
49
|
+
* @param listener - The listener to register
|
|
50
|
+
* @returns The unsubscribe function
|
|
34
51
|
*/
|
|
35
52
|
on<Event extends Constructor<any>, ListenerClass extends Constructor<any>>(event: Event, listener: Listener<InstanceType<Event>, ListenerClass>): UnsubscribeFunction;
|
|
36
53
|
on<Name extends keyof EventsList, ListenerClass extends Constructor<any>>(event: Name, listener: Listener<EventsList[Name], ListenerClass>): UnsubscribeFunction;
|
|
37
54
|
/**
|
|
38
55
|
* Listen for an event depending on a condition
|
|
56
|
+
*
|
|
57
|
+
* @param condition - The condition to check before listening
|
|
58
|
+
* @param event - The event to listen for
|
|
59
|
+
* @param listener - The listener to register
|
|
60
|
+
* @returns The unsubscribe function
|
|
39
61
|
*/
|
|
40
62
|
listenIf<Event extends Constructor<any>, ListenerClass extends Constructor<any>>(condition: boolean | (() => boolean), event: Event, listener: Listener<InstanceType<Event>, ListenerClass>): UnsubscribeFunction;
|
|
41
63
|
listenIf<Name extends keyof EventsList, ListenerClass extends Constructor<any>>(condition: boolean | (() => boolean), event: Name, listener: Listener<EventsList[Name], ListenerClass>): UnsubscribeFunction;
|
|
42
64
|
/**
|
|
43
65
|
* Listen for an event only once
|
|
66
|
+
*
|
|
67
|
+
* @param event - The event to listen for
|
|
68
|
+
* @param listener - The listener to register
|
|
44
69
|
*/
|
|
45
70
|
once<Event extends Constructor<any>, ListenerClass extends Constructor<any>>(event: Event, listener: Listener<InstanceType<Event>, ListenerClass>): void;
|
|
46
71
|
once<Name extends keyof EventsList, ListenerClass extends Constructor<any>>(event: Name, listener: Listener<EventsList[Name], ListenerClass>): void;
|
|
47
72
|
/**
|
|
48
73
|
* Attach a listener to listen for all the events. Wildcard listeners
|
|
49
74
|
* can only be defined as inline callbacks.
|
|
75
|
+
*
|
|
76
|
+
* @param listener - The wildcard listener callback
|
|
77
|
+
* @returns The unsubscribe function
|
|
50
78
|
*/
|
|
51
79
|
onAny(listener: (event: AllowedEventTypes, data: any) => any | Promise<any>): UnsubscribeFunction;
|
|
52
80
|
/**
|
|
@@ -54,6 +82,9 @@ export declare class Emitter<EventsList extends Record<string | symbol | number,
|
|
|
54
82
|
* in parallel.
|
|
55
83
|
*
|
|
56
84
|
* You can await this method to wait for events listeners to finish
|
|
85
|
+
*
|
|
86
|
+
* @param event - The event to emit
|
|
87
|
+
* @param data - The data to pass to listeners
|
|
57
88
|
*/
|
|
58
89
|
emit<Event extends Constructor<any>>(event: Event, data: InstanceType<Event>): Promise<void>;
|
|
59
90
|
emit<Name extends keyof EventsList>(event: Name, data: EventsList[Name]): Promise<void>;
|
|
@@ -62,25 +93,38 @@ export declare class Emitter<EventsList extends Record<string | symbol | number,
|
|
|
62
93
|
* in the same sequence as they are registered.
|
|
63
94
|
*
|
|
64
95
|
* You can await this method to wait for events listeners to finish
|
|
96
|
+
*
|
|
97
|
+
* @param event - The event to emit
|
|
98
|
+
* @param data - The data to pass to listeners
|
|
65
99
|
*/
|
|
66
100
|
emitSerial<Event extends Constructor<any>>(event: Event, data: InstanceType<Event>): Promise<void>;
|
|
67
101
|
emitSerial<Name extends keyof EventsList>(event: Name, data: EventsList[Name]): Promise<void>;
|
|
68
102
|
/**
|
|
69
103
|
* Remove a specific listener for an event
|
|
104
|
+
*
|
|
105
|
+
* @param event - The event to remove listener from
|
|
106
|
+
* @param listener - The listener to remove
|
|
70
107
|
*/
|
|
71
108
|
off(event: keyof EventsList | Constructor<any>, listener: Listener<any, Constructor<any>>): void;
|
|
72
109
|
/**
|
|
73
110
|
* Remove a specific listener listening for all the events
|
|
111
|
+
*
|
|
112
|
+
* @param listener - The wildcard listener to remove
|
|
113
|
+
* @returns The emitter instance for method chaining
|
|
74
114
|
*/
|
|
75
115
|
offAny(listener: (event: keyof EventsList | Constructor<any>, data: any) => any | Promise<any>): this;
|
|
76
116
|
/**
|
|
77
117
|
* Remove a specific listener for an event
|
|
78
118
|
*
|
|
79
119
|
* @alias "off"
|
|
120
|
+
* @param event - The event to remove listener from
|
|
121
|
+
* @param listener - The listener to remove
|
|
80
122
|
*/
|
|
81
123
|
clearListener(event: keyof EventsList | Constructor<any>, listener: Listener<any, Constructor<any>>): void;
|
|
82
124
|
/**
|
|
83
125
|
* Clear all listeners for a specific event
|
|
126
|
+
*
|
|
127
|
+
* @param event - The event to clear listeners for
|
|
84
128
|
*/
|
|
85
129
|
clearListeners(event: keyof EventsList | Constructor<any>): void;
|
|
86
130
|
/**
|
|
@@ -89,10 +133,16 @@ export declare class Emitter<EventsList extends Record<string | symbol | number,
|
|
|
89
133
|
clearAllListeners(): void;
|
|
90
134
|
/**
|
|
91
135
|
* Get count of listeners for a given event or all the events
|
|
136
|
+
*
|
|
137
|
+
* @param event - The event to count listeners for (optional)
|
|
138
|
+
* @returns The number of listeners
|
|
92
139
|
*/
|
|
93
140
|
listenerCount(event?: keyof EventsList | Constructor<any>): number;
|
|
94
141
|
/**
|
|
95
142
|
* Find if an event has one or more listeners
|
|
143
|
+
*
|
|
144
|
+
* @param event - The event to check listeners for (optional)
|
|
145
|
+
* @returns True if the event has listeners
|
|
96
146
|
*/
|
|
97
147
|
hasListeners(event?: keyof EventsList | Constructor<any>): boolean;
|
|
98
148
|
/**
|
|
@@ -104,6 +154,9 @@ export declare class Emitter<EventsList extends Record<string | symbol | number,
|
|
|
104
154
|
*
|
|
105
155
|
* Calling this method one than once drops the existing fakes and
|
|
106
156
|
* creates new one.
|
|
157
|
+
*
|
|
158
|
+
* @param events - Array of events to fake (optional, defaults to all events)
|
|
159
|
+
* @returns The events buffer for assertions
|
|
107
160
|
*/
|
|
108
161
|
fake(events?: (keyof EventsList | Constructor<any>)[]): EventsBuffer<EventsList>;
|
|
109
162
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Constructor } from '@poppinss/utils/types';
|
|
2
|
-
import type { AllowedEventTypes, BufferedEvent, BufferedEventsList } from './types.
|
|
2
|
+
import type { AllowedEventTypes, BufferedEvent, BufferedEventsList } from './types.ts';
|
|
3
3
|
/**
|
|
4
4
|
* Callback function to narrow down an event from
|
|
5
5
|
* the events buffer list
|
|
@@ -12,38 +12,67 @@ export declare class EventsBuffer<EventsList extends Record<string | symbol | nu
|
|
|
12
12
|
#private;
|
|
13
13
|
/**
|
|
14
14
|
* Track emitted event
|
|
15
|
+
*
|
|
16
|
+
* @param event - The event that was emitted
|
|
17
|
+
* @param data - The data passed with the event
|
|
15
18
|
*/
|
|
16
19
|
add<Name extends AllowedEventTypes>(event: Name, data: any): void;
|
|
17
20
|
/**
|
|
18
21
|
* Get all the emitted events
|
|
22
|
+
*
|
|
23
|
+
* @returns Array of all buffered events
|
|
19
24
|
*/
|
|
20
25
|
all(): BufferedEventsList<EventsList>[];
|
|
21
26
|
/**
|
|
22
27
|
* Returns the size of captured events
|
|
28
|
+
*
|
|
29
|
+
* @returns The number of captured events
|
|
23
30
|
*/
|
|
24
31
|
size(): number;
|
|
25
32
|
/**
|
|
26
33
|
* Find if an event was emitted
|
|
34
|
+
*
|
|
35
|
+
* @param event - The event to check for
|
|
36
|
+
* @param finder - Optional callback to filter specific event instances
|
|
37
|
+
* @returns True if the event was emitted
|
|
27
38
|
*/
|
|
28
39
|
exists<Event extends keyof EventsList | Constructor<any>>(event: Event, finder?: EventFinderCallback<EventsList, Event>): boolean;
|
|
29
40
|
/**
|
|
30
41
|
* Find a specific event
|
|
42
|
+
*
|
|
43
|
+
* @param event - The event to find
|
|
44
|
+
* @param finder - Optional callback to filter specific event instances
|
|
45
|
+
* @returns The found event or null
|
|
31
46
|
*/
|
|
32
47
|
find<Event extends keyof EventsList | Constructor<any>>(event: Event, finder?: EventFinderCallback<EventsList, Event>): (Event extends keyof EventsList ? BufferedEvent<Event, EventsList[Event]> : Event extends Constructor<infer A> ? BufferedEvent<Event, A> : never) | null;
|
|
33
48
|
/**
|
|
34
49
|
* Assert a given event has been emitted
|
|
50
|
+
*
|
|
51
|
+
* @param event - The event to assert was emitted
|
|
52
|
+
* @param finder - Optional callback to filter specific event instances
|
|
53
|
+
* @throws AssertionError if the event was not emitted
|
|
35
54
|
*/
|
|
36
55
|
assertEmitted<Event extends keyof EventsList | Constructor<any>>(event: Event, finder?: EventFinderCallback<EventsList, Event>): void;
|
|
37
56
|
/**
|
|
38
57
|
* Assert number of times an event has been emitted
|
|
58
|
+
*
|
|
59
|
+
* @param event - The event to check emission count for
|
|
60
|
+
* @param count - The expected number of emissions
|
|
61
|
+
* @throws AssertionError if the count doesn't match
|
|
39
62
|
*/
|
|
40
63
|
assertEmittedCount<Event extends keyof EventsList | Constructor<any>>(event: Event, count: number): void;
|
|
41
64
|
/**
|
|
42
65
|
* Assert a given event has been not been emitted
|
|
66
|
+
*
|
|
67
|
+
* @param event - The event to assert was not emitted
|
|
68
|
+
* @param finder - Optional callback to filter specific event instances
|
|
69
|
+
* @throws AssertionError if the event was emitted
|
|
43
70
|
*/
|
|
44
71
|
assertNotEmitted<Event extends keyof EventsList | Constructor<any>>(event: Event, finder?: EventFinderCallback<EventsList, Event>): void;
|
|
45
72
|
/**
|
|
46
|
-
* Assert
|
|
73
|
+
* Assert no events have been emitted
|
|
74
|
+
*
|
|
75
|
+
* @throws AssertionError if any events were emitted
|
|
47
76
|
*/
|
|
48
77
|
assertNoneEmitted(): void;
|
|
49
78
|
/**
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import diagnostics_channel from 'node:diagnostics_channel';
|
|
2
|
+
import { type EventDispatchData } from './types.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Traces event.emit method calls
|
|
5
|
+
*/
|
|
6
|
+
export declare const eventDispatch: diagnostics_channel.TracingChannel<"adonisjs.event.dispatch", EventDispatchData>;
|
package/build/src/types.d.ts
CHANGED
|
@@ -5,13 +5,18 @@ import { type LazyImport, type Constructor } from '@poppinss/utils/types';
|
|
|
5
5
|
export type AllowedEventTypes = string | symbol | number | Constructor<any>;
|
|
6
6
|
/**
|
|
7
7
|
* Data structure for a buffered event
|
|
8
|
+
*
|
|
9
|
+
* @template Event - The event type
|
|
10
|
+
* @template Data - The data type
|
|
8
11
|
*/
|
|
9
12
|
export type BufferedEvent<Event, Data> = {
|
|
10
13
|
event: Event;
|
|
11
14
|
data: Data;
|
|
12
15
|
};
|
|
13
16
|
/**
|
|
14
|
-
* Event list item inside
|
|
17
|
+
* Event list item inside buffered items
|
|
18
|
+
*
|
|
19
|
+
* @template EventsList - The events list type
|
|
15
20
|
*/
|
|
16
21
|
export type BufferedEventsList<EventsList> = {
|
|
17
22
|
[Name in keyof EventsList]: BufferedEvent<Name, EventsList[Name]>;
|
|
@@ -20,21 +25,30 @@ export type BufferedEventsList<EventsList> = {
|
|
|
20
25
|
* Representation of listener method on the listener class. The
|
|
21
26
|
* spread args can type hint dependencies and container will
|
|
22
27
|
* resolve them
|
|
28
|
+
*
|
|
29
|
+
* @template Data - The event data type
|
|
23
30
|
*/
|
|
24
31
|
export type ListenerMethod<Data> = (data: Data, ...args: any[]) => any | Promise<any>;
|
|
25
32
|
/**
|
|
26
33
|
* The event listener defined as an inline callback
|
|
34
|
+
*
|
|
35
|
+
* @template Data - The event data type
|
|
27
36
|
*/
|
|
28
37
|
export type ListenerFn<Data> = (data: Data) => any | Promise<any>;
|
|
29
38
|
/**
|
|
30
39
|
* Returns a union of methods from a listener that accepts
|
|
31
40
|
* the event data as the first argument.
|
|
41
|
+
*
|
|
42
|
+
* @template Listener - The listener class constructor
|
|
43
|
+
* @template Data - The event data type
|
|
32
44
|
*/
|
|
33
45
|
export type GetListenersMethods<Listener extends Constructor<any>, Data> = {
|
|
34
46
|
[K in keyof InstanceType<Listener>]: InstanceType<Listener>[K] extends ListenerMethod<Data> ? K : never;
|
|
35
47
|
}[keyof InstanceType<Listener>];
|
|
36
48
|
/**
|
|
37
49
|
* Representation of listener class with handle method
|
|
50
|
+
*
|
|
51
|
+
* @template Data - The event data type
|
|
38
52
|
*/
|
|
39
53
|
export type ListenerClassWithHandleMethod<Data> = Constructor<{
|
|
40
54
|
handle: ListenerMethod<Data>;
|
|
@@ -42,11 +56,16 @@ export type ListenerClassWithHandleMethod<Data> = Constructor<{
|
|
|
42
56
|
/**
|
|
43
57
|
* The event listener defined as an inline callback, string
|
|
44
58
|
* listener class reference or a lazily imported listener
|
|
59
|
+
*
|
|
60
|
+
* @template Data - The event data type
|
|
61
|
+
* @template ListenerClass - The listener class constructor
|
|
45
62
|
*/
|
|
46
63
|
export type Listener<Data, ListenerClass extends Constructor<any>> = ListenerFn<Data> | string | [LazyImport<ListenerClass> | ListenerClass, GetListenersMethods<ListenerClass, Data>?];
|
|
47
64
|
/**
|
|
48
65
|
* The EmitterLike interface exposes a less strict API to accept
|
|
49
66
|
* emitter as an argument to emit events.
|
|
67
|
+
*
|
|
68
|
+
* @template EventsList - The events list type
|
|
50
69
|
*/
|
|
51
70
|
export interface EmitterLike<EventsList extends Record<string | symbol | number, any>> {
|
|
52
71
|
/**
|
|
@@ -54,6 +73,10 @@ export interface EmitterLike<EventsList extends Record<string | symbol | number,
|
|
|
54
73
|
* in parallel.
|
|
55
74
|
*
|
|
56
75
|
* You can await this method to wait for events listeners to finish
|
|
76
|
+
*
|
|
77
|
+
* @param event - The event to emit
|
|
78
|
+
* @param data - The data to pass to listeners
|
|
79
|
+
* @returns Promise that resolves when all listeners finish
|
|
57
80
|
*/
|
|
58
81
|
emit<Event extends Constructor<any>>(event: Event, data: InstanceType<Event>): Promise<void>;
|
|
59
82
|
emit<Name extends keyof EventsList>(event: Name, data: EventsList[Name]): Promise<void>;
|
|
@@ -62,15 +85,32 @@ export interface EmitterLike<EventsList extends Record<string | symbol | number,
|
|
|
62
85
|
* in the same sequence as they are registered.
|
|
63
86
|
*
|
|
64
87
|
* You can await this method to wait for events listeners to finish
|
|
88
|
+
*
|
|
89
|
+
* @param event - The event to emit
|
|
90
|
+
* @param data - The data to pass to listeners
|
|
91
|
+
* @returns Promise that resolves when all listeners finish
|
|
65
92
|
*/
|
|
66
93
|
emitSerial<Event extends Constructor<any>>(event: Event, data: InstanceType<Event>): Promise<void>;
|
|
67
94
|
emitSerial<Name extends keyof EventsList>(event: Name, data: EventsList[Name]): Promise<void>;
|
|
68
95
|
/**
|
|
69
|
-
*
|
|
96
|
+
* Get count of listeners for a given event or all the events
|
|
97
|
+
*
|
|
98
|
+
* @param event - The event to count listeners for (optional)
|
|
99
|
+
* @returns The number of listeners
|
|
70
100
|
*/
|
|
71
101
|
listenerCount(event?: keyof EventsList | Constructor<any>): number;
|
|
72
102
|
/**
|
|
73
|
-
*
|
|
103
|
+
* Find if an event has one or more listeners
|
|
104
|
+
*
|
|
105
|
+
* @param event - The event to check listeners for (optional)
|
|
106
|
+
* @returns True if the event has listeners
|
|
74
107
|
*/
|
|
75
108
|
hasListeners(event?: keyof EventsList | Constructor<any>): boolean;
|
|
76
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Data shared via the event.dispatch tracing channel
|
|
112
|
+
*/
|
|
113
|
+
export type EventDispatchData = {
|
|
114
|
+
event: AllowedEventTypes;
|
|
115
|
+
data: unknown;
|
|
116
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/events",
|
|
3
3
|
"description": "An implementation of the event emitter built on top of emittery",
|
|
4
|
-
"version": "10.1.0-next.
|
|
4
|
+
"version": "10.1.0-next.2",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24.0.0"
|
|
7
7
|
},
|
|
@@ -36,27 +36,27 @@
|
|
|
36
36
|
"quick:test": "node --import=@poppinss/ts-exec --enable-source-maps bin/test.ts"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@adonisjs/application": "^9.0.0-next.
|
|
40
|
-
"@adonisjs/config": "^
|
|
41
|
-
"@adonisjs/eslint-config": "^3.0.0-next.
|
|
42
|
-
"@adonisjs/fold": "^
|
|
39
|
+
"@adonisjs/application": "^9.0.0-next.4",
|
|
40
|
+
"@adonisjs/config": "^6.0.0-next.1",
|
|
41
|
+
"@adonisjs/eslint-config": "^3.0.0-next.1",
|
|
42
|
+
"@adonisjs/fold": "^11.0.0-next.2",
|
|
43
43
|
"@adonisjs/prettier-config": "^1.4.5",
|
|
44
44
|
"@adonisjs/tsconfig": "^2.0.0-next.0",
|
|
45
45
|
"@japa/assert": "^4.1.1",
|
|
46
46
|
"@japa/expect-type": "^2.0.3",
|
|
47
47
|
"@japa/file-system": "^2.3.2",
|
|
48
|
-
"@japa/runner": "^4.
|
|
49
|
-
"@poppinss/ts-exec": "^1.4.
|
|
48
|
+
"@japa/runner": "^4.4.0",
|
|
49
|
+
"@poppinss/ts-exec": "^1.4.1",
|
|
50
50
|
"@release-it/conventional-changelog": "^10.0.1",
|
|
51
|
-
"@types/node": "^24.
|
|
51
|
+
"@types/node": "^24.3.0",
|
|
52
52
|
"c8": "^10.1.3",
|
|
53
53
|
"cross-env": "^10.0.0",
|
|
54
54
|
"del-cli": "^6.0.0",
|
|
55
|
-
"eslint": "^9.
|
|
55
|
+
"eslint": "^9.34.0",
|
|
56
56
|
"prettier": "^3.6.2",
|
|
57
57
|
"release-it": "^19.0.4",
|
|
58
58
|
"tsup": "^8.5.0",
|
|
59
|
-
"typescript": "^5.
|
|
59
|
+
"typescript": "^5.9.2"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
62
|
"@poppinss/utils": "^7.0.0-next.3",
|
|
@@ -64,8 +64,8 @@
|
|
|
64
64
|
"emittery": "^1.2.0"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"@adonisjs/application": "^9.0.0-next.
|
|
68
|
-
"@adonisjs/fold": "^
|
|
67
|
+
"@adonisjs/application": "^9.0.0-next.4",
|
|
68
|
+
"@adonisjs/fold": "^11.0.0-next.2"
|
|
69
69
|
},
|
|
70
70
|
"homepage": "https://github.com/adonisjs/events#readme",
|
|
71
71
|
"repository": {
|