@adonisjs/events 10.1.0-next.1 → 10.1.0-next.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/emitter-Bpdlo50r.js +250 -0
- package/build/factories/emitter.d.ts +1 -1
- package/build/factories/main.d.ts +1 -1
- package/build/factories/main.js +5 -14
- package/build/index.d.ts +2 -2
- package/build/index.js +11 -35
- package/build/src/base_event.d.ts +11 -1
- package/build/src/debug.d.ts +1 -1
- package/build/src/emitter.d.ts +55 -3
- package/build/src/events_buffer.d.ts +31 -2
- package/build/src/tracing_channels.d.ts +1 -1
- package/build/src/types.d.ts +36 -3
- package/build/src/types.js +1 -0
- package/package.json +30 -27
- package/build/chunk-X4QBBFBR.js +0 -464
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import is from "@sindresorhus/is";
|
|
3
|
+
import Emittery from "emittery";
|
|
4
|
+
import { moduleCaller, moduleImporter } from "@adonisjs/fold";
|
|
5
|
+
import { debuglog } from "node:util";
|
|
6
|
+
import string from "@poppinss/utils/string";
|
|
7
|
+
import { AssertionError } from "node:assert";
|
|
8
|
+
import diagnostics_channel from "node:diagnostics_channel";
|
|
9
|
+
var __defProp = Object.defineProperty;
|
|
10
|
+
var __export = (all, symbols) => {
|
|
11
|
+
let target = {};
|
|
12
|
+
for (var name in all) __defProp(target, name, {
|
|
13
|
+
get: all[name],
|
|
14
|
+
enumerable: true
|
|
15
|
+
});
|
|
16
|
+
if (symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
17
|
+
return target;
|
|
18
|
+
};
|
|
19
|
+
var debug_default = debuglog("adonisjs:events");
|
|
20
|
+
var EventsBuffer = class {
|
|
21
|
+
#events = [];
|
|
22
|
+
add(event, data) {
|
|
23
|
+
this.#events.push({
|
|
24
|
+
event,
|
|
25
|
+
data
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
all() {
|
|
29
|
+
return this.#events;
|
|
30
|
+
}
|
|
31
|
+
size() {
|
|
32
|
+
return this.#events.length;
|
|
33
|
+
}
|
|
34
|
+
exists(event, finder) {
|
|
35
|
+
return !!this.find(event, finder);
|
|
36
|
+
}
|
|
37
|
+
find(event, finder) {
|
|
38
|
+
return this.#events.find((bufferedEvent) => {
|
|
39
|
+
if (!finder) return bufferedEvent.event === event;
|
|
40
|
+
return bufferedEvent.event === event && finder(bufferedEvent);
|
|
41
|
+
}) || null;
|
|
42
|
+
}
|
|
43
|
+
assertEmitted(event, finder) {
|
|
44
|
+
if (!this.exists(event, finder)) throw new AssertionError({
|
|
45
|
+
message: is.class(event) ? `Expected "[class ${event.name}]" event to be emitted` : `Expected "${String(event)}" event to be emitted`,
|
|
46
|
+
expected: true,
|
|
47
|
+
actual: false,
|
|
48
|
+
operator: "strictEqual",
|
|
49
|
+
stackStartFn: this.assertEmitted
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
assertEmittedCount(event, count) {
|
|
53
|
+
const actual = this.all().filter((bufferedEvent) => bufferedEvent.event === event).length;
|
|
54
|
+
if (actual !== count) throw new AssertionError({
|
|
55
|
+
message: `Expected "${is.class(event) ? `[class ${event.name}]` : String(event)}" event to be emitted "${count}" ${string.pluralize("time", count)}, instead it was emitted "${actual}" ${string.pluralize("time", actual)}`,
|
|
56
|
+
actual,
|
|
57
|
+
expected: count
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
assertNotEmitted(event, finder) {
|
|
61
|
+
if (this.exists(event, finder)) throw new AssertionError({
|
|
62
|
+
message: is.class(event) ? `Unexpected "[class ${event.name}]" event was emitted` : `Unexpected "${String(event)}" event was emitted`,
|
|
63
|
+
expected: false,
|
|
64
|
+
actual: true,
|
|
65
|
+
operator: "strictEqual",
|
|
66
|
+
stackStartFn: this.assertNotEmitted
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
assertNoneEmitted() {
|
|
70
|
+
const eventsSize = this.size();
|
|
71
|
+
if (eventsSize > 0) throw new AssertionError(Object.assign({
|
|
72
|
+
message: `Expected zero events to be emitted. Instead received "${eventsSize}" ${string.pluralize("event", eventsSize)}`,
|
|
73
|
+
expected: 0,
|
|
74
|
+
actual: eventsSize,
|
|
75
|
+
operator: "strictEqual",
|
|
76
|
+
stackStartFn: this.assertNoneEmitted
|
|
77
|
+
}, { showDiff: true }));
|
|
78
|
+
}
|
|
79
|
+
flush() {
|
|
80
|
+
this.#events = [];
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var tracing_channels_exports = /* @__PURE__ */ __export({ eventDispatch: () => eventDispatch });
|
|
84
|
+
const eventDispatch = diagnostics_channel.tracingChannel("adonisjs.event.dispatch");
|
|
85
|
+
var Emitter = class {
|
|
86
|
+
#eventsClassSymbols = /* @__PURE__ */ new Map();
|
|
87
|
+
#eventsListeners = /* @__PURE__ */ new Map();
|
|
88
|
+
#transport = new Emittery();
|
|
89
|
+
#eventsBuffer;
|
|
90
|
+
#eventsToFake = /* @__PURE__ */ new Set();
|
|
91
|
+
#errorHandler;
|
|
92
|
+
#app;
|
|
93
|
+
get eventsListeners() {
|
|
94
|
+
return this.#eventsListeners;
|
|
95
|
+
}
|
|
96
|
+
constructor(app) {
|
|
97
|
+
this.#app = app;
|
|
98
|
+
}
|
|
99
|
+
#isConstructor(value) {
|
|
100
|
+
return is.class(value);
|
|
101
|
+
}
|
|
102
|
+
#getEventClassSymbol(event) {
|
|
103
|
+
if (!this.#eventsClassSymbols.has(event)) this.#eventsClassSymbols.set(event, Symbol(event.name));
|
|
104
|
+
return this.#eventsClassSymbols.get(event);
|
|
105
|
+
}
|
|
106
|
+
#resolveEvent(event) {
|
|
107
|
+
if (this.#isConstructor(event)) return this.#getEventClassSymbol(event);
|
|
108
|
+
return event;
|
|
109
|
+
}
|
|
110
|
+
#getEventListeners(event) {
|
|
111
|
+
if (!this.#eventsListeners.has(event)) this.#eventsListeners.set(event, /* @__PURE__ */ new Map());
|
|
112
|
+
return this.#eventsListeners.get(event);
|
|
113
|
+
}
|
|
114
|
+
#normalizeEventListener(listener) {
|
|
115
|
+
if (typeof listener === "string") {
|
|
116
|
+
const parts = listener.split(".");
|
|
117
|
+
const method = parts.length === 1 ? "handle" : parts.pop();
|
|
118
|
+
const moduleRefId = parts.join(".");
|
|
119
|
+
return moduleImporter(() => this.#app.import(moduleRefId), method).toCallable(this.#app.container);
|
|
120
|
+
}
|
|
121
|
+
if (Array.isArray(listener)) {
|
|
122
|
+
const listenerModule = listener[0];
|
|
123
|
+
const method = listener[1] || "handle";
|
|
124
|
+
if (this.#isConstructor(listenerModule)) return moduleCaller(listenerModule, method).toCallable(this.#app.container);
|
|
125
|
+
return moduleImporter(listenerModule, method).toCallable(this.#app.container);
|
|
126
|
+
}
|
|
127
|
+
return listener;
|
|
128
|
+
}
|
|
129
|
+
#resolveEventListener(event, listener) {
|
|
130
|
+
const eventListeners = this.#getEventListeners(event);
|
|
131
|
+
if (!eventListeners.has(listener)) eventListeners.set(listener, this.#normalizeEventListener(listener));
|
|
132
|
+
return eventListeners.get(listener);
|
|
133
|
+
}
|
|
134
|
+
onError(callback) {
|
|
135
|
+
this.#errorHandler = callback;
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
listen(event, listeners) {
|
|
139
|
+
listeners.forEach((listener) => this.on(event, [listener, "handle"]));
|
|
140
|
+
}
|
|
141
|
+
on(event, listener) {
|
|
142
|
+
if (debug_default.enabled) debug_default("registering event listener, event: %O, listener: %O", event, listener);
|
|
143
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
144
|
+
const normalizedListener = this.#resolveEventListener(event, listener);
|
|
145
|
+
this.#transport.on(normalizedEvent, normalizedListener);
|
|
146
|
+
return () => this.off(event, listener);
|
|
147
|
+
}
|
|
148
|
+
listenIf(condition, event, listener) {
|
|
149
|
+
if (!condition || typeof condition === "function" && !condition()) return () => {};
|
|
150
|
+
return this.on(event, listener);
|
|
151
|
+
}
|
|
152
|
+
once(event, listener) {
|
|
153
|
+
if (debug_default.enabled) debug_default("registering one time event listener, event: %O, listener: %O", event, listener);
|
|
154
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
155
|
+
const normalizedListener = this.#normalizeEventListener(listener);
|
|
156
|
+
const off = this.#transport.on(normalizedEvent, async (data) => {
|
|
157
|
+
off();
|
|
158
|
+
debug_default("removing one time event listener, event: %O", event);
|
|
159
|
+
await normalizedListener(data);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
onAny(listener) {
|
|
163
|
+
return this.#transport.onAny(listener);
|
|
164
|
+
}
|
|
165
|
+
async emit(event, data) {
|
|
166
|
+
if (this.#eventsToFake.has(event) || this.#eventsToFake.has("*")) {
|
|
167
|
+
debug_default("faking emit. event: %O, data: %O", event, data);
|
|
168
|
+
this.#eventsBuffer.add(event, data);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
173
|
+
await eventDispatch.tracePromise(this.#transport.emit, eventDispatch.hasSubscribers ? {
|
|
174
|
+
event,
|
|
175
|
+
data
|
|
176
|
+
} : void 0, this.#transport, normalizedEvent, data);
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (this.#errorHandler) this.#errorHandler(event, error, data);
|
|
179
|
+
else throw error;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async emitSerial(event, data) {
|
|
183
|
+
if (this.#eventsToFake.has(event) || this.#eventsToFake.has("*")) {
|
|
184
|
+
debug_default("faking emit. event: %O, data: %O", event, data);
|
|
185
|
+
this.#eventsBuffer.add(event, data);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
190
|
+
await eventDispatch.tracePromise(this.#transport.emitSerial, eventDispatch.hasSubscribers ? {
|
|
191
|
+
event,
|
|
192
|
+
data
|
|
193
|
+
} : void 0, this.#transport, normalizedEvent, data);
|
|
194
|
+
} catch (error) {
|
|
195
|
+
if (this.#errorHandler) this.#errorHandler(event, error, data);
|
|
196
|
+
else throw error;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
off(event, listener) {
|
|
200
|
+
if (debug_default.enabled) debug_default("removing listener, event: %O, listener: %O", event, listener);
|
|
201
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
202
|
+
const listeners = this.#getEventListeners(event);
|
|
203
|
+
const normalizedListener = listeners.get(listener);
|
|
204
|
+
if (!normalizedListener) return;
|
|
205
|
+
listeners.delete(listener);
|
|
206
|
+
this.#transport.off(normalizedEvent, normalizedListener);
|
|
207
|
+
}
|
|
208
|
+
offAny(listener) {
|
|
209
|
+
this.#transport.offAny(listener);
|
|
210
|
+
return this;
|
|
211
|
+
}
|
|
212
|
+
clearListener(event, listener) {
|
|
213
|
+
return this.off(event, listener);
|
|
214
|
+
}
|
|
215
|
+
clearListeners(event) {
|
|
216
|
+
debug_default("clearing all listeners for event %O", event);
|
|
217
|
+
this.#transport.clearListeners(this.#resolveEvent(event));
|
|
218
|
+
this.#eventsListeners.delete(event);
|
|
219
|
+
}
|
|
220
|
+
clearAllListeners() {
|
|
221
|
+
debug_default("clearing all event listeners");
|
|
222
|
+
this.#transport.clearListeners();
|
|
223
|
+
this.#eventsListeners.clear();
|
|
224
|
+
}
|
|
225
|
+
listenerCount(event) {
|
|
226
|
+
return this.#transport.listenerCount(event ? this.#resolveEvent(event) : void 0);
|
|
227
|
+
}
|
|
228
|
+
hasListeners(event) {
|
|
229
|
+
return this.listenerCount(event) > 0;
|
|
230
|
+
}
|
|
231
|
+
fake(events) {
|
|
232
|
+
this.restore();
|
|
233
|
+
this.#eventsBuffer = new EventsBuffer();
|
|
234
|
+
if (!events) {
|
|
235
|
+
debug_default("faking all events");
|
|
236
|
+
this.#eventsToFake.add("*");
|
|
237
|
+
} else {
|
|
238
|
+
debug_default("faking events: %O", events);
|
|
239
|
+
events.forEach((event) => this.#eventsToFake.add(event));
|
|
240
|
+
}
|
|
241
|
+
return this.#eventsBuffer;
|
|
242
|
+
}
|
|
243
|
+
restore() {
|
|
244
|
+
debug_default("restoring existing fakes");
|
|
245
|
+
this.#eventsToFake.clear();
|
|
246
|
+
this.#eventsBuffer?.flush();
|
|
247
|
+
this.#eventsBuffer = void 0;
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
export { tracing_channels_exports as n, Emitter as t };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { EmitterFactory } from './emitter.
|
|
1
|
+
export { EmitterFactory } from './emitter.ts';
|
package/build/factories/main.js
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Emitter
|
|
3
|
-
} from "../chunk-X4QBBFBR.js";
|
|
4
|
-
|
|
5
|
-
// factories/emitter.ts
|
|
1
|
+
import { t as Emitter } from "../emitter-Bpdlo50r.js";
|
|
6
2
|
var EmitterFactory = class {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
create(app) {
|
|
11
|
-
return new Emitter(app);
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
export {
|
|
15
|
-
EmitterFactory
|
|
3
|
+
create(app) {
|
|
4
|
+
return new Emitter(app);
|
|
5
|
+
}
|
|
16
6
|
};
|
|
7
|
+
export { EmitterFactory };
|
package/build/index.d.ts
CHANGED
|
@@ -1,3 +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
3
|
export * as tracingChannels from './src/tracing_channels.ts';
|
package/build/index.js
CHANGED
|
@@ -1,38 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Emitter,
|
|
3
|
-
tracing_channels_exports
|
|
4
|
-
} from "./chunk-X4QBBFBR.js";
|
|
5
|
-
|
|
6
|
-
// src/base_event.ts
|
|
1
|
+
import { n as tracing_channels_exports, t as Emitter } from "./emitter-Bpdlo50r.js";
|
|
7
2
|
import { RuntimeException } from "@poppinss/utils/exception";
|
|
8
3
|
var BaseEvent = class {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
static useEmitter(emitter) {
|
|
19
|
-
this.emitter = emitter;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Dispatch the current class as an event. The method takes the arguments
|
|
23
|
-
* accepted by the class constructor.
|
|
24
|
-
*/
|
|
25
|
-
static async dispatch(...args) {
|
|
26
|
-
if (!this.emitter) {
|
|
27
|
-
throw new RuntimeException(
|
|
28
|
-
`Cannot dispatch "${this.name}" event. Make sure to pass emitter to the "BaseEvent" class for dispatch method to work`
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
return this.emitter.emit(this, new this(...args));
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
export {
|
|
35
|
-
BaseEvent,
|
|
36
|
-
Emitter,
|
|
37
|
-
tracing_channels_exports as tracingChannels
|
|
4
|
+
constructor(..._) {}
|
|
5
|
+
static emitter;
|
|
6
|
+
static useEmitter(emitter) {
|
|
7
|
+
this.emitter = emitter;
|
|
8
|
+
}
|
|
9
|
+
static async dispatch(...args) {
|
|
10
|
+
if (!this.emitter) throw new RuntimeException(`Cannot dispatch "${this.name}" event. Make sure to pass emitter to the "BaseEvent" class for dispatch method to work`);
|
|
11
|
+
return this.emitter.emit(this, new this(...args));
|
|
12
|
+
}
|
|
38
13
|
};
|
|
14
|
+
export { BaseEvent, Emitter, tracing_channels_exports as tracingChannels };
|
|
@@ -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/debug.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("util").DebugLogger;
|
|
1
|
+
declare const _default: import("node:util").DebugLogger;
|
|
2
2
|
export default _default;
|
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,37 @@ 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
|
+
* @param event - The event to remove listener from
|
|
120
|
+
* @param listener - The listener to remove
|
|
80
121
|
*/
|
|
81
122
|
clearListener(event: keyof EventsList | Constructor<any>, listener: Listener<any, Constructor<any>>): void;
|
|
82
123
|
/**
|
|
83
124
|
* Clear all listeners for a specific event
|
|
125
|
+
*
|
|
126
|
+
* @param event - The event to clear listeners for
|
|
84
127
|
*/
|
|
85
128
|
clearListeners(event: keyof EventsList | Constructor<any>): void;
|
|
86
129
|
/**
|
|
@@ -89,10 +132,16 @@ export declare class Emitter<EventsList extends Record<string | symbol | number,
|
|
|
89
132
|
clearAllListeners(): void;
|
|
90
133
|
/**
|
|
91
134
|
* Get count of listeners for a given event or all the events
|
|
135
|
+
*
|
|
136
|
+
* @param event - The event to count listeners for (optional)
|
|
137
|
+
* @returns The number of listeners
|
|
92
138
|
*/
|
|
93
139
|
listenerCount(event?: keyof EventsList | Constructor<any>): number;
|
|
94
140
|
/**
|
|
95
141
|
* Find if an event has one or more listeners
|
|
142
|
+
*
|
|
143
|
+
* @param event - The event to check listeners for (optional)
|
|
144
|
+
* @returns True if the event has listeners
|
|
96
145
|
*/
|
|
97
146
|
hasListeners(event?: keyof EventsList | Constructor<any>): boolean;
|
|
98
147
|
/**
|
|
@@ -104,6 +153,9 @@ export declare class Emitter<EventsList extends Record<string | symbol | number,
|
|
|
104
153
|
*
|
|
105
154
|
* Calling this method one than once drops the existing fakes and
|
|
106
155
|
* creates new one.
|
|
156
|
+
*
|
|
157
|
+
* @param events - Array of events to fake (optional, defaults to all events)
|
|
158
|
+
* @returns The events buffer for assertions
|
|
107
159
|
*/
|
|
108
160
|
fake(events?: (keyof EventsList | Constructor<any>)[]): EventsBuffer<EventsList>;
|
|
109
161
|
/**
|
|
@@ -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
|
/**
|
|
@@ -3,4 +3,4 @@ import { type EventDispatchData } from './types.ts';
|
|
|
3
3
|
/**
|
|
4
4
|
* Traces event.emit method calls
|
|
5
5
|
*/
|
|
6
|
-
export declare const eventDispatch: diagnostics_channel.TracingChannel<"adonisjs
|
|
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,25 @@ 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
|
}
|
package/build/src/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
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.3",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24.0.0"
|
|
7
7
|
},
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"typecheck": "tsc --noEmit",
|
|
29
29
|
"precompile": "npm run lint && npm run clean",
|
|
30
30
|
"clean": "del-cli build",
|
|
31
|
-
"compile": "
|
|
31
|
+
"compile": "tsdown && tsc --emitDeclarationOnly --declaration",
|
|
32
32
|
"build": "npm run compile",
|
|
33
33
|
"version": "npm run build",
|
|
34
34
|
"prepublishOnly": "npm run build",
|
|
@@ -36,36 +36,36 @@
|
|
|
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": "^11.0.0-next.
|
|
39
|
+
"@adonisjs/application": "^9.0.0-next.12",
|
|
40
|
+
"@adonisjs/config": "^6.1.0-next.0",
|
|
41
|
+
"@adonisjs/eslint-config": "^3.0.0-next.5",
|
|
42
|
+
"@adonisjs/fold": "^11.0.0-next.3",
|
|
43
43
|
"@adonisjs/prettier-config": "^1.4.5",
|
|
44
|
-
"@adonisjs/tsconfig": "^2.0.0-next.
|
|
45
|
-
"@japa/assert": "^4.
|
|
46
|
-
"@japa/expect-type": "^2.0.
|
|
47
|
-
"@japa/file-system": "^
|
|
48
|
-
"@japa/runner": "^
|
|
49
|
-
"@poppinss/ts-exec": "^1.4.
|
|
50
|
-
"@release-it/conventional-changelog": "^10.0.
|
|
51
|
-
"@types/node": "^
|
|
44
|
+
"@adonisjs/tsconfig": "^2.0.0-next.3",
|
|
45
|
+
"@japa/assert": "^4.2.0",
|
|
46
|
+
"@japa/expect-type": "^2.0.4",
|
|
47
|
+
"@japa/file-system": "^3.0.0",
|
|
48
|
+
"@japa/runner": "^5.0.0",
|
|
49
|
+
"@poppinss/ts-exec": "^1.4.1",
|
|
50
|
+
"@release-it/conventional-changelog": "^10.0.3",
|
|
51
|
+
"@types/node": "^25.0.2",
|
|
52
52
|
"c8": "^10.1.3",
|
|
53
|
-
"cross-env": "^10.
|
|
54
|
-
"del-cli": "^
|
|
55
|
-
"eslint": "^9.
|
|
56
|
-
"prettier": "^3.
|
|
57
|
-
"release-it": "^19.0
|
|
58
|
-
"
|
|
59
|
-
"typescript": "^5.
|
|
53
|
+
"cross-env": "^10.1.0",
|
|
54
|
+
"del-cli": "^7.0.0",
|
|
55
|
+
"eslint": "^9.39.2",
|
|
56
|
+
"prettier": "^3.7.4",
|
|
57
|
+
"release-it": "^19.1.0",
|
|
58
|
+
"tsdown": "^0.17.4",
|
|
59
|
+
"typescript": "^5.9.3"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@poppinss/utils": "^7.0.0-next.
|
|
63
|
-
"@sindresorhus/is": "^7.
|
|
62
|
+
"@poppinss/utils": "^7.0.0-next.4",
|
|
63
|
+
"@sindresorhus/is": "^7.1.1",
|
|
64
64
|
"emittery": "^1.2.0"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"@adonisjs/application": "^9.0.0-next.
|
|
68
|
-
"@adonisjs/fold": "^11.0.0-next.
|
|
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": {
|
|
@@ -85,7 +85,7 @@
|
|
|
85
85
|
"access": "public",
|
|
86
86
|
"provenance": true
|
|
87
87
|
},
|
|
88
|
-
"
|
|
88
|
+
"tsdown": {
|
|
89
89
|
"entry": [
|
|
90
90
|
"./index.ts",
|
|
91
91
|
"./src/types.ts",
|
|
@@ -94,8 +94,11 @@
|
|
|
94
94
|
"outDir": "./build",
|
|
95
95
|
"clean": true,
|
|
96
96
|
"format": "esm",
|
|
97
|
+
"minify": "dce-only",
|
|
98
|
+
"fixedExtension": false,
|
|
97
99
|
"dts": false,
|
|
98
|
-
"
|
|
100
|
+
"treeshake": false,
|
|
101
|
+
"sourcemaps": false,
|
|
99
102
|
"target": "esnext"
|
|
100
103
|
},
|
|
101
104
|
"release-it": {
|
package/build/chunk-X4QBBFBR.js
DELETED
|
@@ -1,464 +0,0 @@
|
|
|
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
|
-
|
|
15
|
-
// src/emitter.ts
|
|
16
|
-
import is2 from "@sindresorhus/is";
|
|
17
|
-
import Emittery from "emittery";
|
|
18
|
-
import { moduleCaller, moduleImporter } from "@adonisjs/fold";
|
|
19
|
-
|
|
20
|
-
// src/debug.ts
|
|
21
|
-
import { debuglog } from "util";
|
|
22
|
-
var debug_default = debuglog("adonisjs:events");
|
|
23
|
-
|
|
24
|
-
// src/events_buffer.ts
|
|
25
|
-
import is from "@sindresorhus/is";
|
|
26
|
-
import string from "@poppinss/utils/string";
|
|
27
|
-
import { AssertionError } from "assert";
|
|
28
|
-
var EventsBuffer = class {
|
|
29
|
-
/**
|
|
30
|
-
* Buffered events
|
|
31
|
-
*/
|
|
32
|
-
#events = [];
|
|
33
|
-
/**
|
|
34
|
-
* Track emitted event
|
|
35
|
-
*/
|
|
36
|
-
add(event, data) {
|
|
37
|
-
this.#events.push({ event, data });
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Get all the emitted events
|
|
41
|
-
*/
|
|
42
|
-
all() {
|
|
43
|
-
return this.#events;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Returns the size of captured events
|
|
47
|
-
*/
|
|
48
|
-
size() {
|
|
49
|
-
return this.#events.length;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Find if an event was emitted
|
|
53
|
-
*/
|
|
54
|
-
exists(event, finder) {
|
|
55
|
-
return !!this.find(event, finder);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Find a specific event
|
|
59
|
-
*/
|
|
60
|
-
find(event, finder) {
|
|
61
|
-
return this.#events.find((bufferedEvent) => {
|
|
62
|
-
if (!finder) {
|
|
63
|
-
return bufferedEvent.event === event;
|
|
64
|
-
}
|
|
65
|
-
return bufferedEvent.event === event && finder(bufferedEvent);
|
|
66
|
-
}) || null;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Assert a given event has been emitted
|
|
70
|
-
*/
|
|
71
|
-
assertEmitted(event, finder) {
|
|
72
|
-
const hasEvent = this.exists(event, finder);
|
|
73
|
-
if (!hasEvent) {
|
|
74
|
-
const message = is.class(event) ? `Expected "[class ${event.name}]" event to be emitted` : `Expected "${String(event)}" event to be emitted`;
|
|
75
|
-
throw new AssertionError({
|
|
76
|
-
message,
|
|
77
|
-
expected: true,
|
|
78
|
-
actual: false,
|
|
79
|
-
operator: "strictEqual",
|
|
80
|
-
stackStartFn: this.assertEmitted
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Assert number of times an event has been emitted
|
|
86
|
-
*/
|
|
87
|
-
assertEmittedCount(event, count) {
|
|
88
|
-
const actual = this.all().filter((bufferedEvent) => bufferedEvent.event === event).length;
|
|
89
|
-
if (actual !== count) {
|
|
90
|
-
const eventName = is.class(event) ? `[class ${event.name}]` : String(event);
|
|
91
|
-
throw new AssertionError({
|
|
92
|
-
message: `Expected "${eventName}" event to be emitted "${count}" ${string.pluralize(
|
|
93
|
-
"time",
|
|
94
|
-
count
|
|
95
|
-
)}, instead it was emitted "${actual}" ${string.pluralize("time", actual)}`,
|
|
96
|
-
actual,
|
|
97
|
-
expected: count
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Assert a given event has been not been emitted
|
|
103
|
-
*/
|
|
104
|
-
assertNotEmitted(event, finder) {
|
|
105
|
-
const hasEvent = this.exists(event, finder);
|
|
106
|
-
if (hasEvent) {
|
|
107
|
-
const isClass = is.class(event);
|
|
108
|
-
const message = isClass ? `Unexpected "[class ${event.name}]" event was emitted` : `Unexpected "${String(event)}" event was emitted`;
|
|
109
|
-
throw new AssertionError({
|
|
110
|
-
message,
|
|
111
|
-
expected: false,
|
|
112
|
-
actual: true,
|
|
113
|
-
operator: "strictEqual",
|
|
114
|
-
stackStartFn: this.assertNotEmitted
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Assert a given event has been not been emitted
|
|
120
|
-
*/
|
|
121
|
-
assertNoneEmitted() {
|
|
122
|
-
const eventsSize = this.size();
|
|
123
|
-
if (eventsSize > 0) {
|
|
124
|
-
throw new AssertionError(
|
|
125
|
-
Object.assign(
|
|
126
|
-
{
|
|
127
|
-
message: `Expected zero events to be emitted. Instead received "${eventsSize}" ${string.pluralize(
|
|
128
|
-
"event",
|
|
129
|
-
eventsSize
|
|
130
|
-
)}`,
|
|
131
|
-
expected: 0,
|
|
132
|
-
actual: eventsSize,
|
|
133
|
-
operator: "strictEqual",
|
|
134
|
-
stackStartFn: this.assertNoneEmitted
|
|
135
|
-
},
|
|
136
|
-
{
|
|
137
|
-
showDiff: true
|
|
138
|
-
}
|
|
139
|
-
)
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Flush events collected within memory
|
|
145
|
-
*/
|
|
146
|
-
flush() {
|
|
147
|
-
this.#events = [];
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
// src/emitter.ts
|
|
152
|
-
var Emitter = class {
|
|
153
|
-
/**
|
|
154
|
-
* Event classes to symbols mapping. We need symbols as emittery
|
|
155
|
-
* does not support class based event names
|
|
156
|
-
*/
|
|
157
|
-
#eventsClassSymbols = /* @__PURE__ */ new Map();
|
|
158
|
-
/**
|
|
159
|
-
* A collection of events and their listeners. We do not track listeners
|
|
160
|
-
* listening for events only once
|
|
161
|
-
*/
|
|
162
|
-
#eventsListeners = /* @__PURE__ */ new Map();
|
|
163
|
-
/**
|
|
164
|
-
* Underlying transport to emit events
|
|
165
|
-
*/
|
|
166
|
-
#transport = new Emittery();
|
|
167
|
-
/**
|
|
168
|
-
* Events buffer. The events are collected inside an in-memory
|
|
169
|
-
* buffer during fakes
|
|
170
|
-
*/
|
|
171
|
-
#eventsBuffer;
|
|
172
|
-
/**
|
|
173
|
-
* A set of events to fake
|
|
174
|
-
*/
|
|
175
|
-
#eventsToFake = /* @__PURE__ */ new Set();
|
|
176
|
-
/**
|
|
177
|
-
* Error handler to catch all errors thrown by listeners
|
|
178
|
-
*/
|
|
179
|
-
#errorHandler;
|
|
180
|
-
/**
|
|
181
|
-
* Reference to AdonisJS application, we need the application root
|
|
182
|
-
* and container reference from it.
|
|
183
|
-
*/
|
|
184
|
-
#app;
|
|
185
|
-
/**
|
|
186
|
-
* Returns a map of events and their registered listeners. The
|
|
187
|
-
* map key is the event name and the value is another map
|
|
188
|
-
* of listeners.
|
|
189
|
-
*
|
|
190
|
-
* The listeners map key is the original binding listener
|
|
191
|
-
* and the value is a callback function.
|
|
192
|
-
*/
|
|
193
|
-
get eventsListeners() {
|
|
194
|
-
return this.#eventsListeners;
|
|
195
|
-
}
|
|
196
|
-
constructor(app) {
|
|
197
|
-
this.#app = app;
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Check if the value is a constructor and narrow down its types
|
|
201
|
-
*/
|
|
202
|
-
#isConstructor(value) {
|
|
203
|
-
return is2.class(value);
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Returns the symbol for a class based event.
|
|
207
|
-
*/
|
|
208
|
-
#getEventClassSymbol(event) {
|
|
209
|
-
if (!this.#eventsClassSymbols.has(event)) {
|
|
210
|
-
this.#eventsClassSymbols.set(event, Symbol(event.name));
|
|
211
|
-
}
|
|
212
|
-
return this.#eventsClassSymbols.get(event);
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Normalizes the event to emittery supported data types. The class
|
|
216
|
-
* constructors are cached against a unique symbol.
|
|
217
|
-
*/
|
|
218
|
-
#resolveEvent(event) {
|
|
219
|
-
if (this.#isConstructor(event)) {
|
|
220
|
-
return this.#getEventClassSymbol(event);
|
|
221
|
-
}
|
|
222
|
-
return event;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Returns the event listeners map
|
|
226
|
-
*/
|
|
227
|
-
#getEventListeners(event) {
|
|
228
|
-
if (!this.#eventsListeners.has(event)) {
|
|
229
|
-
this.#eventsListeners.set(event, /* @__PURE__ */ new Map());
|
|
230
|
-
}
|
|
231
|
-
return this.#eventsListeners.get(event);
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Normalizes the event listener to a function that can be passed to
|
|
235
|
-
* emittery.
|
|
236
|
-
*/
|
|
237
|
-
#normalizeEventListener(listener) {
|
|
238
|
-
if (typeof listener === "string") {
|
|
239
|
-
const parts = listener.split(".");
|
|
240
|
-
const method = parts.length === 1 ? "handle" : parts.pop();
|
|
241
|
-
const moduleRefId = parts.join(".");
|
|
242
|
-
return moduleImporter(() => this.#app.import(moduleRefId), method).toCallable(
|
|
243
|
-
this.#app.container
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
if (Array.isArray(listener)) {
|
|
247
|
-
const listenerModule = listener[0];
|
|
248
|
-
const method = listener[1] || "handle";
|
|
249
|
-
if (this.#isConstructor(listenerModule)) {
|
|
250
|
-
return moduleCaller(listenerModule, method).toCallable(this.#app.container);
|
|
251
|
-
}
|
|
252
|
-
return moduleImporter(listenerModule, method).toCallable(this.#app.container);
|
|
253
|
-
}
|
|
254
|
-
return listener;
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Resolves the event listener either from the cache or normalizes
|
|
258
|
-
* it and stores it inside the cache
|
|
259
|
-
*/
|
|
260
|
-
#resolveEventListener(event, listener) {
|
|
261
|
-
const eventListeners = this.#getEventListeners(event);
|
|
262
|
-
if (!eventListeners.has(listener)) {
|
|
263
|
-
eventListeners.set(listener, this.#normalizeEventListener(listener));
|
|
264
|
-
}
|
|
265
|
-
return eventListeners.get(listener);
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* Register a global error handler
|
|
269
|
-
*/
|
|
270
|
-
onError(callback) {
|
|
271
|
-
this.#errorHandler = callback;
|
|
272
|
-
return this;
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Bind multiple listeners to listen for a single event. The listen
|
|
276
|
-
* method is a convenience helper to be used with class based
|
|
277
|
-
* events and listeners.
|
|
278
|
-
*/
|
|
279
|
-
listen(event, listeners) {
|
|
280
|
-
listeners.forEach((listener) => this.on(event, [listener, "handle"]));
|
|
281
|
-
}
|
|
282
|
-
on(event, listener) {
|
|
283
|
-
if (debug_default.enabled) {
|
|
284
|
-
debug_default("registering event listener, event: %O, listener: %O", event, listener);
|
|
285
|
-
}
|
|
286
|
-
const normalizedEvent = this.#resolveEvent(event);
|
|
287
|
-
const normalizedListener = this.#resolveEventListener(event, listener);
|
|
288
|
-
this.#transport.on(normalizedEvent, normalizedListener);
|
|
289
|
-
return () => this.off(event, listener);
|
|
290
|
-
}
|
|
291
|
-
listenIf(condition, event, listener) {
|
|
292
|
-
if (!condition || typeof condition === "function" && !condition()) {
|
|
293
|
-
return () => {
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
return this.on(event, listener);
|
|
297
|
-
}
|
|
298
|
-
once(event, listener) {
|
|
299
|
-
if (debug_default.enabled) {
|
|
300
|
-
debug_default("registering one time event listener, event: %O, listener: %O", event, listener);
|
|
301
|
-
}
|
|
302
|
-
const normalizedEvent = this.#resolveEvent(event);
|
|
303
|
-
const normalizedListener = this.#normalizeEventListener(listener);
|
|
304
|
-
const off = this.#transport.on(normalizedEvent, async (data) => {
|
|
305
|
-
off();
|
|
306
|
-
debug_default("removing one time event listener, event: %O", event);
|
|
307
|
-
await normalizedListener(data);
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
/**
|
|
311
|
-
* Attach a listener to listen for all the events. Wildcard listeners
|
|
312
|
-
* can only be defined as inline callbacks.
|
|
313
|
-
*/
|
|
314
|
-
onAny(listener) {
|
|
315
|
-
return this.#transport.onAny(listener);
|
|
316
|
-
}
|
|
317
|
-
async emit(event, data) {
|
|
318
|
-
if (this.#eventsToFake.has(event) || this.#eventsToFake.has("*")) {
|
|
319
|
-
debug_default("faking emit. event: %O, data: %O", event, data);
|
|
320
|
-
this.#eventsBuffer.add(event, data);
|
|
321
|
-
return;
|
|
322
|
-
}
|
|
323
|
-
try {
|
|
324
|
-
const normalizedEvent = this.#resolveEvent(event);
|
|
325
|
-
await eventDispatch.tracePromise(
|
|
326
|
-
this.#transport.emit,
|
|
327
|
-
{
|
|
328
|
-
event,
|
|
329
|
-
data
|
|
330
|
-
},
|
|
331
|
-
this.#transport,
|
|
332
|
-
normalizedEvent,
|
|
333
|
-
data
|
|
334
|
-
);
|
|
335
|
-
} catch (error) {
|
|
336
|
-
if (this.#errorHandler) {
|
|
337
|
-
this.#errorHandler(event, error, data);
|
|
338
|
-
} else {
|
|
339
|
-
throw error;
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
async emitSerial(event, data) {
|
|
344
|
-
if (this.#eventsToFake.has(event) || this.#eventsToFake.has("*")) {
|
|
345
|
-
debug_default("faking emit. event: %O, data: %O", event, data);
|
|
346
|
-
this.#eventsBuffer.add(event, data);
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
try {
|
|
350
|
-
const normalizedEvent = this.#resolveEvent(event);
|
|
351
|
-
await eventDispatch.tracePromise(
|
|
352
|
-
this.#transport.emitSerial,
|
|
353
|
-
{
|
|
354
|
-
event,
|
|
355
|
-
data
|
|
356
|
-
},
|
|
357
|
-
this.#transport,
|
|
358
|
-
normalizedEvent,
|
|
359
|
-
data
|
|
360
|
-
);
|
|
361
|
-
} catch (error) {
|
|
362
|
-
if (this.#errorHandler) {
|
|
363
|
-
this.#errorHandler(event, error, data);
|
|
364
|
-
} else {
|
|
365
|
-
throw error;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Remove a specific listener for an event
|
|
371
|
-
*/
|
|
372
|
-
off(event, listener) {
|
|
373
|
-
if (debug_default.enabled) {
|
|
374
|
-
debug_default("removing listener, event: %O, listener: %O", event, listener);
|
|
375
|
-
}
|
|
376
|
-
const normalizedEvent = this.#resolveEvent(event);
|
|
377
|
-
const listeners = this.#getEventListeners(event);
|
|
378
|
-
const normalizedListener = listeners.get(listener);
|
|
379
|
-
if (!normalizedListener) {
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
listeners.delete(listener);
|
|
383
|
-
this.#transport.off(normalizedEvent, normalizedListener);
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* Remove a specific listener listening for all the events
|
|
387
|
-
*/
|
|
388
|
-
offAny(listener) {
|
|
389
|
-
this.#transport.offAny(listener);
|
|
390
|
-
return this;
|
|
391
|
-
}
|
|
392
|
-
/**
|
|
393
|
-
* Remove a specific listener for an event
|
|
394
|
-
*
|
|
395
|
-
* @alias "off"
|
|
396
|
-
*/
|
|
397
|
-
clearListener(event, listener) {
|
|
398
|
-
return this.off(event, listener);
|
|
399
|
-
}
|
|
400
|
-
/**
|
|
401
|
-
* Clear all listeners for a specific event
|
|
402
|
-
*/
|
|
403
|
-
clearListeners(event) {
|
|
404
|
-
debug_default("clearing all listeners for event %O", event);
|
|
405
|
-
this.#transport.clearListeners(this.#resolveEvent(event));
|
|
406
|
-
this.#eventsListeners.delete(event);
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Clear all listeners for all the events
|
|
410
|
-
*/
|
|
411
|
-
clearAllListeners() {
|
|
412
|
-
debug_default("clearing all event listeners");
|
|
413
|
-
this.#transport.clearListeners();
|
|
414
|
-
this.#eventsListeners.clear();
|
|
415
|
-
}
|
|
416
|
-
/**
|
|
417
|
-
* Get count of listeners for a given event or all the events
|
|
418
|
-
*/
|
|
419
|
-
listenerCount(event) {
|
|
420
|
-
return this.#transport.listenerCount(event ? this.#resolveEvent(event) : void 0);
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Find if an event has one or more listeners
|
|
424
|
-
*/
|
|
425
|
-
hasListeners(event) {
|
|
426
|
-
return this.listenerCount(event) > 0;
|
|
427
|
-
}
|
|
428
|
-
/**
|
|
429
|
-
* Fake one or more events. The listeners for faked events will
|
|
430
|
-
* not be invoked.
|
|
431
|
-
*
|
|
432
|
-
* The return value is an events buffer that collects all the
|
|
433
|
-
* events within memory.
|
|
434
|
-
*
|
|
435
|
-
* Calling this method one than once drops the existing fakes and
|
|
436
|
-
* creates new one.
|
|
437
|
-
*/
|
|
438
|
-
fake(events) {
|
|
439
|
-
this.restore();
|
|
440
|
-
this.#eventsBuffer = new EventsBuffer();
|
|
441
|
-
if (!events) {
|
|
442
|
-
debug_default("faking all events");
|
|
443
|
-
this.#eventsToFake.add("*");
|
|
444
|
-
} else {
|
|
445
|
-
debug_default("faking events: %O", events);
|
|
446
|
-
events.forEach((event) => this.#eventsToFake.add(event));
|
|
447
|
-
}
|
|
448
|
-
return this.#eventsBuffer;
|
|
449
|
-
}
|
|
450
|
-
/**
|
|
451
|
-
* Restore fakes
|
|
452
|
-
*/
|
|
453
|
-
restore() {
|
|
454
|
-
debug_default("restoring existing fakes");
|
|
455
|
-
this.#eventsToFake.clear();
|
|
456
|
-
this.#eventsBuffer?.flush();
|
|
457
|
-
this.#eventsBuffer = void 0;
|
|
458
|
-
}
|
|
459
|
-
};
|
|
460
|
-
|
|
461
|
-
export {
|
|
462
|
-
tracing_channels_exports,
|
|
463
|
-
Emitter
|
|
464
|
-
};
|