@adonisjs/events 8.4.9-4 → 9.0.0-0
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 +1 -4
- package/build/chunk-VVJERWW5.js +402 -0
- package/build/{src/emitter.d.ts → emitter-253440aa.d.ts} +54 -5
- package/build/factories/main.d.ts +17 -1
- package/build/factories/main.js +16 -9
- package/build/index.d.ts +27 -2
- package/build/index.js +36 -10
- package/build/src/types.d.ts +12 -10
- package/build/src/types.js +0 -9
- package/package.json +35 -25
- package/build/factories/emitter.d.ts +0 -12
- package/build/factories/emitter.js +0 -21
- package/build/src/base_event.d.ts +0 -21
- package/build/src/base_event.js +0 -36
- package/build/src/debug.d.ts +0 -3
- package/build/src/debug.js +0 -10
- package/build/src/emitter.js +0 -320
- package/build/src/events_buffer.d.ts +0 -47
- package/build/src/events_buffer.js +0 -126
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<br />
|
|
4
4
|
|
|
5
|
-
[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]
|
|
5
|
+
[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]
|
|
6
6
|
|
|
7
7
|
## Introduction
|
|
8
8
|
AdonisJS events is an implementation of the event emitter built on top of [emittery](https://github.com/sindresorhus/emittery). Alongside defining event listeners as inline callbacks, you can also bind modules to events and define events as classes.
|
|
@@ -32,6 +32,3 @@ AdonisJS events is open-sourced software licensed under the [MIT license](LICENS
|
|
|
32
32
|
|
|
33
33
|
[license-image]: https://img.shields.io/npm/l/@adonisjs/events?color=blueviolet&style=for-the-badge
|
|
34
34
|
[license-url]: LICENSE.md "license"
|
|
35
|
-
|
|
36
|
-
[synk-image]: https://img.shields.io/snyk/vulnerabilities/github/adonisjs/events?label=Synk%20Vulnerabilities&style=for-the-badge
|
|
37
|
-
[synk-url]: https://snyk.io/test/github/adonisjs/events?targetFile=package.json "synk"
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
// src/emitter.ts
|
|
2
|
+
import is2 from "@sindresorhus/is";
|
|
3
|
+
import Emittery from "emittery";
|
|
4
|
+
import { moduleExpression, moduleCaller, moduleImporter } from "@adonisjs/fold";
|
|
5
|
+
|
|
6
|
+
// src/debug.ts
|
|
7
|
+
import { debuglog } from "node:util";
|
|
8
|
+
var debug_default = debuglog("adonisjs:events");
|
|
9
|
+
|
|
10
|
+
// src/events_buffer.ts
|
|
11
|
+
import is from "@sindresorhus/is";
|
|
12
|
+
import { AssertionError } from "node:assert";
|
|
13
|
+
var EventsBuffer = class {
|
|
14
|
+
/**
|
|
15
|
+
* Buffered events
|
|
16
|
+
*/
|
|
17
|
+
#events = [];
|
|
18
|
+
/**
|
|
19
|
+
* Track emitted event
|
|
20
|
+
*/
|
|
21
|
+
add(event, data) {
|
|
22
|
+
this.#events.push({ event, data });
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Get all the emitted events
|
|
26
|
+
*/
|
|
27
|
+
all() {
|
|
28
|
+
return this.#events;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns the size of captured events
|
|
32
|
+
*/
|
|
33
|
+
size() {
|
|
34
|
+
return this.#events.length;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Find if an event was emitted
|
|
38
|
+
*/
|
|
39
|
+
exists(finder) {
|
|
40
|
+
return !!this.find(finder);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get selected events
|
|
44
|
+
*/
|
|
45
|
+
filter(finder) {
|
|
46
|
+
if (typeof finder === "function" && !is.class(finder)) {
|
|
47
|
+
return this.#events.filter(finder);
|
|
48
|
+
}
|
|
49
|
+
return this.#events.filter((event) => event.event === finder);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Find a specific event
|
|
53
|
+
*/
|
|
54
|
+
find(finder) {
|
|
55
|
+
if (typeof finder === "function" && !is.class(finder)) {
|
|
56
|
+
return this.#events.find(finder) || null;
|
|
57
|
+
}
|
|
58
|
+
return this.#events.find((event) => event.event === finder) || null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Assert a given event has been emitted
|
|
62
|
+
*/
|
|
63
|
+
assertEmitted(finder) {
|
|
64
|
+
const hasEvent = this.exists(finder);
|
|
65
|
+
if (!hasEvent) {
|
|
66
|
+
const isClass = is.class(finder);
|
|
67
|
+
const message = typeof finder === "function" && !isClass ? `Expected callback to find an emitted event` : isClass ? `Expected "${finder.name}" event to be emitted` : `Expected "${String(finder)}" event to be emitted`;
|
|
68
|
+
throw new AssertionError({
|
|
69
|
+
message,
|
|
70
|
+
expected: true,
|
|
71
|
+
actual: false,
|
|
72
|
+
operator: "strictEqual",
|
|
73
|
+
stackStartFn: this.assertEmitted
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Assert a given event has been not been emitted
|
|
79
|
+
*/
|
|
80
|
+
assertNotEmitted(finder) {
|
|
81
|
+
const hasEvent = this.exists(finder);
|
|
82
|
+
if (hasEvent) {
|
|
83
|
+
const isClass = is.class(finder);
|
|
84
|
+
const message = typeof finder === "function" && !isClass ? `Expected callback to not find any event` : isClass ? `Expected "${finder.name}" event to be not emitted` : `Expected "${String(finder)}" event to be not emitted`;
|
|
85
|
+
throw new AssertionError({
|
|
86
|
+
message,
|
|
87
|
+
expected: false,
|
|
88
|
+
actual: true,
|
|
89
|
+
operator: "strictEqual",
|
|
90
|
+
stackStartFn: this.assertNotEmitted
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Assert a given event has been not been emitted
|
|
96
|
+
*/
|
|
97
|
+
assertNoneEmitted() {
|
|
98
|
+
const eventsSize = this.size();
|
|
99
|
+
if (eventsSize > 0) {
|
|
100
|
+
throw new AssertionError(
|
|
101
|
+
Object.assign(
|
|
102
|
+
{
|
|
103
|
+
message: `Expected zero events to be emitted. Instead received "${eventsSize}" event(s)`,
|
|
104
|
+
expected: 0,
|
|
105
|
+
actual: eventsSize,
|
|
106
|
+
operator: "strictEqual",
|
|
107
|
+
stackStartFn: this.assertNoneEmitted
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
showDiff: true
|
|
111
|
+
}
|
|
112
|
+
)
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Flush events collected within memory
|
|
118
|
+
*/
|
|
119
|
+
flush() {
|
|
120
|
+
this.#events = [];
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/emitter.ts
|
|
125
|
+
var Emitter = class {
|
|
126
|
+
/**
|
|
127
|
+
* Event classes to symbols mapping. We need symbols as emittery
|
|
128
|
+
* does not support class based event names
|
|
129
|
+
*/
|
|
130
|
+
#eventsClassSymbols = /* @__PURE__ */ new Map();
|
|
131
|
+
/**
|
|
132
|
+
* A collection of events and their listeners. We do not track listeners
|
|
133
|
+
* listening for events only once
|
|
134
|
+
*/
|
|
135
|
+
#eventsListeners = /* @__PURE__ */ new Map();
|
|
136
|
+
/**
|
|
137
|
+
* Underlying transport to emit events
|
|
138
|
+
*/
|
|
139
|
+
#transport = new Emittery();
|
|
140
|
+
/**
|
|
141
|
+
* Events buffer. The events are collected inside an in-memory
|
|
142
|
+
* buffer during fakes
|
|
143
|
+
*/
|
|
144
|
+
#eventsBuffer;
|
|
145
|
+
/**
|
|
146
|
+
* A set of events to fake
|
|
147
|
+
*/
|
|
148
|
+
#eventsToFake = /* @__PURE__ */ new Set();
|
|
149
|
+
/**
|
|
150
|
+
* Error handler to catch all errors thrown by listeners
|
|
151
|
+
*/
|
|
152
|
+
#errorHandler;
|
|
153
|
+
/**
|
|
154
|
+
* Reference to AdonisJS application, we need the application root
|
|
155
|
+
* and container reference from it.
|
|
156
|
+
*/
|
|
157
|
+
#app;
|
|
158
|
+
/**
|
|
159
|
+
* Returns a map of events and their registered listeners. The
|
|
160
|
+
* map key is the event name and the value is another map
|
|
161
|
+
* of listeners.
|
|
162
|
+
*
|
|
163
|
+
* The listeners map key is the original binding listener
|
|
164
|
+
* and the value is a callback function.
|
|
165
|
+
*/
|
|
166
|
+
get eventsListeners() {
|
|
167
|
+
return this.#eventsListeners;
|
|
168
|
+
}
|
|
169
|
+
constructor(app) {
|
|
170
|
+
this.#app = app;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Returns the symbol for a class based event.
|
|
174
|
+
*/
|
|
175
|
+
#getEventClassSymbol(event) {
|
|
176
|
+
if (!this.#eventsClassSymbols.has(event)) {
|
|
177
|
+
this.#eventsClassSymbols.set(event, Symbol(event.name));
|
|
178
|
+
}
|
|
179
|
+
return this.#eventsClassSymbols.get(event);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Normalizes the event to emittery supported data types. The class
|
|
183
|
+
* constructors are cached against a unique symbol.
|
|
184
|
+
*/
|
|
185
|
+
#resolveEvent(event) {
|
|
186
|
+
if (is2.class(event)) {
|
|
187
|
+
return this.#getEventClassSymbol(event);
|
|
188
|
+
}
|
|
189
|
+
return event;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Returns the event listeners map
|
|
193
|
+
*/
|
|
194
|
+
#getEventListeners(event) {
|
|
195
|
+
if (!this.#eventsListeners.has(event)) {
|
|
196
|
+
this.#eventsListeners.set(event, /* @__PURE__ */ new Map());
|
|
197
|
+
}
|
|
198
|
+
return this.#eventsListeners.get(event);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Normalizes the event listener to a function that can be passed to
|
|
202
|
+
* emittery.
|
|
203
|
+
*/
|
|
204
|
+
#normalizeEventListener(listener) {
|
|
205
|
+
if (typeof listener === "string") {
|
|
206
|
+
return moduleExpression(listener, this.#app.appRoot.toString()).toCallable(
|
|
207
|
+
this.#app.container
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
if (Array.isArray(listener)) {
|
|
211
|
+
const listenerModule = listener[0];
|
|
212
|
+
const method = listener[1] || "handle";
|
|
213
|
+
if (is2.class(listenerModule)) {
|
|
214
|
+
return moduleCaller(listenerModule, method).toCallable(this.#app.container);
|
|
215
|
+
}
|
|
216
|
+
return moduleImporter(listenerModule, method).toCallable(this.#app.container);
|
|
217
|
+
}
|
|
218
|
+
return listener;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Resolves the event listener either from the cache or normalizes
|
|
222
|
+
* it and stores it inside the cache
|
|
223
|
+
*/
|
|
224
|
+
#resolveEventListener(event, listener) {
|
|
225
|
+
const eventListeners = this.#getEventListeners(event);
|
|
226
|
+
if (!eventListeners.has(listener)) {
|
|
227
|
+
eventListeners.set(listener, this.#normalizeEventListener(listener));
|
|
228
|
+
}
|
|
229
|
+
return eventListeners.get(listener);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Register a global error handler
|
|
233
|
+
*/
|
|
234
|
+
onError(callback) {
|
|
235
|
+
this.#errorHandler = callback;
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Bind multiple listeners to listen for a single event. The listen
|
|
240
|
+
* method is a convenience helper to be used with class based
|
|
241
|
+
* events and listeners.
|
|
242
|
+
*/
|
|
243
|
+
listen(event, listeners) {
|
|
244
|
+
listeners.forEach((listener) => this.on(event, [listener, "handle"]));
|
|
245
|
+
}
|
|
246
|
+
on(event, listener) {
|
|
247
|
+
if (debug_default.enabled) {
|
|
248
|
+
debug_default("registering event listener, event: %O, listener: %O", event, listener);
|
|
249
|
+
}
|
|
250
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
251
|
+
const normalizedListener = this.#resolveEventListener(event, listener);
|
|
252
|
+
this.#transport.on(normalizedEvent, normalizedListener);
|
|
253
|
+
return () => this.off(event, listener);
|
|
254
|
+
}
|
|
255
|
+
once(event, listener) {
|
|
256
|
+
if (debug_default.enabled) {
|
|
257
|
+
debug_default("registering one time event listener, event: %O, listener: %O", event, listener);
|
|
258
|
+
}
|
|
259
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
260
|
+
const normalizedListener = this.#normalizeEventListener(listener);
|
|
261
|
+
const off = this.#transport.on(normalizedEvent, async (data) => {
|
|
262
|
+
off();
|
|
263
|
+
debug_default("removing one time event listener, event: %O", event);
|
|
264
|
+
await normalizedListener(data);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Attach a listener to listen for all the events. Wildcard listeners
|
|
269
|
+
* can only be defined as inline callbacks.
|
|
270
|
+
*/
|
|
271
|
+
onAny(listener) {
|
|
272
|
+
return this.#transport.onAny(listener);
|
|
273
|
+
}
|
|
274
|
+
async emit(event, data) {
|
|
275
|
+
if (this.#eventsToFake.has(event) || this.#eventsToFake.has("*")) {
|
|
276
|
+
debug_default("faking emit. event: %O, data: %O", event, data);
|
|
277
|
+
this.#eventsBuffer.add(event, data);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
try {
|
|
281
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
282
|
+
await this.#transport.emit(normalizedEvent, data);
|
|
283
|
+
} catch (error) {
|
|
284
|
+
if (this.#errorHandler) {
|
|
285
|
+
this.#errorHandler(event, error, data);
|
|
286
|
+
} else {
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
async emitSerial(event, data) {
|
|
292
|
+
if (this.#eventsToFake.has(event) || this.#eventsToFake.has("*")) {
|
|
293
|
+
debug_default("faking emit. event: %O, data: %O", event, data);
|
|
294
|
+
this.#eventsBuffer.add(event, data);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
try {
|
|
298
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
299
|
+
await this.#transport.emitSerial(normalizedEvent, data);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (this.#errorHandler) {
|
|
302
|
+
this.#errorHandler(event, error, data);
|
|
303
|
+
} else {
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Remove a specific listener for an event
|
|
310
|
+
*/
|
|
311
|
+
off(event, listener) {
|
|
312
|
+
if (debug_default.enabled) {
|
|
313
|
+
debug_default("removing listener, event: %O, listener: %O", event, listener);
|
|
314
|
+
}
|
|
315
|
+
const normalizedEvent = this.#resolveEvent(event);
|
|
316
|
+
const listeners = this.#getEventListeners(event);
|
|
317
|
+
const normalizedListener = listeners.get(listener);
|
|
318
|
+
if (!normalizedListener) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
listeners.delete(listener);
|
|
322
|
+
this.#transport.off(normalizedEvent, normalizedListener);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Remove a specific listener listening for all the events
|
|
326
|
+
*/
|
|
327
|
+
offAny(listener) {
|
|
328
|
+
this.#transport.offAny(listener);
|
|
329
|
+
return this;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Remove a specific listener for an event
|
|
333
|
+
*
|
|
334
|
+
* @alias "off"
|
|
335
|
+
*/
|
|
336
|
+
clearListener(event, listener) {
|
|
337
|
+
return this.off(event, listener);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Clear all listeners for a specific event
|
|
341
|
+
*/
|
|
342
|
+
clearListeners(event) {
|
|
343
|
+
debug_default("clearing all listeners for event %O", event);
|
|
344
|
+
this.#transport.clearListeners(this.#resolveEvent(event));
|
|
345
|
+
this.#eventsListeners.delete(event);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Clear all listeners for all the events
|
|
349
|
+
*/
|
|
350
|
+
clearAllListeners() {
|
|
351
|
+
debug_default("clearing all event listeners");
|
|
352
|
+
this.#transport.clearListeners();
|
|
353
|
+
this.#eventsListeners.clear();
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Get count of listeners for a given event or all the events
|
|
357
|
+
*/
|
|
358
|
+
listenerCount(event) {
|
|
359
|
+
return this.#transport.listenerCount(event ? this.#resolveEvent(event) : void 0);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Find if an event has one or more listeners
|
|
363
|
+
*/
|
|
364
|
+
hasListeners(event) {
|
|
365
|
+
return this.listenerCount(event) > 0;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Fake one or more events. The listeners for faked events will
|
|
369
|
+
* not be invoked.
|
|
370
|
+
*
|
|
371
|
+
* The return value is an events buffer that collects all the
|
|
372
|
+
* events within memory.
|
|
373
|
+
*
|
|
374
|
+
* Calling this method one than once drops the existing fakes and
|
|
375
|
+
* creates new one.
|
|
376
|
+
*/
|
|
377
|
+
fake(events) {
|
|
378
|
+
this.restore();
|
|
379
|
+
this.#eventsBuffer = new EventsBuffer();
|
|
380
|
+
if (!events) {
|
|
381
|
+
debug_default("faking all events");
|
|
382
|
+
this.#eventsToFake.add("*");
|
|
383
|
+
} else {
|
|
384
|
+
debug_default("faking events: %O", events);
|
|
385
|
+
events.forEach((event) => this.#eventsToFake.add(event));
|
|
386
|
+
}
|
|
387
|
+
return this.#eventsBuffer;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Restore fakes
|
|
391
|
+
*/
|
|
392
|
+
restore() {
|
|
393
|
+
debug_default("restoring existing fakes");
|
|
394
|
+
this.#eventsToFake.clear();
|
|
395
|
+
this.#eventsBuffer?.flush();
|
|
396
|
+
this.#eventsBuffer = void 0;
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
export {
|
|
401
|
+
Emitter
|
|
402
|
+
};
|
|
@@ -1,12 +1,59 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { Application } from '@adonisjs/application';
|
|
2
|
+
import { UnsubscribeFunction } from 'emittery';
|
|
3
|
+
import { AllowedEventTypes, BufferedEventsList, Constructor, BufferedEvent, Listener, ListenerMethod, ListenerClassWithHandleMethod, LazyImport } from './src/types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Exposes API to filter, find events from the events buffer.
|
|
7
|
+
*/
|
|
8
|
+
declare class EventsBuffer<EventsList extends Record<string | symbol | number, any>> {
|
|
9
|
+
#private;
|
|
10
|
+
/**
|
|
11
|
+
* Track emitted event
|
|
12
|
+
*/
|
|
13
|
+
add<Name extends AllowedEventTypes>(event: Name, data: any): void;
|
|
14
|
+
/**
|
|
15
|
+
* Get all the emitted events
|
|
16
|
+
*/
|
|
17
|
+
all(): BufferedEventsList<EventsList>[];
|
|
18
|
+
/**
|
|
19
|
+
* Returns the size of captured events
|
|
20
|
+
*/
|
|
21
|
+
size(): number;
|
|
22
|
+
/**
|
|
23
|
+
* Find if an event was emitted
|
|
24
|
+
*/
|
|
25
|
+
exists<Event extends keyof EventsList | Constructor>(finder: Event | ((event: BufferedEventsList<EventsList>) => boolean)): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Get selected events
|
|
28
|
+
*/
|
|
29
|
+
filter(finder: keyof EventsList | Constructor | ((event: BufferedEventsList<EventsList>) => boolean)): BufferedEventsList<EventsList>[];
|
|
30
|
+
/**
|
|
31
|
+
* Find a specific event
|
|
32
|
+
*/
|
|
33
|
+
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;
|
|
34
|
+
/**
|
|
35
|
+
* Assert a given event has been emitted
|
|
36
|
+
*/
|
|
37
|
+
assertEmitted<Event extends keyof EventsList | Constructor>(finder: Event | ((event: BufferedEventsList<EventsList>) => boolean)): void;
|
|
38
|
+
/**
|
|
39
|
+
* Assert a given event has been not been emitted
|
|
40
|
+
*/
|
|
41
|
+
assertNotEmitted<Event extends keyof EventsList | Constructor<any>>(finder: Event | ((event: BufferedEventsList<EventsList>) => boolean)): void;
|
|
42
|
+
/**
|
|
43
|
+
* Assert a given event has been not been emitted
|
|
44
|
+
*/
|
|
45
|
+
assertNoneEmitted(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Flush events collected within memory
|
|
48
|
+
*/
|
|
49
|
+
flush(): void;
|
|
50
|
+
}
|
|
51
|
+
|
|
5
52
|
/**
|
|
6
53
|
* Event emitter is built on top of emittery with support class based
|
|
7
54
|
* events and listeners
|
|
8
55
|
*/
|
|
9
|
-
|
|
56
|
+
declare class Emitter<EventsList extends Record<string | symbol | number, any>> {
|
|
10
57
|
#private;
|
|
11
58
|
/**
|
|
12
59
|
* Returns a map of events and their registered listeners. The
|
|
@@ -105,3 +152,5 @@ export declare class Emitter<EventsList extends Record<string | symbol | number,
|
|
|
105
152
|
*/
|
|
106
153
|
restore(): void;
|
|
107
154
|
}
|
|
155
|
+
|
|
156
|
+
export { Emitter as E };
|
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import { Application } from '@adonisjs/application';
|
|
2
|
+
import { E as Emitter } from '../emitter-253440aa.js';
|
|
3
|
+
import 'emittery';
|
|
4
|
+
import '../src/types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Emitter factory is used to create an instance of emitter
|
|
8
|
+
* for testing
|
|
9
|
+
*/
|
|
10
|
+
declare class EmitterFactory {
|
|
11
|
+
/**
|
|
12
|
+
* Create emitter instance
|
|
13
|
+
*/
|
|
14
|
+
create(app: Application<any>): Emitter<Record<string | number | symbol, any>>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { EmitterFactory };
|
package/build/factories/main.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import {
|
|
2
|
+
Emitter
|
|
3
|
+
} from "../chunk-VVJERWW5.js";
|
|
4
|
+
|
|
5
|
+
// factories/emitter.ts
|
|
6
|
+
var EmitterFactory = class {
|
|
7
|
+
/**
|
|
8
|
+
* Create emitter instance
|
|
9
|
+
*/
|
|
10
|
+
create(app) {
|
|
11
|
+
return new Emitter(app);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
export {
|
|
15
|
+
EmitterFactory
|
|
16
|
+
};
|
package/build/index.d.ts
CHANGED
|
@@ -1,2 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { E as Emitter } from './emitter-253440aa.js';
|
|
2
|
+
import '@adonisjs/application';
|
|
3
|
+
import 'emittery';
|
|
4
|
+
import './src/types.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Base event adds ability to a class to act as an event. You can emit the
|
|
8
|
+
* event by calling "Event.dispatch" method.
|
|
9
|
+
*/
|
|
10
|
+
declare class BaseEvent {
|
|
11
|
+
constructor(..._: any[]);
|
|
12
|
+
/**
|
|
13
|
+
* The emitter to use for dispatching events
|
|
14
|
+
*/
|
|
15
|
+
static emitter?: Emitter<any>;
|
|
16
|
+
/**
|
|
17
|
+
* Specify the emitter instance to use for dispatching events
|
|
18
|
+
*/
|
|
19
|
+
static useEmitter(emitter: Emitter<any>): void;
|
|
20
|
+
/**
|
|
21
|
+
* Dispatch the current class as an event. The method takes the arguments
|
|
22
|
+
* accepted by the class constructor.
|
|
23
|
+
*/
|
|
24
|
+
static dispatch<T extends typeof BaseEvent>(this: T, ...args: ConstructorParameters<T>): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { BaseEvent, Emitter };
|
package/build/index.js
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import {
|
|
2
|
+
Emitter
|
|
3
|
+
} from "./chunk-VVJERWW5.js";
|
|
4
|
+
|
|
5
|
+
// src/base_event.ts
|
|
6
|
+
import { RuntimeException } from "@poppinss/utils";
|
|
7
|
+
var BaseEvent = class {
|
|
8
|
+
constructor(..._) {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The emitter to use for dispatching events
|
|
12
|
+
*/
|
|
13
|
+
static emitter;
|
|
14
|
+
/**
|
|
15
|
+
* Specify the emitter instance to use for dispatching events
|
|
16
|
+
*/
|
|
17
|
+
static useEmitter(emitter) {
|
|
18
|
+
this.emitter = emitter;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Dispatch the current class as an event. The method takes the arguments
|
|
22
|
+
* accepted by the class constructor.
|
|
23
|
+
*/
|
|
24
|
+
static async dispatch(...args) {
|
|
25
|
+
if (!this.emitter) {
|
|
26
|
+
throw new RuntimeException(
|
|
27
|
+
`Cannot dispatch "${this.name}" event. Make sure to pass emitter to the "BaseEvent" class for dispatch method to work`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
return this.emitter.emit(this, new this(...args));
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
export {
|
|
34
|
+
BaseEvent,
|
|
35
|
+
Emitter
|
|
36
|
+
};
|
package/build/src/types.d.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Data types for event name
|
|
3
3
|
*/
|
|
4
|
-
|
|
4
|
+
type AllowedEventTypes = string | symbol | number | Constructor;
|
|
5
5
|
/**
|
|
6
6
|
* Class constructor type
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
type Constructor<T = unknown> = new (...args: any[]) => T;
|
|
9
9
|
/**
|
|
10
10
|
* A function that lazily imports a middleware
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
type LazyImport<DefaultExport> = () => Promise<{
|
|
13
13
|
default: DefaultExport;
|
|
14
14
|
}>;
|
|
15
15
|
/**
|
|
16
16
|
* Data structure for a buffered event
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
type BufferedEvent<Event, Data> = {
|
|
19
19
|
event: Event;
|
|
20
20
|
data: Data;
|
|
21
21
|
};
|
|
22
22
|
/**
|
|
23
23
|
* Event list item inside bufferred items
|
|
24
24
|
*/
|
|
25
|
-
|
|
25
|
+
type BufferedEventsList<EventsList> = {
|
|
26
26
|
[Name in keyof EventsList]: BufferedEvent<Name, EventsList[Name]>;
|
|
27
27
|
}[keyof EventsList] | BufferedEvent<Constructor<any>, any>;
|
|
28
28
|
/**
|
|
@@ -30,26 +30,28 @@ export type BufferedEventsList<EventsList> = {
|
|
|
30
30
|
* spread args can type hint dependencies and container will
|
|
31
31
|
* resolve them
|
|
32
32
|
*/
|
|
33
|
-
|
|
33
|
+
type ListenerMethod<Data> = (data: Data, ...args: any[]) => any | Promise<any>;
|
|
34
34
|
/**
|
|
35
35
|
* The event listener defined as an inline callback
|
|
36
36
|
*/
|
|
37
|
-
|
|
37
|
+
type ListenerFn<Data> = (data: Data) => any | Promise<any>;
|
|
38
38
|
/**
|
|
39
39
|
* Returns a union of methods from a listener that accepts
|
|
40
40
|
* the event data as the first argument.
|
|
41
41
|
*/
|
|
42
|
-
|
|
42
|
+
type GetListenersMethods<Listener extends Constructor<any>, Data> = {
|
|
43
43
|
[K in keyof InstanceType<Listener>]: InstanceType<Listener>[K] extends ListenerMethod<Data> ? K : never;
|
|
44
44
|
}[keyof InstanceType<Listener>];
|
|
45
45
|
/**
|
|
46
46
|
* Representation of listener class with handle method
|
|
47
47
|
*/
|
|
48
|
-
|
|
48
|
+
type ListenerClassWithHandleMethod<Data> = Constructor<{
|
|
49
49
|
handle: ListenerMethod<Data>;
|
|
50
50
|
}>;
|
|
51
51
|
/**
|
|
52
52
|
* The event listener defined as an inline callback, string
|
|
53
53
|
* listener class reference or a lazily imported listener
|
|
54
54
|
*/
|
|
55
|
-
|
|
55
|
+
type Listener<Data, ListenerClass extends Constructor> = ListenerFn<Data> | string | [LazyImport<ListenerClass> | ListenerClass, GetListenersMethods<ListenerClass, Data>?];
|
|
56
|
+
|
|
57
|
+
export { AllowedEventTypes, BufferedEvent, BufferedEventsList, Constructor, GetListenersMethods, LazyImport, Listener, ListenerClassWithHandleMethod, ListenerFn, ListenerMethod };
|