@adonisjs/events 8.4.9-2 → 8.4.9-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/build/factories/emitter.d.ts +7 -1
- package/build/factories/emitter.js +15 -0
- package/build/factories/main.d.ts +0 -1
- package/build/factories/main.js +8 -0
- package/build/index.d.ts +0 -1
- package/build/index.js +8 -0
- package/build/src/base_event.d.ts +14 -1
- package/build/src/base_event.js +22 -0
- package/build/src/debug.d.ts +0 -1
- package/build/src/debug.js +8 -0
- package/build/src/emitter.d.ts +80 -1
- package/build/src/emitter.js +157 -1
- package/build/src/events_buffer.d.ts +33 -1
- package/build/src/events_buffer.js +44 -0
- package/build/src/types.d.ts +34 -1
- package/build/src/types.js +8 -0
- package/package.json +32 -60
- package/build/factories/emitter.d.ts.map +0 -1
- package/build/factories/main.d.ts.map +0 -1
- package/build/index.d.ts.map +0 -1
- package/build/src/base_event.d.ts.map +0 -1
- package/build/src/debug.d.ts.map +0 -1
- package/build/src/emitter.d.ts.map +0 -1
- package/build/src/events_buffer.d.ts.map +0 -1
- package/build/src/types.d.ts.map +0 -1
- package/factories/emitter.ts +0 -24
- package/factories/main.ts +0 -10
- package/index.ts +0 -11
- package/src/base_event.ts +0 -45
- package/src/debug.ts +0 -11
- package/src/emitter.ts +0 -407
- package/src/events_buffer.ts +0 -168
- package/src/types.ts +0 -75
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { Application } from '@adonisjs/application';
|
|
2
2
|
import { Emitter } from '../src/emitter.js';
|
|
3
|
+
/**
|
|
4
|
+
* Emitter factory is used to create an instance of emitter
|
|
5
|
+
* for testing
|
|
6
|
+
*/
|
|
3
7
|
export declare class EmitterFactory {
|
|
8
|
+
/**
|
|
9
|
+
* Create emitter instance
|
|
10
|
+
*/
|
|
4
11
|
create(app: Application<any>): Emitter<Record<string | number | symbol, any>>;
|
|
5
12
|
}
|
|
6
|
-
//# sourceMappingURL=emitter.d.ts.map
|
|
@@ -1,5 +1,20 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/events
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
import { Emitter } from '../src/emitter.js';
|
|
10
|
+
/**
|
|
11
|
+
* Emitter factory is used to create an instance of emitter
|
|
12
|
+
* for testing
|
|
13
|
+
*/
|
|
2
14
|
export class EmitterFactory {
|
|
15
|
+
/**
|
|
16
|
+
* Create emitter instance
|
|
17
|
+
*/
|
|
3
18
|
create(app) {
|
|
4
19
|
return new Emitter(app);
|
|
5
20
|
}
|
package/build/factories/main.js
CHANGED
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -1,2 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/events
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
export { Emitter } from './src/emitter.js';
|
|
2
10
|
export { BaseEvent } from './src/base_event.js';
|
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import type { Emitter } from './emitter.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base event adds ability to a class to act as an event. You can emit the
|
|
4
|
+
* event by calling "Event.dispatch" method.
|
|
5
|
+
*/
|
|
2
6
|
export declare class BaseEvent {
|
|
3
7
|
constructor(..._: any[]);
|
|
8
|
+
/**
|
|
9
|
+
* The emitter to use for dispatching events
|
|
10
|
+
*/
|
|
4
11
|
static emitter?: Emitter<any>;
|
|
12
|
+
/**
|
|
13
|
+
* Specify the emitter instance to use for dispatching events
|
|
14
|
+
*/
|
|
5
15
|
static useEmitter(emitter: Emitter<any>): void;
|
|
16
|
+
/**
|
|
17
|
+
* Dispatch the current class as an event. The method takes the arguments
|
|
18
|
+
* accepted by the class constructor.
|
|
19
|
+
*/
|
|
6
20
|
static dispatch<T extends typeof BaseEvent>(this: T, ...args: ConstructorParameters<T>): Promise<void>;
|
|
7
21
|
}
|
|
8
|
-
//# sourceMappingURL=base_event.d.ts.map
|
package/build/src/base_event.js
CHANGED
|
@@ -1,10 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/events
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
import { RuntimeException } from '@poppinss/utils';
|
|
10
|
+
/**
|
|
11
|
+
* Base event adds ability to a class to act as an event. You can emit the
|
|
12
|
+
* event by calling "Event.dispatch" method.
|
|
13
|
+
*/
|
|
2
14
|
export class BaseEvent {
|
|
3
15
|
constructor(..._) { }
|
|
16
|
+
/**
|
|
17
|
+
* The emitter to use for dispatching events
|
|
18
|
+
*/
|
|
4
19
|
static emitter;
|
|
20
|
+
/**
|
|
21
|
+
* Specify the emitter instance to use for dispatching events
|
|
22
|
+
*/
|
|
5
23
|
static useEmitter(emitter) {
|
|
6
24
|
this.emitter = emitter;
|
|
7
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Dispatch the current class as an event. The method takes the arguments
|
|
28
|
+
* accepted by the class constructor.
|
|
29
|
+
*/
|
|
8
30
|
static async dispatch(...args) {
|
|
9
31
|
if (!this.emitter) {
|
|
10
32
|
throw new RuntimeException(`Cannot dispatch "${this.name}" event. Make sure to pass emitter to the "BaseEvent" class for dispatch method to work`);
|
package/build/src/debug.d.ts
CHANGED
package/build/src/debug.js
CHANGED
|
@@ -1,2 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/events
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
import { debuglog } from 'node:util';
|
|
2
10
|
export default debuglog('adonisjs:events');
|
package/build/src/emitter.d.ts
CHANGED
|
@@ -2,27 +2,106 @@ import type { Application } from '@adonisjs/application';
|
|
|
2
2
|
import { type UnsubscribeFunction } from 'emittery';
|
|
3
3
|
import { EventsBuffer } from './events_buffer.js';
|
|
4
4
|
import type { Listener, LazyImport, Constructor, ListenerMethod, AllowedEventTypes, ListenerClassWithHandleMethod } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Event emitter is built on top of emittery with support class based
|
|
7
|
+
* events and listeners
|
|
8
|
+
*/
|
|
5
9
|
export declare class Emitter<EventsList extends Record<string | symbol | number, any>> {
|
|
6
10
|
#private;
|
|
11
|
+
/**
|
|
12
|
+
* Returns a map of events and their registered listeners. The
|
|
13
|
+
* map key is the event name and the value is another map
|
|
14
|
+
* of listeners.
|
|
15
|
+
*
|
|
16
|
+
* The listeners map key is the original binding listener
|
|
17
|
+
* and the value is a callback function.
|
|
18
|
+
*/
|
|
7
19
|
get eventsListeners(): Map<AllowedEventTypes, Map<Listener<any, Constructor<any>>, ListenerMethod<any>>>;
|
|
8
20
|
constructor(app: Application<any>);
|
|
21
|
+
/**
|
|
22
|
+
* Register a global error handler
|
|
23
|
+
*/
|
|
9
24
|
onError(callback: (event: keyof EventsList | Constructor<any>, error: any, data: any) => void): this;
|
|
25
|
+
/**
|
|
26
|
+
* Bind multiple listeners to listen for a single event. The listen
|
|
27
|
+
* method is a convenience helper to be used with class based
|
|
28
|
+
* events and listeners.
|
|
29
|
+
*/
|
|
10
30
|
listen<Event extends Constructor<any>>(event: Event, listeners: (ListenerClassWithHandleMethod<InstanceType<Event>> | LazyImport<ListenerClassWithHandleMethod<InstanceType<Event>>>)[]): void;
|
|
31
|
+
/**
|
|
32
|
+
* Listen for an event. The method returns the unsubscribe function.
|
|
33
|
+
*/
|
|
11
34
|
on<Event extends Constructor, ListenerClass extends Constructor>(event: Event, listener: Listener<InstanceType<Event>, ListenerClass>): UnsubscribeFunction;
|
|
12
35
|
on<Name extends keyof EventsList, ListenerClass extends Constructor>(event: Name, listener: Listener<EventsList[Name], ListenerClass>): UnsubscribeFunction;
|
|
36
|
+
/**
|
|
37
|
+
* Listen for an event only once
|
|
38
|
+
*/
|
|
13
39
|
once<Event extends Constructor, ListenerClass extends Constructor>(event: Event, listener: Listener<InstanceType<Event>, ListenerClass>): void;
|
|
14
40
|
once<Name extends keyof EventsList, ListenerClass extends Constructor>(event: Name, listener: Listener<EventsList[Name], ListenerClass>): void;
|
|
41
|
+
/**
|
|
42
|
+
* Attach a listener to listen for all the events. Wildcard listeners
|
|
43
|
+
* can only be defined as inline callbacks.
|
|
44
|
+
*/
|
|
15
45
|
onAny(listener: (event: AllowedEventTypes, data: any) => any | Promise<any>): UnsubscribeFunction;
|
|
46
|
+
/**
|
|
47
|
+
* Emit event. The event listeners will be called asynchronously
|
|
48
|
+
* in parallel.
|
|
49
|
+
*
|
|
50
|
+
* You can await this method to wait for events listeners to finish
|
|
51
|
+
*/
|
|
16
52
|
emit<Event extends Constructor<any>>(event: Event, data: InstanceType<Event>): Promise<void>;
|
|
17
53
|
emit<Name extends keyof EventsList>(event: Name, data: EventsList[Name]): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Emit events serially. The event listeners will be called asynchronously
|
|
56
|
+
* in the same sequence as they are registered.
|
|
57
|
+
*
|
|
58
|
+
* You can await this method to wait for events listeners to finish
|
|
59
|
+
*/
|
|
60
|
+
emitSerial<Event extends Constructor<any>>(event: Event, data: InstanceType<Event>): Promise<void>;
|
|
61
|
+
emitSerial<Name extends keyof EventsList>(event: Name, data: EventsList[Name]): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Remove a specific listener for an event
|
|
64
|
+
*/
|
|
18
65
|
off(event: keyof EventsList | Constructor<any>, listener: Listener<any, Constructor<any>>): void;
|
|
66
|
+
/**
|
|
67
|
+
* Remove a specific listener listening for all the events
|
|
68
|
+
*/
|
|
19
69
|
offAny(listener: (event: keyof EventsList | Constructor<any>, data: any) => any | Promise<any>): this;
|
|
70
|
+
/**
|
|
71
|
+
* Remove a specific listener for an event
|
|
72
|
+
*
|
|
73
|
+
* @alias "off"
|
|
74
|
+
*/
|
|
20
75
|
clearListener(event: keyof EventsList | Constructor<any>, listener: Listener<any, Constructor<any>>): void;
|
|
76
|
+
/**
|
|
77
|
+
* Clear all listeners for a specific event
|
|
78
|
+
*/
|
|
21
79
|
clearListeners(event: keyof EventsList | Constructor<any>): void;
|
|
80
|
+
/**
|
|
81
|
+
* Clear all listeners for all the events
|
|
82
|
+
*/
|
|
22
83
|
clearAllListeners(): void;
|
|
84
|
+
/**
|
|
85
|
+
* Get count of listeners for a given event or all the events
|
|
86
|
+
*/
|
|
23
87
|
listenerCount(event?: keyof EventsList | Constructor<any>): number;
|
|
88
|
+
/**
|
|
89
|
+
* Find if an event has one or more listeners
|
|
90
|
+
*/
|
|
24
91
|
hasListeners(event?: keyof EventsList | Constructor<any>): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Fake one or more events. The listeners for faked events will
|
|
94
|
+
* not be invoked.
|
|
95
|
+
*
|
|
96
|
+
* The return value is an events buffer that collects all the
|
|
97
|
+
* events within memory.
|
|
98
|
+
*
|
|
99
|
+
* Calling this method one than once drops the existing fakes and
|
|
100
|
+
* creates new one.
|
|
101
|
+
*/
|
|
25
102
|
fake(events?: (keyof EventsList | Constructor<any>)[]): EventsBuffer<EventsList>;
|
|
103
|
+
/**
|
|
104
|
+
* Restore fakes
|
|
105
|
+
*/
|
|
26
106
|
restore(): void;
|
|
27
107
|
}
|
|
28
|
-
//# sourceMappingURL=emitter.d.ts.map
|
package/build/src/emitter.js
CHANGED
|
@@ -1,54 +1,130 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/events
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
import is from '@sindresorhus/is';
|
|
2
10
|
import Emittery from 'emittery';
|
|
3
11
|
import { moduleExpression, moduleCaller, moduleImporter } from '@adonisjs/fold';
|
|
4
12
|
import debug from './debug.js';
|
|
5
13
|
import { EventsBuffer } from './events_buffer.js';
|
|
14
|
+
/**
|
|
15
|
+
* Event emitter is built on top of emittery with support class based
|
|
16
|
+
* events and listeners
|
|
17
|
+
*/
|
|
6
18
|
export class Emitter {
|
|
19
|
+
/**
|
|
20
|
+
* Event classes to symbols mapping. We need symbols as emittery
|
|
21
|
+
* does not support class based event names
|
|
22
|
+
*/
|
|
7
23
|
#eventsClassSymbols = new Map();
|
|
24
|
+
/**
|
|
25
|
+
* A collection of events and their listeners. We do not track listeners
|
|
26
|
+
* listening for events only once
|
|
27
|
+
*/
|
|
8
28
|
#eventsListeners = new Map();
|
|
29
|
+
/**
|
|
30
|
+
* Underlying transport to emit events
|
|
31
|
+
*/
|
|
9
32
|
#transport = new Emittery();
|
|
33
|
+
/**
|
|
34
|
+
* Events buffer. The events are collected inside an in-memory
|
|
35
|
+
* buffer during fakes
|
|
36
|
+
*/
|
|
10
37
|
#eventsBuffer;
|
|
38
|
+
/**
|
|
39
|
+
* A set of events to fake
|
|
40
|
+
*/
|
|
11
41
|
#eventsToFake = new Set();
|
|
42
|
+
/**
|
|
43
|
+
* Error handler to catch all errors thrown by listeners
|
|
44
|
+
*/
|
|
12
45
|
#errorHandler;
|
|
46
|
+
/**
|
|
47
|
+
* Reference to AdonisJS application, we need the application root
|
|
48
|
+
* and container reference from it.
|
|
49
|
+
*/
|
|
13
50
|
#app;
|
|
51
|
+
/**
|
|
52
|
+
* Returns a map of events and their registered listeners. The
|
|
53
|
+
* map key is the event name and the value is another map
|
|
54
|
+
* of listeners.
|
|
55
|
+
*
|
|
56
|
+
* The listeners map key is the original binding listener
|
|
57
|
+
* and the value is a callback function.
|
|
58
|
+
*/
|
|
14
59
|
get eventsListeners() {
|
|
15
60
|
return this.#eventsListeners;
|
|
16
61
|
}
|
|
17
62
|
constructor(app) {
|
|
18
63
|
this.#app = app;
|
|
19
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Returns the symbol for a class based event.
|
|
67
|
+
*/
|
|
20
68
|
#getEventClassSymbol(event) {
|
|
21
69
|
if (!this.#eventsClassSymbols.has(event)) {
|
|
22
70
|
this.#eventsClassSymbols.set(event, Symbol(event.name));
|
|
23
71
|
}
|
|
24
72
|
return this.#eventsClassSymbols.get(event);
|
|
25
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Normalizes the event to emittery supported data types. The class
|
|
76
|
+
* constructors are cached against a unique symbol.
|
|
77
|
+
*/
|
|
26
78
|
#resolveEvent(event) {
|
|
27
79
|
if (is.class_(event)) {
|
|
28
80
|
return this.#getEventClassSymbol(event);
|
|
29
81
|
}
|
|
30
82
|
return event;
|
|
31
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Returns the event listeners map
|
|
86
|
+
*/
|
|
32
87
|
#getEventListeners(event) {
|
|
33
88
|
if (!this.#eventsListeners.has(event)) {
|
|
34
89
|
this.#eventsListeners.set(event, new Map());
|
|
35
90
|
}
|
|
36
91
|
return this.#eventsListeners.get(event);
|
|
37
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Normalizes the event listener to a function that can be passed to
|
|
95
|
+
* emittery.
|
|
96
|
+
*/
|
|
38
97
|
#normalizeEventListener(listener) {
|
|
98
|
+
/**
|
|
99
|
+
* Parse string based listener
|
|
100
|
+
*/
|
|
39
101
|
if (typeof listener === 'string') {
|
|
40
|
-
return moduleExpression(listener, this.#app.appRoot).toCallable(this.#app.container);
|
|
102
|
+
return moduleExpression(listener, this.#app.appRoot.toString()).toCallable(this.#app.container);
|
|
41
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Parse array based listener with the listener reference
|
|
106
|
+
* or lazily imported listener class
|
|
107
|
+
*/
|
|
42
108
|
if (Array.isArray(listener)) {
|
|
43
109
|
const listenerModule = listener[0];
|
|
44
110
|
const method = listener[1] || 'handle';
|
|
111
|
+
/**
|
|
112
|
+
* Class reference
|
|
113
|
+
*/
|
|
45
114
|
if (is.class_(listenerModule)) {
|
|
46
115
|
return moduleCaller(listenerModule, method).toCallable(this.#app.container);
|
|
47
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Lazily loaded module
|
|
119
|
+
*/
|
|
48
120
|
return moduleImporter(listenerModule, method).toCallable(this.#app.container);
|
|
49
121
|
}
|
|
50
122
|
return listener;
|
|
51
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Resolves the event listener either from the cache or normalizes
|
|
126
|
+
* it and stores it inside the cache
|
|
127
|
+
*/
|
|
52
128
|
#resolveEventListener(event, listener) {
|
|
53
129
|
const eventListeners = this.#getEventListeners(event);
|
|
54
130
|
if (!eventListeners.has(listener)) {
|
|
@@ -56,10 +132,18 @@ export class Emitter {
|
|
|
56
132
|
}
|
|
57
133
|
return eventListeners.get(listener);
|
|
58
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Register a global error handler
|
|
137
|
+
*/
|
|
59
138
|
onError(callback) {
|
|
60
139
|
this.#errorHandler = callback;
|
|
61
140
|
return this;
|
|
62
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Bind multiple listeners to listen for a single event. The listen
|
|
144
|
+
* method is a convenience helper to be used with class based
|
|
145
|
+
* events and listeners.
|
|
146
|
+
*/
|
|
63
147
|
listen(event, listeners) {
|
|
64
148
|
listeners.forEach((listener) => this.on(event, [listener, 'handle']));
|
|
65
149
|
}
|
|
@@ -78,16 +162,30 @@ export class Emitter {
|
|
|
78
162
|
}
|
|
79
163
|
const normalizedEvent = this.#resolveEvent(event);
|
|
80
164
|
const normalizedListener = this.#normalizeEventListener(listener);
|
|
165
|
+
/**
|
|
166
|
+
* Listening for an event and unsubscribing right after the event is emitted.
|
|
167
|
+
* Internally emittery does the same thing, but they do not await the
|
|
168
|
+
* handler. Therefore, the "once" listeners will finish after the
|
|
169
|
+
* "emit" call. This behavior is not inline with the "on" event
|
|
170
|
+
* listeners.
|
|
171
|
+
*/
|
|
81
172
|
const off = this.#transport.on(normalizedEvent, async (data) => {
|
|
82
173
|
off();
|
|
83
174
|
debug('removing one time event listener, event: %O', event);
|
|
84
175
|
await normalizedListener(data);
|
|
85
176
|
});
|
|
86
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* Attach a listener to listen for all the events. Wildcard listeners
|
|
180
|
+
* can only be defined as inline callbacks.
|
|
181
|
+
*/
|
|
87
182
|
onAny(listener) {
|
|
88
183
|
return this.#transport.onAny(listener);
|
|
89
184
|
}
|
|
90
185
|
async emit(event, data) {
|
|
186
|
+
/**
|
|
187
|
+
* Entertain fakes if exists
|
|
188
|
+
*/
|
|
91
189
|
if (this.#eventsToFake.has(event) || this.#eventsToFake.has('*')) {
|
|
92
190
|
debug('faking emit. event: %O, data: %O', event, data);
|
|
93
191
|
this.#eventsBuffer.add(event, data);
|
|
@@ -106,6 +204,31 @@ export class Emitter {
|
|
|
106
204
|
}
|
|
107
205
|
}
|
|
108
206
|
}
|
|
207
|
+
async emitSerial(event, data) {
|
|
208
|
+
/**
|
|
209
|
+
* Entertain fakes if exists
|
|
210
|
+
*/
|
|
211
|
+
if (this.#eventsToFake.has(event) || this.#eventsToFake.has('*')) {
|
|
212
|
+
debug('faking emit. event: %O, data: %O', event, data);
|
|
213
|
+
this.#eventsBuffer.add(event, data);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
218
|
+
await this.#transport.emitSerial(normalizedEvent, data);
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
if (this.#errorHandler) {
|
|
222
|
+
this.#errorHandler(event, error, data);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Remove a specific listener for an event
|
|
231
|
+
*/
|
|
109
232
|
off(event, listener) {
|
|
110
233
|
if (debug.enabled) {
|
|
111
234
|
debug('removing listener, event: %O, listener: %O', event, listener);
|
|
@@ -119,29 +242,59 @@ export class Emitter {
|
|
|
119
242
|
listeners.delete(listener);
|
|
120
243
|
this.#transport.off(normalizedEvent, normalizedListener);
|
|
121
244
|
}
|
|
245
|
+
/**
|
|
246
|
+
* Remove a specific listener listening for all the events
|
|
247
|
+
*/
|
|
122
248
|
offAny(listener) {
|
|
123
249
|
this.#transport.offAny(listener);
|
|
124
250
|
return this;
|
|
125
251
|
}
|
|
252
|
+
/**
|
|
253
|
+
* Remove a specific listener for an event
|
|
254
|
+
*
|
|
255
|
+
* @alias "off"
|
|
256
|
+
*/
|
|
126
257
|
clearListener(event, listener) {
|
|
127
258
|
return this.off(event, listener);
|
|
128
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* Clear all listeners for a specific event
|
|
262
|
+
*/
|
|
129
263
|
clearListeners(event) {
|
|
130
264
|
debug('clearing all listeners for event %O', event);
|
|
131
265
|
this.#transport.clearListeners(this.#resolveEvent(event));
|
|
132
266
|
this.#eventsListeners.delete(event);
|
|
133
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Clear all listeners for all the events
|
|
270
|
+
*/
|
|
134
271
|
clearAllListeners() {
|
|
135
272
|
debug('clearing all event listeners');
|
|
136
273
|
this.#transport.clearListeners();
|
|
137
274
|
this.#eventsListeners.clear();
|
|
138
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* Get count of listeners for a given event or all the events
|
|
278
|
+
*/
|
|
139
279
|
listenerCount(event) {
|
|
140
280
|
return this.#transport.listenerCount(event ? this.#resolveEvent(event) : undefined);
|
|
141
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Find if an event has one or more listeners
|
|
284
|
+
*/
|
|
142
285
|
hasListeners(event) {
|
|
143
286
|
return this.listenerCount(event) > 0;
|
|
144
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* Fake one or more events. The listeners for faked events will
|
|
290
|
+
* not be invoked.
|
|
291
|
+
*
|
|
292
|
+
* The return value is an events buffer that collects all the
|
|
293
|
+
* events within memory.
|
|
294
|
+
*
|
|
295
|
+
* Calling this method one than once drops the existing fakes and
|
|
296
|
+
* creates new one.
|
|
297
|
+
*/
|
|
145
298
|
fake(events) {
|
|
146
299
|
this.restore();
|
|
147
300
|
this.#eventsBuffer = new EventsBuffer();
|
|
@@ -155,6 +308,9 @@ export class Emitter {
|
|
|
155
308
|
}
|
|
156
309
|
return this.#eventsBuffer;
|
|
157
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* Restore fakes
|
|
313
|
+
*/
|
|
158
314
|
restore() {
|
|
159
315
|
debug('restoring existing fakes');
|
|
160
316
|
this.#eventsToFake.clear();
|
|
@@ -1,15 +1,47 @@
|
|
|
1
1
|
import type { AllowedEventTypes, BufferedEvent, BufferedEventsList, Constructor } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Exposes API to filter, find events from the events buffer.
|
|
4
|
+
*/
|
|
2
5
|
export declare class EventsBuffer<EventsList extends Record<string | symbol | number, any>> {
|
|
3
6
|
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Track emitted event
|
|
9
|
+
*/
|
|
4
10
|
add<Name extends AllowedEventTypes>(event: Name, data: any): void;
|
|
11
|
+
/**
|
|
12
|
+
* Get all the emitted events
|
|
13
|
+
*/
|
|
5
14
|
all(): BufferedEventsList<EventsList>[];
|
|
15
|
+
/**
|
|
16
|
+
* Returns the size of captured events
|
|
17
|
+
*/
|
|
6
18
|
size(): number;
|
|
19
|
+
/**
|
|
20
|
+
* Find if an event was emitted
|
|
21
|
+
*/
|
|
7
22
|
exists<Event extends keyof EventsList | Constructor>(finder: Event | ((event: BufferedEventsList<EventsList>) => boolean)): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Get selected events
|
|
25
|
+
*/
|
|
8
26
|
filter(finder: keyof EventsList | Constructor | ((event: BufferedEventsList<EventsList>) => boolean)): BufferedEventsList<EventsList>[];
|
|
27
|
+
/**
|
|
28
|
+
* Find a specific event
|
|
29
|
+
*/
|
|
9
30
|
find<Event extends keyof EventsList | Constructor>(finder: Event | ((event: BufferedEventsList<EventsList>) => boolean)): (Event extends keyof EventsList ? BufferedEvent<Event, EventsList[Event]> : Event extends Constructor<infer A> ? BufferedEvent<Event, A> : BufferedEventsList<EventsList>) | null;
|
|
31
|
+
/**
|
|
32
|
+
* Assert a given event has been emitted
|
|
33
|
+
*/
|
|
10
34
|
assertEmitted<Event extends keyof EventsList | Constructor>(finder: Event | ((event: BufferedEventsList<EventsList>) => boolean)): void;
|
|
35
|
+
/**
|
|
36
|
+
* Assert a given event has been not been emitted
|
|
37
|
+
*/
|
|
11
38
|
assertNotEmitted<Event extends keyof EventsList | Constructor<any>>(finder: Event | ((event: BufferedEventsList<EventsList>) => boolean)): void;
|
|
39
|
+
/**
|
|
40
|
+
* Assert a given event has been not been emitted
|
|
41
|
+
*/
|
|
12
42
|
assertNoneEmitted(): void;
|
|
43
|
+
/**
|
|
44
|
+
* Flush events collected within memory
|
|
45
|
+
*/
|
|
13
46
|
flush(): void;
|
|
14
47
|
}
|
|
15
|
-
//# sourceMappingURL=events_buffer.d.ts.map
|
|
@@ -1,31 +1,66 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/events
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
import is from '@sindresorhus/is';
|
|
2
10
|
import { AssertionError } from 'node:assert';
|
|
11
|
+
/**
|
|
12
|
+
* Exposes API to filter, find events from the events buffer.
|
|
13
|
+
*/
|
|
3
14
|
export class EventsBuffer {
|
|
15
|
+
/**
|
|
16
|
+
* Buffered events
|
|
17
|
+
*/
|
|
4
18
|
#events = [];
|
|
19
|
+
/**
|
|
20
|
+
* Track emitted event
|
|
21
|
+
*/
|
|
5
22
|
add(event, data) {
|
|
6
23
|
this.#events.push({ event: event, data });
|
|
7
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Get all the emitted events
|
|
27
|
+
*/
|
|
8
28
|
all() {
|
|
9
29
|
return this.#events;
|
|
10
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns the size of captured events
|
|
33
|
+
*/
|
|
11
34
|
size() {
|
|
12
35
|
return this.#events.length;
|
|
13
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Find if an event was emitted
|
|
39
|
+
*/
|
|
14
40
|
exists(finder) {
|
|
15
41
|
return !!this.find(finder);
|
|
16
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Get selected events
|
|
45
|
+
*/
|
|
17
46
|
filter(finder) {
|
|
18
47
|
if (typeof finder === 'function' && !is.class_(finder)) {
|
|
19
48
|
return this.#events.filter(finder);
|
|
20
49
|
}
|
|
21
50
|
return this.#events.filter((event) => event.event === finder);
|
|
22
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Find a specific event
|
|
54
|
+
*/
|
|
23
55
|
find(finder) {
|
|
24
56
|
if (typeof finder === 'function' && !is.class_(finder)) {
|
|
25
57
|
return (this.#events.find(finder) || null);
|
|
26
58
|
}
|
|
27
59
|
return (this.#events.find((event) => event.event === finder) || null);
|
|
28
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Assert a given event has been emitted
|
|
63
|
+
*/
|
|
29
64
|
assertEmitted(finder) {
|
|
30
65
|
const hasEvent = this.exists(finder);
|
|
31
66
|
if (!hasEvent) {
|
|
@@ -44,6 +79,9 @@ export class EventsBuffer {
|
|
|
44
79
|
});
|
|
45
80
|
}
|
|
46
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Assert a given event has been not been emitted
|
|
84
|
+
*/
|
|
47
85
|
assertNotEmitted(finder) {
|
|
48
86
|
const hasEvent = this.exists(finder);
|
|
49
87
|
if (hasEvent) {
|
|
@@ -62,6 +100,9 @@ export class EventsBuffer {
|
|
|
62
100
|
});
|
|
63
101
|
}
|
|
64
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* Assert a given event has been not been emitted
|
|
105
|
+
*/
|
|
65
106
|
assertNoneEmitted() {
|
|
66
107
|
const eventsSize = this.size();
|
|
67
108
|
if (eventsSize > 0) {
|
|
@@ -76,6 +117,9 @@ export class EventsBuffer {
|
|
|
76
117
|
}));
|
|
77
118
|
}
|
|
78
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Flush events collected within memory
|
|
122
|
+
*/
|
|
79
123
|
flush() {
|
|
80
124
|
this.#events = [];
|
|
81
125
|
}
|