@adonisjs/events 7.1.2 → 7.2.1
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/LICENSE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# The MIT License
|
|
2
2
|
|
|
3
|
-
Copyright
|
|
3
|
+
Copyright 2022 Harminder Virk, contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
|
@@ -40,11 +40,87 @@ declare module '@ioc:Adonis/Core/Event' {
|
|
|
40
40
|
clearListeners(event?: string): any;
|
|
41
41
|
listenerCount(event?: string): number;
|
|
42
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Fake emitter to be used for finding and asserting
|
|
45
|
+
* faked events
|
|
46
|
+
*/
|
|
47
|
+
export interface FakeEmitterContract {
|
|
48
|
+
/**
|
|
49
|
+
* Returns all the emitted events
|
|
50
|
+
*/
|
|
51
|
+
all(): {
|
|
52
|
+
name: string;
|
|
53
|
+
data: any;
|
|
54
|
+
}[];
|
|
55
|
+
/**
|
|
56
|
+
* Find if the event exists
|
|
57
|
+
*/
|
|
58
|
+
exists<K extends keyof EventsList>(event: K): boolean;
|
|
59
|
+
exists(event: string): boolean;
|
|
60
|
+
exists<Events extends Record<string, any> = EventsList>(matchCallback: (event: {
|
|
61
|
+
[K in keyof Events]: {
|
|
62
|
+
name: K;
|
|
63
|
+
data: Events[K];
|
|
64
|
+
};
|
|
65
|
+
}[keyof Events]) => boolean): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Find an event
|
|
68
|
+
*/
|
|
69
|
+
find<K extends keyof EventsList>(event: K): {
|
|
70
|
+
name: K;
|
|
71
|
+
data: DataForEvent<K>;
|
|
72
|
+
} | null;
|
|
73
|
+
find(event: string): {
|
|
74
|
+
name: string;
|
|
75
|
+
data: any;
|
|
76
|
+
};
|
|
77
|
+
find<Events extends Record<string, any> = EventsList>(matchCallback: (event: {
|
|
78
|
+
[K in keyof Events]: {
|
|
79
|
+
name: K;
|
|
80
|
+
data: Events[K];
|
|
81
|
+
};
|
|
82
|
+
}[keyof Events]) => boolean): {
|
|
83
|
+
[K in keyof Events]: {
|
|
84
|
+
name: K;
|
|
85
|
+
data: Events[K];
|
|
86
|
+
};
|
|
87
|
+
}[keyof Events] | null;
|
|
88
|
+
/**
|
|
89
|
+
* Find multiple events
|
|
90
|
+
*/
|
|
91
|
+
filter<K extends keyof EventsList>(event: K): {
|
|
92
|
+
name: K;
|
|
93
|
+
data: DataForEvent<K>;
|
|
94
|
+
}[];
|
|
95
|
+
filter(event: string): {
|
|
96
|
+
name: string;
|
|
97
|
+
data: any;
|
|
98
|
+
}[];
|
|
99
|
+
filter<Events extends Record<string, any> = EventsList>(matchCallback: (event: {
|
|
100
|
+
[K in keyof Events]: {
|
|
101
|
+
name: K;
|
|
102
|
+
data: Events[K];
|
|
103
|
+
};
|
|
104
|
+
}[keyof Events]) => boolean): {
|
|
105
|
+
[K in keyof Events]: {
|
|
106
|
+
name: K;
|
|
107
|
+
data: Events[K];
|
|
108
|
+
};
|
|
109
|
+
}[keyof Events][];
|
|
110
|
+
/**
|
|
111
|
+
* Get number of events emitted
|
|
112
|
+
*/
|
|
113
|
+
size(): number;
|
|
114
|
+
}
|
|
43
115
|
/**
|
|
44
116
|
* Shape of Event emitter
|
|
45
117
|
*/
|
|
46
118
|
export interface EmitterContract {
|
|
47
119
|
transport: EmitterTransportContract;
|
|
120
|
+
/**
|
|
121
|
+
* Fake the upcoming events
|
|
122
|
+
*/
|
|
123
|
+
fake<K extends keyof EventsList>(events?: K[] | string[]): FakeEmitterContract;
|
|
48
124
|
/**
|
|
49
125
|
* Define a custom error handler
|
|
50
126
|
*/
|
|
@@ -92,6 +168,10 @@ declare module '@ioc:Adonis/Core/Event' {
|
|
|
92
168
|
*/
|
|
93
169
|
clearListeners<K extends keyof EventsList>(event: K): void;
|
|
94
170
|
clearListeners<K extends string>(event: K): void;
|
|
171
|
+
/**
|
|
172
|
+
* Clear all listeners for all events
|
|
173
|
+
*/
|
|
174
|
+
clearAllListeners(): void;
|
|
95
175
|
/**
|
|
96
176
|
* Returns count of listeners listening for a given event
|
|
97
177
|
*/
|
|
@@ -115,7 +195,7 @@ declare module '@ioc:Adonis/Core/Event' {
|
|
|
115
195
|
/**
|
|
116
196
|
* Restore traps
|
|
117
197
|
*/
|
|
118
|
-
restore():
|
|
198
|
+
restore(): void;
|
|
119
199
|
}
|
|
120
200
|
/**
|
|
121
201
|
* An interface to define typed events
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference path="../../adonis-typings/events.d.ts" />
|
|
2
2
|
import { ApplicationContract } from '@ioc:Adonis/Core/Application';
|
|
3
3
|
import { AnyHandler, EventsList, TrapHandler, EventHandler, DataForEvent, ErrorHandler, TrapAllHandler, EmitterContract, EmitterTransportContract } from '@ioc:Adonis/Core/Event';
|
|
4
|
+
import { FakeEmitter } from '../FakeEmitter';
|
|
4
5
|
/**
|
|
5
6
|
* Emitter class exposes the API for async event emitter built on top of
|
|
6
7
|
* Emittery. It also exposes an API to pre-define the Typescript types
|
|
@@ -9,10 +10,21 @@ import { AnyHandler, EventsList, TrapHandler, EventHandler, DataForEvent, ErrorH
|
|
|
9
10
|
export declare class Emitter implements EmitterContract {
|
|
10
11
|
transport: EmitterTransportContract;
|
|
11
12
|
private iocResolver?;
|
|
13
|
+
/**
|
|
14
|
+
* Error handler to report emitter errors
|
|
15
|
+
*/
|
|
16
|
+
private errorHandler?;
|
|
17
|
+
/**
|
|
18
|
+
* Deprecated properties to manage trapping events
|
|
19
|
+
*/
|
|
12
20
|
private trappingEvents;
|
|
13
21
|
private traps;
|
|
14
22
|
private trapAllHandler?;
|
|
15
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Fakes
|
|
25
|
+
*/
|
|
26
|
+
private eventsToFake;
|
|
27
|
+
private fakeEmitter;
|
|
16
28
|
constructor(app?: ApplicationContract);
|
|
17
29
|
/**
|
|
18
30
|
* Returns reference to the IoC resolver. Do not call this method until
|
|
@@ -85,7 +97,12 @@ export declare class Emitter implements EmitterContract {
|
|
|
85
97
|
*/
|
|
86
98
|
trapAll(handler: TrapAllHandler): this;
|
|
87
99
|
/**
|
|
88
|
-
*
|
|
100
|
+
* Fake event emitter to collect events in-memory vs
|
|
101
|
+
* emitting them
|
|
102
|
+
*/
|
|
103
|
+
fake(events?: any[]): FakeEmitter;
|
|
104
|
+
/**
|
|
105
|
+
* Restore fakes
|
|
89
106
|
*/
|
|
90
|
-
restore():
|
|
107
|
+
restore(): void;
|
|
91
108
|
}
|
|
@@ -14,6 +14,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
14
14
|
exports.Emitter = void 0;
|
|
15
15
|
/// <reference path="../../adonis-typings/events.ts" />
|
|
16
16
|
const emittery_1 = __importDefault(require("emittery"));
|
|
17
|
+
const FakeEmitter_1 = require("../FakeEmitter");
|
|
17
18
|
const IocResolver_1 = require("../IocResolver");
|
|
18
19
|
/**
|
|
19
20
|
* Emitter class exposes the API for async event emitter built on top of
|
|
@@ -23,8 +24,16 @@ const IocResolver_1 = require("../IocResolver");
|
|
|
23
24
|
class Emitter {
|
|
24
25
|
constructor(app) {
|
|
25
26
|
this.transport = new emittery_1.default();
|
|
27
|
+
/**
|
|
28
|
+
* Deprecated properties to manage trapping events
|
|
29
|
+
*/
|
|
26
30
|
this.trappingEvents = false;
|
|
27
31
|
this.traps = new Map();
|
|
32
|
+
/**
|
|
33
|
+
* Fakes
|
|
34
|
+
*/
|
|
35
|
+
this.eventsToFake = new Set();
|
|
36
|
+
this.fakeEmitter = new FakeEmitter_1.FakeEmitter();
|
|
28
37
|
if (app) {
|
|
29
38
|
this.iocResolver = new IocResolver_1.IocResolver(app);
|
|
30
39
|
}
|
|
@@ -87,21 +96,30 @@ class Emitter {
|
|
|
87
96
|
*/
|
|
88
97
|
async emit(event, data) {
|
|
89
98
|
try {
|
|
99
|
+
let shouldEmitEvent = true;
|
|
100
|
+
/**
|
|
101
|
+
* Register event with the fake emitter
|
|
102
|
+
*/
|
|
103
|
+
if (this.eventsToFake.has('*') || this.eventsToFake.has(event)) {
|
|
104
|
+
shouldEmitEvent = false;
|
|
105
|
+
this.fakeEmitter.events.push({ name: event, data });
|
|
106
|
+
}
|
|
90
107
|
if (this.trappingEvents) {
|
|
91
108
|
/**
|
|
92
109
|
* Give preference to the handler for a specific event
|
|
93
110
|
*/
|
|
94
111
|
if (this.traps.has(event)) {
|
|
95
|
-
|
|
112
|
+
shouldEmitEvent = false;
|
|
113
|
+
await this.traps.get(event)(data);
|
|
96
114
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (this.trapAllHandler) {
|
|
101
|
-
return await this.trapAllHandler(event, data);
|
|
115
|
+
else if (this.trapAllHandler) {
|
|
116
|
+
shouldEmitEvent = false;
|
|
117
|
+
await this.trapAllHandler(event, data);
|
|
102
118
|
}
|
|
103
119
|
}
|
|
104
|
-
|
|
120
|
+
if (shouldEmitEvent) {
|
|
121
|
+
return await this.transport.emit(event, data);
|
|
122
|
+
}
|
|
105
123
|
}
|
|
106
124
|
catch (error) {
|
|
107
125
|
if (this.errorHandler) {
|
|
@@ -183,6 +201,7 @@ class Emitter {
|
|
|
183
201
|
* Trap event instead of emitting it
|
|
184
202
|
*/
|
|
185
203
|
trap(event, handler) {
|
|
204
|
+
process.emitWarning('DeprecationWarning', '"Event.trap" is deprecated. Instead use "Event.fake" method');
|
|
186
205
|
this.trappingEvents = true;
|
|
187
206
|
this.traps.set(event, handler);
|
|
188
207
|
return this;
|
|
@@ -191,18 +210,41 @@ class Emitter {
|
|
|
191
210
|
* Trap all events instead of emitting them
|
|
192
211
|
*/
|
|
193
212
|
trapAll(handler) {
|
|
213
|
+
process.emitWarning('DeprecationWarning', '"Event.trapAll" is deprecated. Instead use "Event.fake" method');
|
|
194
214
|
this.trappingEvents = true;
|
|
195
215
|
this.trapAllHandler = handler;
|
|
196
216
|
return this;
|
|
197
217
|
}
|
|
198
218
|
/**
|
|
199
|
-
*
|
|
219
|
+
* Fake event emitter to collect events in-memory vs
|
|
220
|
+
* emitting them
|
|
221
|
+
*/
|
|
222
|
+
fake(events) {
|
|
223
|
+
/**
|
|
224
|
+
* If no events have been mentioned, then fake
|
|
225
|
+
* all the events
|
|
226
|
+
*/
|
|
227
|
+
if (!events) {
|
|
228
|
+
this.eventsToFake.add('*');
|
|
229
|
+
return this.fakeEmitter;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Only track event names when wildcard is not added
|
|
233
|
+
*/
|
|
234
|
+
if (!this.eventsToFake.has('*')) {
|
|
235
|
+
events.forEach((event) => this.eventsToFake.add(event));
|
|
236
|
+
}
|
|
237
|
+
return this.fakeEmitter;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Restore fakes
|
|
200
241
|
*/
|
|
201
242
|
restore() {
|
|
202
243
|
this.trappingEvents = false;
|
|
203
|
-
this.traps.clear();
|
|
204
244
|
this.trapAllHandler = undefined;
|
|
205
|
-
|
|
245
|
+
this.traps.clear();
|
|
246
|
+
this.eventsToFake.clear();
|
|
247
|
+
this.fakeEmitter.restore();
|
|
206
248
|
}
|
|
207
249
|
}
|
|
208
250
|
exports.Emitter = Emitter;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { FakeEmitterContract } from '@ioc:Adonis/Core/Event';
|
|
2
|
+
/**
|
|
3
|
+
* Fake emitter to be used for finding and asserting
|
|
4
|
+
* faked events
|
|
5
|
+
*/
|
|
6
|
+
export declare class FakeEmitter implements FakeEmitterContract {
|
|
7
|
+
events: {
|
|
8
|
+
name: string;
|
|
9
|
+
data: any;
|
|
10
|
+
}[];
|
|
11
|
+
/**
|
|
12
|
+
* Get all the emitted events
|
|
13
|
+
*/
|
|
14
|
+
all(): {
|
|
15
|
+
name: string;
|
|
16
|
+
data: any;
|
|
17
|
+
}[];
|
|
18
|
+
/**
|
|
19
|
+
* Returns the size of captured events
|
|
20
|
+
*/
|
|
21
|
+
size(): number;
|
|
22
|
+
/**
|
|
23
|
+
* Find if the event has emitted
|
|
24
|
+
*/
|
|
25
|
+
exists(eventOrCallback: string | ((event: {
|
|
26
|
+
name: string;
|
|
27
|
+
data: any;
|
|
28
|
+
}) => boolean)): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Get selected events
|
|
31
|
+
*/
|
|
32
|
+
filter(eventOrCallback: string | ((event: {
|
|
33
|
+
name: string;
|
|
34
|
+
data: any;
|
|
35
|
+
}) => boolean)): any[];
|
|
36
|
+
/**
|
|
37
|
+
* Find a specific event
|
|
38
|
+
*/
|
|
39
|
+
find(eventOrCallback: string | ((event: {
|
|
40
|
+
name: string;
|
|
41
|
+
data: any;
|
|
42
|
+
}) => boolean)): any;
|
|
43
|
+
restore(): void;
|
|
44
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* @adonisjs/events
|
|
4
|
+
*
|
|
5
|
+
* (c) AdonisJS
|
|
6
|
+
*
|
|
7
|
+
* For the full copyright and license information, please view the LICENSE
|
|
8
|
+
* file that was distributed with this source code.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.FakeEmitter = void 0;
|
|
12
|
+
/**
|
|
13
|
+
* Fake emitter to be used for finding and asserting
|
|
14
|
+
* faked events
|
|
15
|
+
*/
|
|
16
|
+
class FakeEmitter {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.events = [];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Get all the emitted events
|
|
22
|
+
*/
|
|
23
|
+
all() {
|
|
24
|
+
return this.events;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns the size of captured events
|
|
28
|
+
*/
|
|
29
|
+
size() {
|
|
30
|
+
return this.events.length;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Find if the event has emitted
|
|
34
|
+
*/
|
|
35
|
+
exists(eventOrCallback) {
|
|
36
|
+
return !!this.find(eventOrCallback);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get selected events
|
|
40
|
+
*/
|
|
41
|
+
filter(eventOrCallback) {
|
|
42
|
+
if (typeof eventOrCallback === 'function') {
|
|
43
|
+
return this.events.filter(eventOrCallback);
|
|
44
|
+
}
|
|
45
|
+
return this.events.filter((event) => event.name === eventOrCallback);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Find a specific event
|
|
49
|
+
*/
|
|
50
|
+
find(eventOrCallback) {
|
|
51
|
+
if (typeof eventOrCallback === 'function') {
|
|
52
|
+
return this.events.find(eventOrCallback);
|
|
53
|
+
}
|
|
54
|
+
return this.events.find((event) => event.name === eventOrCallback);
|
|
55
|
+
}
|
|
56
|
+
restore() {
|
|
57
|
+
this.events = [];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.FakeEmitter = FakeEmitter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/events",
|
|
3
|
-
"version": "7.1
|
|
3
|
+
"version": "7.2.1",
|
|
4
4
|
"description": "Event emitter with asynchronous events",
|
|
5
5
|
"main": "build/providers/EventProvider.js",
|
|
6
6
|
"files": [
|
|
@@ -13,12 +13,12 @@
|
|
|
13
13
|
"scripts": {
|
|
14
14
|
"mrm": "mrm --preset=@adonisjs/mrm-preset",
|
|
15
15
|
"pretest": "npm run lint",
|
|
16
|
-
"test": "node
|
|
17
|
-
"clean": "del build",
|
|
16
|
+
"test": "node -r @adonisjs/require-ts/build/register bin/test.ts",
|
|
17
|
+
"clean": "del-cli build",
|
|
18
18
|
"compile": "npm run lint && npm run clean && tsc",
|
|
19
19
|
"build": "npm run compile",
|
|
20
20
|
"commit": "git-cz",
|
|
21
|
-
"release": "np",
|
|
21
|
+
"release": "np --message=\"chore(release): %s\"",
|
|
22
22
|
"version": "npm run build",
|
|
23
23
|
"format": "prettier --write .",
|
|
24
24
|
"prepublishOnly": "npm run build",
|
|
@@ -33,19 +33,24 @@
|
|
|
33
33
|
"license": "MIT",
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@adonisjs/application": "^5.1.5",
|
|
36
|
-
"@adonisjs/mrm-preset": "^
|
|
36
|
+
"@adonisjs/mrm-preset": "^5.0.2",
|
|
37
37
|
"@adonisjs/require-ts": "^2.0.7",
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
38
|
+
"@japa/assert": "^1.2.3",
|
|
39
|
+
"@japa/run-failed-tests": "^1.0.3",
|
|
40
|
+
"@japa/runner": "^2.0.6",
|
|
41
|
+
"@japa/spec-reporter": "^1.1.7",
|
|
42
|
+
"@poppinss/dev-utils": "^2.0.1",
|
|
43
|
+
"@types/node": "^17.0.8",
|
|
44
|
+
"commitizen": "^4.2.4",
|
|
45
|
+
"cz-conventional-changelog": "^3.3.0",
|
|
40
46
|
"del-cli": "^4.0.1",
|
|
41
|
-
"eslint": "^7.
|
|
47
|
+
"eslint": "^8.7.0",
|
|
42
48
|
"eslint-config-prettier": "^8.3.0",
|
|
43
|
-
"eslint-plugin-adonis": "^1.
|
|
44
|
-
"eslint-plugin-prettier": "^
|
|
49
|
+
"eslint-plugin-adonis": "^2.1.0",
|
|
50
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
45
51
|
"github-label-sync": "^2.0.1",
|
|
46
52
|
"husky": "^7.0.1",
|
|
47
|
-
"
|
|
48
|
-
"mrm": "^3.0.2",
|
|
53
|
+
"mrm": "^4.0.0",
|
|
49
54
|
"np": "^7.5.0",
|
|
50
55
|
"prettier": "^2.3.2",
|
|
51
56
|
"typescript": "^4.3.5"
|
|
@@ -68,7 +73,7 @@
|
|
|
68
73
|
"anyBranch": false
|
|
69
74
|
},
|
|
70
75
|
"dependencies": {
|
|
71
|
-
"emittery": "^0.
|
|
76
|
+
"emittery": "^0.10.0"
|
|
72
77
|
},
|
|
73
78
|
"peerDependencies": {
|
|
74
79
|
"@adonisjs/application": "^5.0.0"
|
|
@@ -88,5 +93,48 @@
|
|
|
88
93
|
"publishConfig": {
|
|
89
94
|
"access": "public",
|
|
90
95
|
"tag": "latest"
|
|
96
|
+
},
|
|
97
|
+
"mrmConfig": {
|
|
98
|
+
"core": true,
|
|
99
|
+
"license": "MIT",
|
|
100
|
+
"services": [
|
|
101
|
+
"github-actions"
|
|
102
|
+
],
|
|
103
|
+
"minNodeVersion": "14.15.4",
|
|
104
|
+
"probotApps": [
|
|
105
|
+
"stale",
|
|
106
|
+
"lock"
|
|
107
|
+
],
|
|
108
|
+
"runGhActionsOnWindows": false
|
|
109
|
+
},
|
|
110
|
+
"eslintConfig": {
|
|
111
|
+
"extends": [
|
|
112
|
+
"plugin:adonis/typescriptPackage",
|
|
113
|
+
"prettier"
|
|
114
|
+
],
|
|
115
|
+
"plugins": [
|
|
116
|
+
"prettier"
|
|
117
|
+
],
|
|
118
|
+
"rules": {
|
|
119
|
+
"prettier/prettier": [
|
|
120
|
+
"error",
|
|
121
|
+
{
|
|
122
|
+
"endOfLine": "auto"
|
|
123
|
+
}
|
|
124
|
+
]
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
"eslintIgnore": [
|
|
128
|
+
"build"
|
|
129
|
+
],
|
|
130
|
+
"prettier": {
|
|
131
|
+
"trailingComma": "es5",
|
|
132
|
+
"semi": false,
|
|
133
|
+
"singleQuote": true,
|
|
134
|
+
"useTabs": false,
|
|
135
|
+
"quoteProps": "consistent",
|
|
136
|
+
"bracketSpacing": true,
|
|
137
|
+
"arrowParens": "always",
|
|
138
|
+
"printWidth": 100
|
|
91
139
|
}
|
|
92
140
|
}
|