@adonisjs/events 8.4.9-5 → 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/package.json CHANGED
@@ -1,14 +1,11 @@
1
1
  {
2
2
  "name": "@adonisjs/events",
3
- "version": "8.4.9-5",
3
+ "version": "9.0.0-0",
4
4
  "description": "An implementation of the event emitter built on top of emittery",
5
5
  "main": "build/index.js",
6
6
  "type": "module",
7
7
  "files": [
8
- "build/src",
9
- "build/factories",
10
- "build/index.d.ts",
11
- "build/index.js"
8
+ "build"
12
9
  ],
13
10
  "imports": {
14
11
  "#listeners/*": "./tests/app/listeners/*.js"
@@ -26,7 +23,7 @@
26
23
  "test": "cross-env NODE_DEBUG=adonisjs:events c8 npm run quick:test",
27
24
  "clean": "del-cli build",
28
25
  "typecheck": "tsc --noEmit",
29
- "compile": "npm run lint && npm run clean && tsc",
26
+ "compile": "npm run lint && npm run clean && tsup-node",
30
27
  "build": "npm run compile",
31
28
  "release": "np",
32
29
  "version": "npm run build",
@@ -43,38 +40,39 @@
43
40
  "author": "virk,adonisjs",
44
41
  "license": "MIT",
45
42
  "devDependencies": {
46
- "@adonisjs/application": "^7.1.2-15",
43
+ "@adonisjs/application": "^8.0.0-0",
47
44
  "@adonisjs/eslint-config": "^1.1.8",
48
- "@adonisjs/fold": "^9.9.3-8",
45
+ "@adonisjs/fold": "^9.9.3-10",
49
46
  "@adonisjs/prettier-config": "^1.1.8",
50
47
  "@adonisjs/tsconfig": "^1.1.8",
51
- "@commitlint/cli": "^17.7.1",
52
- "@commitlint/config-conventional": "^17.7.0",
53
- "@japa/assert": "^2.0.0-1",
54
- "@japa/expect-type": "^2.0.0-0",
55
- "@japa/file-system": "^2.0.0-1",
56
- "@japa/runner": "^3.0.0-3",
57
- "@swc/core": "^1.3.78",
58
- "@types/node": "^20.5.3",
48
+ "@commitlint/cli": "^17.8.0",
49
+ "@commitlint/config-conventional": "^17.8.0",
50
+ "@japa/assert": "^2.0.0",
51
+ "@japa/expect-type": "^2.0.0",
52
+ "@japa/file-system": "^2.0.0",
53
+ "@japa/runner": "^3.0.2",
54
+ "@swc/core": "1.3.82",
55
+ "@types/node": "^20.8.6",
59
56
  "c8": "^8.0.1",
60
57
  "cross-env": "^7.0.3",
61
- "del-cli": "^5.0.0",
62
- "eslint": "^8.47.0",
58
+ "del-cli": "^5.1.0",
59
+ "eslint": "^8.51.0",
63
60
  "github-label-sync": "^2.3.1",
64
61
  "husky": "^8.0.3",
65
62
  "np": "^8.0.4",
66
- "prettier": "^3.0.0",
63
+ "prettier": "^3.0.3",
67
64
  "ts-node": "^10.9.1",
68
- "typescript": "^5.1.6"
65
+ "tsup": "^7.2.0",
66
+ "typescript": "^5.2.2"
69
67
  },
70
68
  "dependencies": {
71
- "@poppinss/utils": "^6.5.0-5",
72
- "@sindresorhus/is": "^6.0.0",
69
+ "@poppinss/utils": "^6.5.0",
70
+ "@sindresorhus/is": "^6.0.1",
73
71
  "emittery": "^1.0.1"
74
72
  },
75
73
  "peerDependencies": {
76
- "@adonisjs/application": "^7.1.2-15",
77
- "@adonisjs/fold": "^9.9.3-8"
74
+ "@adonisjs/application": "^8.0.0-0",
75
+ "@adonisjs/fold": "^9.9.3-10"
78
76
  },
79
77
  "repository": {
80
78
  "type": "git",
@@ -111,5 +109,17 @@
111
109
  "eslintConfig": {
112
110
  "extends": "@adonisjs/eslint-config/package"
113
111
  },
114
- "prettier": "@adonisjs/prettier-config"
112
+ "prettier": "@adonisjs/prettier-config",
113
+ "tsup": {
114
+ "entry": [
115
+ "./index.ts",
116
+ "./src/types.ts",
117
+ "./factories/main.ts"
118
+ ],
119
+ "outDir": "./build",
120
+ "clean": true,
121
+ "format": "esm",
122
+ "dts": true,
123
+ "target": "esnext"
124
+ }
115
125
  }
@@ -1,12 +0,0 @@
1
- import type { Application } from '@adonisjs/application';
2
- import { Emitter } from '../src/emitter.js';
3
- /**
4
- * Emitter factory is used to create an instance of emitter
5
- * for testing
6
- */
7
- export declare class EmitterFactory {
8
- /**
9
- * Create emitter instance
10
- */
11
- create(app: Application<any>): Emitter<Record<string | number | symbol, any>>;
12
- }
@@ -1,21 +0,0 @@
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
- */
9
- import { Emitter } from '../src/emitter.js';
10
- /**
11
- * Emitter factory is used to create an instance of emitter
12
- * for testing
13
- */
14
- export class EmitterFactory {
15
- /**
16
- * Create emitter instance
17
- */
18
- create(app) {
19
- return new Emitter(app);
20
- }
21
- }
@@ -1,21 +0,0 @@
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
- */
6
- export declare class BaseEvent {
7
- constructor(..._: any[]);
8
- /**
9
- * The emitter to use for dispatching events
10
- */
11
- static emitter?: Emitter<any>;
12
- /**
13
- * Specify the emitter instance to use for dispatching events
14
- */
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
- */
20
- static dispatch<T extends typeof BaseEvent>(this: T, ...args: ConstructorParameters<T>): Promise<void>;
21
- }
@@ -1,36 +0,0 @@
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
- */
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
- */
14
- export class BaseEvent {
15
- constructor(..._) { }
16
- /**
17
- * The emitter to use for dispatching events
18
- */
19
- static emitter;
20
- /**
21
- * Specify the emitter instance to use for dispatching events
22
- */
23
- static useEmitter(emitter) {
24
- this.emitter = emitter;
25
- }
26
- /**
27
- * Dispatch the current class as an event. The method takes the arguments
28
- * accepted by the class constructor.
29
- */
30
- static async dispatch(...args) {
31
- if (!this.emitter) {
32
- throw new RuntimeException(`Cannot dispatch "${this.name}" event. Make sure to pass emitter to the "BaseEvent" class for dispatch method to work`);
33
- }
34
- return this.emitter.emit(this, new this(...args));
35
- }
36
- }
@@ -1,3 +0,0 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- declare const _default: import("util").DebugLogger;
3
- export default _default;
@@ -1,10 +0,0 @@
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
- */
9
- import { debuglog } from 'node:util';
10
- export default debuglog('adonisjs:events');
@@ -1,320 +0,0 @@
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
- */
9
- import is from '@sindresorhus/is';
10
- import Emittery from 'emittery';
11
- import { moduleExpression, moduleCaller, moduleImporter } from '@adonisjs/fold';
12
- import debug from './debug.js';
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
- */
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
- */
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
- */
28
- #eventsListeners = new Map();
29
- /**
30
- * Underlying transport to emit events
31
- */
32
- #transport = new Emittery();
33
- /**
34
- * Events buffer. The events are collected inside an in-memory
35
- * buffer during fakes
36
- */
37
- #eventsBuffer;
38
- /**
39
- * A set of events to fake
40
- */
41
- #eventsToFake = new Set();
42
- /**
43
- * Error handler to catch all errors thrown by listeners
44
- */
45
- #errorHandler;
46
- /**
47
- * Reference to AdonisJS application, we need the application root
48
- * and container reference from it.
49
- */
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
- */
59
- get eventsListeners() {
60
- return this.#eventsListeners;
61
- }
62
- constructor(app) {
63
- this.#app = app;
64
- }
65
- /**
66
- * Returns the symbol for a class based event.
67
- */
68
- #getEventClassSymbol(event) {
69
- if (!this.#eventsClassSymbols.has(event)) {
70
- this.#eventsClassSymbols.set(event, Symbol(event.name));
71
- }
72
- return this.#eventsClassSymbols.get(event);
73
- }
74
- /**
75
- * Normalizes the event to emittery supported data types. The class
76
- * constructors are cached against a unique symbol.
77
- */
78
- #resolveEvent(event) {
79
- if (is.class_(event)) {
80
- return this.#getEventClassSymbol(event);
81
- }
82
- return event;
83
- }
84
- /**
85
- * Returns the event listeners map
86
- */
87
- #getEventListeners(event) {
88
- if (!this.#eventsListeners.has(event)) {
89
- this.#eventsListeners.set(event, new Map());
90
- }
91
- return this.#eventsListeners.get(event);
92
- }
93
- /**
94
- * Normalizes the event listener to a function that can be passed to
95
- * emittery.
96
- */
97
- #normalizeEventListener(listener) {
98
- /**
99
- * Parse string based listener
100
- */
101
- if (typeof listener === 'string') {
102
- return moduleExpression(listener, this.#app.appRoot.toString()).toCallable(this.#app.container);
103
- }
104
- /**
105
- * Parse array based listener with the listener reference
106
- * or lazily imported listener class
107
- */
108
- if (Array.isArray(listener)) {
109
- const listenerModule = listener[0];
110
- const method = listener[1] || 'handle';
111
- /**
112
- * Class reference
113
- */
114
- if (is.class_(listenerModule)) {
115
- return moduleCaller(listenerModule, method).toCallable(this.#app.container);
116
- }
117
- /**
118
- * Lazily loaded module
119
- */
120
- return moduleImporter(listenerModule, method).toCallable(this.#app.container);
121
- }
122
- return listener;
123
- }
124
- /**
125
- * Resolves the event listener either from the cache or normalizes
126
- * it and stores it inside the cache
127
- */
128
- #resolveEventListener(event, listener) {
129
- const eventListeners = this.#getEventListeners(event);
130
- if (!eventListeners.has(listener)) {
131
- eventListeners.set(listener, this.#normalizeEventListener(listener));
132
- }
133
- return eventListeners.get(listener);
134
- }
135
- /**
136
- * Register a global error handler
137
- */
138
- onError(callback) {
139
- this.#errorHandler = callback;
140
- return this;
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
- */
147
- listen(event, listeners) {
148
- listeners.forEach((listener) => this.on(event, [listener, 'handle']));
149
- }
150
- on(event, listener) {
151
- if (debug.enabled) {
152
- debug('registering event listener, event: %O, listener: %O', event, listener);
153
- }
154
- const normalizedEvent = this.#resolveEvent(event);
155
- const normalizedListener = this.#resolveEventListener(event, listener);
156
- this.#transport.on(normalizedEvent, normalizedListener);
157
- return () => this.off(event, listener);
158
- }
159
- once(event, listener) {
160
- if (debug.enabled) {
161
- debug('registering one time event listener, event: %O, listener: %O', event, listener);
162
- }
163
- const normalizedEvent = this.#resolveEvent(event);
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
- */
172
- const off = this.#transport.on(normalizedEvent, async (data) => {
173
- off();
174
- debug('removing one time event listener, event: %O', event);
175
- await normalizedListener(data);
176
- });
177
- }
178
- /**
179
- * Attach a listener to listen for all the events. Wildcard listeners
180
- * can only be defined as inline callbacks.
181
- */
182
- onAny(listener) {
183
- return this.#transport.onAny(listener);
184
- }
185
- async emit(event, data) {
186
- /**
187
- * Entertain fakes if exists
188
- */
189
- if (this.#eventsToFake.has(event) || this.#eventsToFake.has('*')) {
190
- debug('faking emit. event: %O, data: %O', event, data);
191
- this.#eventsBuffer.add(event, data);
192
- return;
193
- }
194
- try {
195
- const normalizedEvent = this.#resolveEvent(event);
196
- await this.#transport.emit(normalizedEvent, data);
197
- }
198
- catch (error) {
199
- if (this.#errorHandler) {
200
- this.#errorHandler(event, error, data);
201
- }
202
- else {
203
- throw error;
204
- }
205
- }
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
- */
232
- off(event, listener) {
233
- if (debug.enabled) {
234
- debug('removing listener, event: %O, listener: %O', event, listener);
235
- }
236
- const normalizedEvent = this.#resolveEvent(event);
237
- const listeners = this.#getEventListeners(event);
238
- const normalizedListener = listeners.get(listener);
239
- if (!normalizedListener) {
240
- return;
241
- }
242
- listeners.delete(listener);
243
- this.#transport.off(normalizedEvent, normalizedListener);
244
- }
245
- /**
246
- * Remove a specific listener listening for all the events
247
- */
248
- offAny(listener) {
249
- this.#transport.offAny(listener);
250
- return this;
251
- }
252
- /**
253
- * Remove a specific listener for an event
254
- *
255
- * @alias "off"
256
- */
257
- clearListener(event, listener) {
258
- return this.off(event, listener);
259
- }
260
- /**
261
- * Clear all listeners for a specific event
262
- */
263
- clearListeners(event) {
264
- debug('clearing all listeners for event %O', event);
265
- this.#transport.clearListeners(this.#resolveEvent(event));
266
- this.#eventsListeners.delete(event);
267
- }
268
- /**
269
- * Clear all listeners for all the events
270
- */
271
- clearAllListeners() {
272
- debug('clearing all event listeners');
273
- this.#transport.clearListeners();
274
- this.#eventsListeners.clear();
275
- }
276
- /**
277
- * Get count of listeners for a given event or all the events
278
- */
279
- listenerCount(event) {
280
- return this.#transport.listenerCount(event ? this.#resolveEvent(event) : undefined);
281
- }
282
- /**
283
- * Find if an event has one or more listeners
284
- */
285
- hasListeners(event) {
286
- return this.listenerCount(event) > 0;
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
- */
298
- fake(events) {
299
- this.restore();
300
- this.#eventsBuffer = new EventsBuffer();
301
- if (!events) {
302
- debug('faking all events');
303
- this.#eventsToFake.add('*');
304
- }
305
- else {
306
- debug('faking events: %O', events);
307
- events.forEach((event) => this.#eventsToFake.add(event));
308
- }
309
- return this.#eventsBuffer;
310
- }
311
- /**
312
- * Restore fakes
313
- */
314
- restore() {
315
- debug('restoring existing fakes');
316
- this.#eventsToFake.clear();
317
- this.#eventsBuffer?.flush();
318
- this.#eventsBuffer = undefined;
319
- }
320
- }
@@ -1,47 +0,0 @@
1
- import type { AllowedEventTypes, BufferedEvent, BufferedEventsList, Constructor } from './types.js';
2
- /**
3
- * Exposes API to filter, find events from the events buffer.
4
- */
5
- export declare class EventsBuffer<EventsList extends Record<string | symbol | number, any>> {
6
- #private;
7
- /**
8
- * Track emitted event
9
- */
10
- add<Name extends AllowedEventTypes>(event: Name, data: any): void;
11
- /**
12
- * Get all the emitted events
13
- */
14
- all(): BufferedEventsList<EventsList>[];
15
- /**
16
- * Returns the size of captured events
17
- */
18
- size(): number;
19
- /**
20
- * Find if an event was emitted
21
- */
22
- exists<Event extends keyof EventsList | Constructor>(finder: Event | ((event: BufferedEventsList<EventsList>) => boolean)): boolean;
23
- /**
24
- * Get selected events
25
- */
26
- filter(finder: keyof EventsList | Constructor | ((event: BufferedEventsList<EventsList>) => boolean)): BufferedEventsList<EventsList>[];
27
- /**
28
- * Find a specific event
29
- */
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
- */
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
- */
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
- */
42
- assertNoneEmitted(): void;
43
- /**
44
- * Flush events collected within memory
45
- */
46
- flush(): void;
47
- }