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
101 lines (84 loc) · 2.29 KB
/
plugin.ts
File metadata and controls
101 lines (84 loc) · 2.29 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
import { getLogger, Logger } from '@alilc/lowcode-utils';
import {
ILowCodePluginRuntime,
ILowCodePluginManager,
} from './plugin-types';
import {
IPublicTypePluginConfig,
IPublicTypePluginMeta,
} from '@alilc/lowcode-types';
import { invariant } from '../utils';
export class LowCodePluginRuntime implements ILowCodePluginRuntime {
config: IPublicTypePluginConfig;
logger: Logger;
private manager: ILowCodePluginManager;
private _inited: boolean;
private pluginName: string;
meta: IPublicTypePluginMeta;
/**
* 标识插件状态,是否被 disabled
*/
private _disabled: boolean;
constructor(
pluginName: string,
manager: ILowCodePluginManager,
config: IPublicTypePluginConfig,
meta: IPublicTypePluginMeta,
) {
this.manager = manager;
this.config = config;
this.pluginName = pluginName;
this.meta = meta;
this.logger = getLogger({ level: 'warn', bizName: `plugin:${pluginName}` });
}
get name() {
return this.pluginName;
}
get dep() {
if (typeof this.meta.dependencies === 'string') {
return [this.meta.dependencies];
}
// compat legacy way to declare dependencies
const legacyDepValue = (this.config as any).dep;
if (typeof legacyDepValue === 'string') {
return [legacyDepValue];
}
return this.meta.dependencies || legacyDepValue || [];
}
get disabled() {
return this._disabled;
}
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);
}
}