This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plug.ts
125 lines (109 loc) · 3.03 KB
/
Plug.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
import {Logable} from "@U/logger.js";
import type {JSONable} from "@U/Models.js";
import type {Corpus} from "@U/Corpus.js";
import {canCall} from "@U/Corpus.js";
const State = {
create: 0,
installed: 1,
uninstalled: 2,
error: 3,
} as const;
export abstract class Plug extends Logable implements JSONable {
public static readonly plugs: Map<new() => Plug, Plug> = new Map();
public static readonly corpuses: Corpus[] = [];
/**获取当前类的实例*/
public static getInst<T extends Plug>(this: new() => T): T {
let instance: Plug | undefined = Plug.plugs.get(this);
if (!(instance instanceof this)) {
instance = new this();
Plug.plugs.set(this, instance);
}
return <T>instance;
}
public static exitAll(): Promise<void> {
return Promise.all([...this.plugs.values()].map((p) => p.uninstall())).then(_ => {
this.logger.info(">>>>>>>>>> 全部卸载完成 <<<<<<<<<<");
if (process.execArgv.includes("--inspect")) {
return;
}
setTimeout(() => {
this.logger.info("退出");
process.exit(0);
}, 500);
});
}
public readonly module: NodeModule;
public declare readonly __proto__: Readonly<this>;
public description: string = "这个插件没有描述";
public _error: any = null;
public name: string = this.constructor.name;
#state: typeof State[keyof typeof State] = State.create;
protected constructor(module: NodeModule) {
super();
this.module = module;
this.#init();
}
public async install(): Promise<void> {
}
public async uninstall(): Promise<void> {
}
public override toString() {
return `${this.constructor.name} {name: ${this.name}, State: ${this.#state}}`;
}
public toJSON() {
return {"name": this.name, "description": this.description, "State": this.state};
}
#init(): void {
this.install = async function () {
try {
if (this.#state === State.error) {
return;
}
if (this.#state === State.installed) {
return;
}
await this.__proto__.install.call(this);
this.#state = State.installed;
canCall.mergeTo(this, Plug.corpuses);
this.logger.info("已启动\t" + this.toString());
} catch (e) {
this.error = e;
}
};
this.uninstall = async function () {
try {
if (this.#state === State.uninstalled) {
return;
}
await this.__proto__.uninstall.call(this);
if (this.#state === State.error) {
return;
}
this.#state = State.uninstalled;
canCall.separateTo(this, Plug.corpuses);
this.logger.info("已停止\t" + this.toString());
} catch (e) {
this.error = e;
}
};
this.#state = State.uninstalled;
this.logger.debug("init:\t" + this.module.filename);
}
public get error() {
return this._error;
}
public set error(e: any) {
this.#state = State.error;
this._error = e;
this.logger.error(e);
}
public get state(): string {
return Object.entries(State).find(v => v[1] === this.#state)?.[0] ?? "unknown";
}
public get corpus(): Corpus[] {
return Plug.corpuses.filter(v => v.plug === this);
}
}
export interface PlugDecorator {
(target: Plug, propertyKey: string): void;
}