forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.ts
More file actions
113 lines (92 loc) · 2.54 KB
/
plugin.ts
File metadata and controls
113 lines (92 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { getLogger, Logger } from '@alilc/lowcode-utils';
import {
ILowCodePlugin,
ILowCodePluginConfig,
ILowCodePluginManager,
ILowCodePluginConfigMeta,
} from './plugin-types';
import { EventEmitter } from 'events';
import { invariant } from '../utils';
export class LowCodePlugin implements ILowCodePlugin {
config: ILowCodePluginConfig;
logger: Logger;
private manager: ILowCodePluginManager;
private emitter: EventEmitter;
private _inited: boolean;
private pluginName: string;
private meta: ILowCodePluginConfigMeta;
/**
* 标识插件状态,是否被 disabled
*/
private _disabled: boolean;
constructor(
pluginName: string,
manager: ILowCodePluginManager,
config: ILowCodePluginConfig,
meta: ILowCodePluginConfigMeta,
) {
this.manager = manager;
this.config = config;
this.emitter = new EventEmitter();
this.pluginName = pluginName;
this.meta = meta;
this.logger = getLogger({ level: 'warn', bizName: `designer:plugin:${pluginName}` });
}
get name() {
return this.pluginName;
}
get dep() {
if (typeof this.meta.dependencies === 'string') {
return [this.meta.dependencies];
}
return this.meta.dependencies || [];
}
get disabled() {
return this._disabled;
}
on(event: string | symbol, listener: (...args: any[]) => void): any {
this.emitter.on(event, listener);
return () => {
this.emitter.off(event, listener);
};
}
emit(event: string | symbol, ...args: any[]) {
return this.emitter.emit(event, ...args);
}
removeAllListeners(event: string | symbol): any {
return this.emitter.removeAllListeners(event);
}
isInited() {
return this._inited;
}
async init(forceInit?: boolean) {
if (this._inited && !forceInit) return;
this.logger.log('method init called');
await this.config.init?.call(undefined);
this._inited = true;
}
async destroy() {
if (!this._inited) return;
this.logger.log('method destroy called');
await this.config?.destroy?.call(undefined);
this._inited = false;
}
setDisabled(flag = true) {
this._disabled = flag;
}
toProxy() {
invariant(this._inited, 'Could not call toProxy before init');
const exports = this.config.exports?.();
return new Proxy(this, {
get(target, prop, receiver) {
if ({}.hasOwnProperty.call(exports, prop)) {
return exports?.[prop as string];
}
return Reflect.get(target, prop, receiver);
},
});
}
async dispose() {
await this.manager.delete(this.name);
}
}