@midwayjs/event-emitter 4.0.0-alpha.1 → 4.0.0-beta.10
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 +21 -0
- package/README.md +1 -1
- package/dist/configuration.d.ts +2 -1
- package/dist/configuration.js +2 -9
- package/dist/decorator.d.ts +2 -2
- package/dist/decorator.js +9 -6
- package/dist/interface.d.ts +17 -1
- package/dist/service.d.ts +4 -1
- package/dist/service.js +65 -3
- package/package.json +6 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 - Now midwayjs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
package/dist/configuration.d.ts
CHANGED
package/dist/configuration.js
CHANGED
|
@@ -8,23 +8,16 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.EventEmitterConfiguration = void 0;
|
|
10
10
|
const core_1 = require("@midwayjs/core");
|
|
11
|
+
const service_1 = require("./service");
|
|
11
12
|
let EventEmitterConfiguration = class EventEmitterConfiguration {
|
|
12
13
|
async onReady(container) {
|
|
14
|
+
await container.getAsync(service_1.EventEmitterService);
|
|
13
15
|
}
|
|
14
16
|
};
|
|
15
17
|
exports.EventEmitterConfiguration = EventEmitterConfiguration;
|
|
16
18
|
exports.EventEmitterConfiguration = EventEmitterConfiguration = __decorate([
|
|
17
19
|
(0, core_1.Configuration)({
|
|
18
20
|
namespace: 'eventEmitter',
|
|
19
|
-
importConfigs: [
|
|
20
|
-
{
|
|
21
|
-
default: {
|
|
22
|
-
eventEmitter: {
|
|
23
|
-
defaultMaxListeners: 10,
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
],
|
|
28
21
|
})
|
|
29
22
|
], EventEmitterConfiguration);
|
|
30
23
|
//# sourceMappingURL=configuration.js.map
|
package/dist/decorator.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function OnEvent(event:
|
|
1
|
+
import { OnEventOptions } from './interface';
|
|
2
|
+
export declare function OnEvent(event: string, options?: OnEventOptions): MethodDecorator;
|
|
3
3
|
//# sourceMappingURL=decorator.d.ts.map
|
package/dist/decorator.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OnEvent =
|
|
4
|
-
const core_1 = require("@midwayjs/core");
|
|
3
|
+
exports.OnEvent = OnEvent;
|
|
5
4
|
const const_1 = require("./const");
|
|
5
|
+
const core_1 = require("@midwayjs/core");
|
|
6
6
|
function OnEvent(event, options) {
|
|
7
|
-
return (target, propertyKey) => {
|
|
7
|
+
return (target, propertyKey, descriptor) => {
|
|
8
8
|
core_1.DecoratorManager.saveModule(const_1.EVENT_KEY, target.constructor);
|
|
9
9
|
core_1.MetadataManager.defineMetadata(const_1.EVENT_KEY, {
|
|
10
10
|
event,
|
|
11
|
-
options
|
|
12
|
-
|
|
11
|
+
options: {
|
|
12
|
+
...options,
|
|
13
|
+
suppressErrors: options?.suppressErrors ?? true,
|
|
14
|
+
},
|
|
15
|
+
}, target, propertyKey);
|
|
16
|
+
return descriptor;
|
|
13
17
|
};
|
|
14
18
|
}
|
|
15
|
-
exports.OnEvent = OnEvent;
|
|
16
19
|
//# sourceMappingURL=decorator.js.map
|
package/dist/interface.d.ts
CHANGED
|
@@ -1,4 +1,20 @@
|
|
|
1
|
-
import { ConstructorOptions } from 'eventemitter2';
|
|
1
|
+
import { ConstructorOptions, OnOptions } from 'eventemitter2';
|
|
2
2
|
export interface EventEmitterConfigOptions extends ConstructorOptions {
|
|
3
3
|
}
|
|
4
|
+
export type OnEventOptions = OnOptions & {
|
|
5
|
+
/**
|
|
6
|
+
* If "true", prepends (instead of append) the given listener to the array of listeners.
|
|
7
|
+
*
|
|
8
|
+
* @see https://github.com/EventEmitter2/EventEmitter2#emitterprependlistenerevent-listener-options
|
|
9
|
+
*
|
|
10
|
+
* @default false
|
|
11
|
+
*/
|
|
12
|
+
prependListener?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* If "true", the onEvent callback will not throw an error while handling the event. Otherwise, if "false" it will throw an error.
|
|
15
|
+
*
|
|
16
|
+
* @default true
|
|
17
|
+
*/
|
|
18
|
+
suppressErrors?: boolean;
|
|
19
|
+
};
|
|
4
20
|
//# sourceMappingURL=interface.d.ts.map
|
package/dist/service.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { EventEmitter2 as EventEmitter } from 'eventemitter2';
|
|
2
|
-
export declare class EventEmitterService
|
|
2
|
+
export declare class EventEmitterService {
|
|
3
3
|
private eventEmitter;
|
|
4
4
|
private config;
|
|
5
|
+
private applicationContext;
|
|
5
6
|
init(): Promise<void>;
|
|
6
7
|
getEventEmitter(): EventEmitter;
|
|
8
|
+
emitAsync(event: string | symbol | string[], ...args: any[]): Promise<any[]>;
|
|
9
|
+
emit(event: string | symbol | string[], ...args: any[]): boolean;
|
|
7
10
|
}
|
|
8
11
|
export interface EventEmitterService extends EventEmitter {
|
|
9
12
|
}
|
package/dist/service.js
CHANGED
|
@@ -14,12 +14,16 @@ const eventemitter2_1 = require("eventemitter2");
|
|
|
14
14
|
const core_1 = require("@midwayjs/core");
|
|
15
15
|
const const_1 = require("./const");
|
|
16
16
|
let EventEmitterService = class EventEmitterService {
|
|
17
|
+
eventEmitter;
|
|
18
|
+
config;
|
|
19
|
+
applicationContext;
|
|
17
20
|
async init() {
|
|
18
21
|
this.eventEmitter = new eventemitter2_1.EventEmitter2(this.config);
|
|
19
22
|
// proxy eventEmitter public methods to this
|
|
20
23
|
const methods = Object.getOwnPropertyNames(eventemitter2_1.EventEmitter2.prototype);
|
|
21
24
|
for (const method of methods) {
|
|
22
|
-
if (method !== 'constructor' &&
|
|
25
|
+
if (method !== 'constructor' &&
|
|
26
|
+
typeof this.eventEmitter[method] === 'function') {
|
|
23
27
|
this[method] = this.eventEmitter[method].bind(this.eventEmitter);
|
|
24
28
|
}
|
|
25
29
|
}
|
|
@@ -27,20 +31,78 @@ let EventEmitterService = class EventEmitterService {
|
|
|
27
31
|
for (const eventModule of events) {
|
|
28
32
|
const props = core_1.MetadataManager.getPropertiesWithMetadata(const_1.EVENT_KEY, eventModule);
|
|
29
33
|
for (const prop in props) {
|
|
30
|
-
const { event, options } = props[prop];
|
|
31
|
-
|
|
34
|
+
const { event, options = {} } = props[prop];
|
|
35
|
+
const { prependListener, suppressErrors = true, ...emitterOptions } = options;
|
|
36
|
+
const listener = async (...args) => {
|
|
37
|
+
try {
|
|
38
|
+
const instance = await this.applicationContext.getAsync(eventModule);
|
|
39
|
+
await instance[prop](...args);
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
if (!suppressErrors) {
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
// 如果 suppressErrors 为 true,则忽略错误
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
if (prependListener) {
|
|
49
|
+
this.eventEmitter.prependListener(event, listener, emitterOptions);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
this.eventEmitter.on(event, listener, emitterOptions);
|
|
53
|
+
}
|
|
32
54
|
}
|
|
33
55
|
}
|
|
34
56
|
}
|
|
35
57
|
getEventEmitter() {
|
|
36
58
|
return this.eventEmitter;
|
|
37
59
|
}
|
|
60
|
+
async emitAsync(event, ...args) {
|
|
61
|
+
const listeners = this.eventEmitter.listeners(event);
|
|
62
|
+
const results = [];
|
|
63
|
+
// 执行所有监听器
|
|
64
|
+
for (const listener of listeners) {
|
|
65
|
+
try {
|
|
66
|
+
const result = await listener(...args);
|
|
67
|
+
if (result !== undefined) {
|
|
68
|
+
results.push(result);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
// 如果错误不应该被抑制,则抛出
|
|
73
|
+
if (!err.suppressErrors) {
|
|
74
|
+
throw err;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return results;
|
|
79
|
+
}
|
|
80
|
+
emit(event, ...args) {
|
|
81
|
+
const result = this.eventEmitter.emit(event, ...args);
|
|
82
|
+
// 处理异步监听器
|
|
83
|
+
const listeners = this.eventEmitter.listeners(event);
|
|
84
|
+
Promise.all(listeners.map(async (listener) => {
|
|
85
|
+
try {
|
|
86
|
+
if (listener.constructor.name === 'AsyncFunction') {
|
|
87
|
+
await listener(...args);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
// 忽略同步 emit 中的错误
|
|
92
|
+
}
|
|
93
|
+
}));
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
38
96
|
};
|
|
39
97
|
exports.EventEmitterService = EventEmitterService;
|
|
40
98
|
__decorate([
|
|
41
99
|
(0, core_1.Config)('eventEmitter'),
|
|
42
100
|
__metadata("design:type", Object)
|
|
43
101
|
], EventEmitterService.prototype, "config", void 0);
|
|
102
|
+
__decorate([
|
|
103
|
+
(0, core_1.ApplicationContext)(),
|
|
104
|
+
__metadata("design:type", Object)
|
|
105
|
+
], EventEmitterService.prototype, "applicationContext", void 0);
|
|
44
106
|
__decorate([
|
|
45
107
|
(0, core_1.Init)(),
|
|
46
108
|
__metadata("design:type", Function),
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/event-emitter",
|
|
3
3
|
"description": "midway event emitter component",
|
|
4
|
-
"version": "4.0.0-
|
|
4
|
+
"version": "4.0.0-beta.10",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
"index.d.ts"
|
|
11
11
|
],
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"@midwayjs/core": "
|
|
14
|
-
"@midwayjs/mock": "
|
|
13
|
+
"@midwayjs/core": "^4.0.0-beta.10",
|
|
14
|
+
"@midwayjs/mock": "^4.0.0-beta.10"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"eventemitter2": "6.4.9"
|
|
@@ -30,10 +30,11 @@
|
|
|
30
30
|
"lint": "mwts check"
|
|
31
31
|
},
|
|
32
32
|
"engines": {
|
|
33
|
-
"node": ">=
|
|
33
|
+
"node": ">=20"
|
|
34
34
|
},
|
|
35
35
|
"repository": {
|
|
36
36
|
"type": "git",
|
|
37
37
|
"url": "https://github.com/midwayjs/midway.git"
|
|
38
|
-
}
|
|
38
|
+
},
|
|
39
|
+
"gitHead": "1b1856629913703f67304155aaf611ec936a81ac"
|
|
39
40
|
}
|