floating-live 0.17.0-alpha.3 → 0.17.0-alpha.4

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.
@@ -1,16 +1,4 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
- if (kind === "m") throw new TypeError("Private method is not writable");
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
- };
12
- var _CommonPluginContext_app, _CommonPluginContext_accessApp, _CommonPluginContext_abortController, _CommonPluginContext_registered, _CommonPluginContext_whenRegisterMap;
13
- import { AppError } from "../../error/index.js";
1
+ import { AppErrorLegacy } from "../../error/index.js";
14
2
  class ListMap extends Map {
15
3
  addItem(key, value) {
16
4
  const list = this.get(key);
@@ -30,261 +18,150 @@ export class CommonPluginContext {
30
18
  return false;
31
19
  }
32
20
  getApp() {
33
- return __classPrivateFieldGet(this, _CommonPluginContext_accessApp, "f") ? __classPrivateFieldGet(this, _CommonPluginContext_app, "f") : null;
34
- }
21
+ return this.#accessApp ? this.#app : null;
22
+ }
23
+ /** 主程序,插件不可访问 */
24
+ #app;
25
+ #accessApp;
26
+ /** AbortController,插件不可访问 */
27
+ #abortController = new AbortController();
28
+ #registered = {
29
+ plugins: new Set(),
30
+ values: new Set(),
31
+ commands: new Set(),
32
+ hooks: new ListMap(),
33
+ events: new ListMap(),
34
+ };
35
+ #whenRegisterMap = new ListMap();
35
36
  getRegisteredPlugins() {
36
- return [...__classPrivateFieldGet(this, _CommonPluginContext_registered, "f").plugins];
37
+ return [...this.#registered.plugins];
37
38
  }
39
+ /** 插件名称 */
40
+ pluginName;
38
41
  constructor(app, pluginName, options) {
39
- /** 主程序,插件不可访问 */
40
- _CommonPluginContext_app.set(this, void 0);
41
- _CommonPluginContext_accessApp.set(this, void 0);
42
- /** AbortController,插件不可访问 */
43
- _CommonPluginContext_abortController.set(this, new AbortController());
44
- _CommonPluginContext_registered.set(this, {
45
- plugins: new Set(),
46
- values: new Set(),
47
- commands: new Set(),
48
- hooks: new ListMap(),
49
- events: new ListMap(),
50
- });
51
- _CommonPluginContext_whenRegisterMap.set(this, new ListMap());
52
- __classPrivateFieldSet(this, _CommonPluginContext_app, app, "f");
42
+ this.#app = app;
53
43
  this.pluginName = pluginName;
54
- __classPrivateFieldSet(this, _CommonPluginContext_accessApp, options?.accessApp ?? false, "f");
44
+ this.#accessApp = options?.accessApp ?? false;
55
45
  }
56
46
  /** 抛出错误
57
47
  *
58
48
  * 一般用于自身导致的错误,若非插件本身导致的错误,使用throw语句即可
59
49
  */
60
50
  throw(err) {
61
- err.source ?? (err.source = `plugin:${this.pluginName}`);
51
+ err.source ??= `plugin:${this.pluginName}`;
62
52
  throw err;
63
53
  }
64
54
  error(id, options) {
65
- return new AppError(id, options);
55
+ return new AppErrorLegacy(id, options);
66
56
  }
67
57
  register(plugin, options) {
68
- try {
69
- const p = __classPrivateFieldGet(this, _CommonPluginContext_app, "f").register(plugin, options);
70
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").plugins.add(plugin.name);
71
- return p;
72
- }
73
- catch (err) {
74
- throw this.throw(err);
75
- }
58
+ const p = this.#app.register(plugin, options);
59
+ this.#registered.plugins.add(plugin.name);
60
+ return p;
76
61
  }
77
62
  unregister(pluginName) {
78
- try {
79
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregister(pluginName);
80
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").plugins.delete(pluginName);
81
- }
82
- catch (err) {
83
- this.throw(err);
84
- }
63
+ this.#app.unregister(pluginName);
64
+ this.#registered.plugins.delete(pluginName);
85
65
  }
86
66
  getPluginExposes(pluginName) {
87
- return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").getPluginExposes(pluginName);
67
+ return this.#app.getPluginExposes(pluginName);
88
68
  }
89
69
  whenRegister(pluginName, callback) {
90
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").whenRegister(pluginName, callback);
91
- __classPrivateFieldGet(this, _CommonPluginContext_whenRegisterMap, "f").addItem(pluginName, callback);
70
+ this.#app.whenRegister(pluginName, callback);
71
+ this.#whenRegisterMap.addItem(pluginName, callback);
92
72
  }
93
73
  cancelWhenRegister(pluginName, callback) {
94
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").cancelWhenRegister(pluginName, callback);
95
- __classPrivateFieldGet(this, _CommonPluginContext_whenRegisterMap, "f").deleteItem(pluginName, callback);
74
+ this.#app.cancelWhenRegister(pluginName, callback);
75
+ this.#whenRegisterMap.deleteItem(pluginName, callback);
96
76
  }
97
77
  hasPlugin(pluginName) {
98
- return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").hasPlugin(pluginName);
78
+ return this.#app.hasPlugin(pluginName);
99
79
  }
100
80
  registerValue(name, options) {
101
- try {
102
- const ctx = __classPrivateFieldGet(this, _CommonPluginContext_app, "f").registerValue(name, options);
103
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").values.add(name);
104
- return ctx;
105
- }
106
- catch (err) {
107
- throw this.throw(err);
108
- }
81
+ const ctx = this.#app.registerValue(name, options);
82
+ this.#registered.values.add(name);
83
+ return ctx;
109
84
  }
110
85
  unregisterValue(name) {
111
- try {
112
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregisterValue(name);
113
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").values.delete(name);
114
- }
115
- catch (err) {
116
- this.throw(err);
117
- }
86
+ this.#app.unregisterValue(name);
87
+ this.#registered.values.delete(name);
118
88
  }
119
89
  registerCommand(name, func, options) {
120
- try {
121
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").registerCommand(name, func, options);
122
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").commands.add(name);
123
- }
124
- catch (err) {
125
- this.throw(err);
126
- }
90
+ this.#app.registerCommand(name, func, options);
91
+ this.#registered.commands.add(name);
127
92
  }
128
93
  unregisterCommand(name) {
129
- try {
130
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregisterCommand(name);
131
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").commands.delete(name);
132
- }
133
- catch (err) {
134
- this.throw(err);
135
- }
94
+ this.#app.unregisterCommand(name);
95
+ this.#registered.commands.delete(name);
136
96
  }
137
97
  hasCommand(name) {
138
- try {
139
- return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").hasCommand(name);
140
- }
141
- catch (err) {
142
- throw this.throw(err);
143
- }
98
+ return this.#app.hasCommand(name);
144
99
  }
145
100
  on(type, listener) {
146
- try {
147
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").on(type, listener, this.signal);
148
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").events.addItem(type, listener);
149
- }
150
- catch (err) {
151
- this.throw(err);
152
- }
101
+ this.#app.on(type, listener, this.signal);
102
+ this.#registered.events.addItem(type, listener);
153
103
  }
154
104
  once(type, listener) {
155
- try {
156
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").once(type, listener, this.signal);
157
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").events.addItem(type, listener);
158
- }
159
- catch (err) {
160
- this.throw(err);
161
- }
105
+ this.#app.once(type, listener, this.signal);
106
+ this.#registered.events.addItem(type, listener);
162
107
  }
163
108
  off(type, listener) {
164
- try {
165
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").off(type, listener);
166
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").events.deleteItem(type, listener);
167
- }
168
- catch (err) {
169
- this.throw(err);
170
- }
109
+ this.#app.off(type, listener);
110
+ this.#registered.events.deleteItem(type, listener);
171
111
  }
172
112
  emit(type, detail, options) {
173
- try {
174
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").emit(type, detail || {}, {
175
- source: `plugin:${this.pluginName}`,
176
- ...options,
177
- });
178
- }
179
- catch (err) {
180
- this.throw(err);
181
- }
113
+ this.#app.emit(type, detail || {}, {
114
+ source: `plugin:${this.pluginName}`,
115
+ ...options,
116
+ });
182
117
  }
183
118
  call(name, ...args) {
184
- try {
185
- return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").callWithOptions(name, { source: `plugin:${this.pluginName}` }, ...args);
186
- }
187
- catch (err) {
188
- this.throw(err);
189
- throw "";
190
- }
119
+ return this.#app.callWithOptions(name, { source: `plugin:${this.pluginName}` }, ...args);
191
120
  }
192
121
  callWithOptions(name, options, ...args) {
193
- try {
194
- return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").callWithOptions(name, options, ...args);
195
- }
196
- catch (err) {
197
- this.throw(err);
198
- throw "";
199
- }
122
+ return this.#app.callWithOptions(name, options, ...args);
200
123
  }
201
124
  useHook(name, func, options) {
202
- try {
203
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").useHook(name, func, options);
204
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").hooks.addItem(name, func);
205
- }
206
- catch (err) {
207
- this.throw(err);
208
- }
125
+ this.#app.useHook(name, func, options);
126
+ this.#registered.hooks.addItem(name, func);
209
127
  }
210
128
  unuseHook(name, func) {
211
- try {
212
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unuseHook(name, func);
213
- __classPrivateFieldGet(this, _CommonPluginContext_registered, "f").hooks.deleteItem(name, func);
214
- }
215
- catch (err) {
216
- throw this.throw(err);
217
- }
129
+ this.#app.unuseHook(name, func);
130
+ this.#registered.hooks.deleteItem(name, func);
218
131
  }
219
132
  callHook(name, ctx, options) {
220
- try {
221
- return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").callHook(name, ctx);
222
- }
223
- catch (err) {
224
- throw this.throw(err);
225
- }
133
+ return this.#app.callHook(name, ctx);
226
134
  }
227
135
  callHookSync(name, ctx, options) {
228
- try {
229
- return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").callHookSync(name, ctx);
230
- }
231
- catch (err) {
232
- throw this.throw(err);
233
- }
136
+ return this.#app.callHookSync(name, ctx);
234
137
  }
235
138
  watch(name, watcher) {
236
- try {
237
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").watch(name, watcher);
238
- }
239
- catch (err) {
240
- throw this.throw(err);
241
- }
139
+ this.#app.watch(name, watcher);
242
140
  }
243
141
  unwatch(name, watcher) {
244
- try {
245
- __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unwatch(name, watcher);
246
- }
247
- catch (err) {
248
- throw this.throw(err);
249
- }
142
+ this.#app.unwatch(name, watcher);
250
143
  }
251
144
  hasValue(name) {
252
- try {
253
- return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").hasValue(name);
254
- }
255
- catch (err) {
256
- throw this.throw(err);
257
- }
145
+ return this.#app.hasValue(name);
258
146
  }
259
147
  getValue(name) {
260
- try {
261
- return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").getValue(name);
262
- }
263
- catch (err) {
264
- throw this.throw(err);
265
- }
148
+ return this.#app.getValue(name);
266
149
  }
267
150
  setValue(name, value) {
268
- try {
269
- return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").setValue(name, value);
270
- }
271
- catch (err) {
272
- throw this.throw(err);
273
- }
151
+ return this.#app.setValue(name, value);
274
152
  }
275
153
  destroy() {
276
- __classPrivateFieldGet(this, _CommonPluginContext_whenRegisterMap, "f").forEach((callbacks, pluginName) => {
277
- callbacks.forEach((callback) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").cancelWhenRegister(pluginName, callback));
154
+ this.#whenRegisterMap.forEach((callbacks, pluginName) => {
155
+ callbacks.forEach((callback) => this.#app.cancelWhenRegister(pluginName, callback));
278
156
  });
279
- const { plugins, values, hooks, commands } = __classPrivateFieldGet(this, _CommonPluginContext_registered, "f");
280
- plugins.forEach((pluginName) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregister(pluginName));
281
- hooks.forEach((hookList, name) => hookList.forEach((hookFunc) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unuseHook(name, hookFunc)));
282
- commands.forEach((name) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregisterCommand(name));
283
- values.forEach((name) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregisterValue(name));
284
- __classPrivateFieldGet(this, _CommonPluginContext_abortController, "f").abort();
157
+ const { plugins, values, hooks, commands } = this.#registered;
158
+ plugins.forEach((pluginName) => this.#app.unregister(pluginName));
159
+ hooks.forEach((hookList, name) => hookList.forEach((hookFunc) => this.#app.unuseHook(name, hookFunc)));
160
+ commands.forEach((name) => this.#app.unregisterCommand(name));
161
+ values.forEach((name) => this.#app.unregisterValue(name));
162
+ this.#abortController.abort();
285
163
  }
286
164
  get signal() {
287
- return __classPrivateFieldGet(this, _CommonPluginContext_abortController, "f").signal;
165
+ return this.#abortController.signal;
288
166
  }
289
167
  }
290
- _CommonPluginContext_app = new WeakMap(), _CommonPluginContext_accessApp = new WeakMap(), _CommonPluginContext_abortController = new WeakMap(), _CommonPluginContext_registered = new WeakMap(), _CommonPluginContext_whenRegisterMap = new WeakMap();
@@ -1,91 +1,78 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _SafePluginContext_permissions;
13
- import { AppError } from "../../error/index.js";
1
+ import { AppErrorLegacy } from "../../error/index.js";
14
2
  import { CommonPluginContext } from "./CommonPluginContext.js";
15
3
  export class SafePluginContext extends CommonPluginContext {
16
4
  get safe() {
17
5
  return true;
18
6
  }
7
+ #permissions = {};
19
8
  constructor(app, pluginName, options) {
20
9
  super(app, pluginName);
21
- _SafePluginContext_permissions.set(this, {});
22
- __classPrivateFieldSet(this, _SafePluginContext_permissions, options.permissions, "f");
10
+ this.#permissions = options.permissions;
23
11
  }
24
12
  throwNotPermitted(permission, cause) {
25
- this.throw(new AppError("plugin:no_permission", {
13
+ this.throw(new AppErrorLegacy("plugin:no_permission", {
26
14
  message: "插件权限不足",
27
15
  cause,
28
16
  permission,
29
17
  }));
30
18
  }
31
19
  register(plugin, options) {
32
- if (!__classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["plugin.register"]) {
20
+ if (!this.#permissions["plugin.register"]) {
33
21
  this.throwNotPermitted("plugin.register", "无插件安装权限");
34
22
  }
35
23
  return super.register(plugin, options);
36
24
  }
37
25
  unregister(pluginName) {
38
- if (!__classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["plugin.register"]) {
26
+ if (!this.#permissions["plugin.register"]) {
39
27
  this.throwNotPermitted("plugin.register", "无插件安装权限");
40
28
  }
41
29
  return super.unregister(pluginName);
42
30
  }
43
31
  emit(type, detail, options) {
44
- if (!(__classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["event.emit.advanced"] ||
45
- __classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["event.emit"]?.includes(type))) {
32
+ if (!(this.#permissions["event.emit.advanced"] ||
33
+ this.#permissions["event.emit"]?.includes(type))) {
46
34
  this.throwNotPermitted(`event.emit:${type}`, `无事件[${type}]的发送权限`);
47
35
  }
48
- if (!__classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["event.emit.advanced"] && options) {
36
+ if (!this.#permissions["event.emit.advanced"] && options) {
49
37
  this.throwNotPermitted("event.emit.advanced", `无事件高级发送权限`);
50
38
  }
51
39
  return super.emit(type, detail, options);
52
40
  }
53
41
  call(name, ...args) {
54
- if (!(__classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["command.call.advanced"] ||
55
- __classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["command.call"]?.includes(name))) {
42
+ if (!(this.#permissions["command.call.advanced"] ||
43
+ this.#permissions["command.call"]?.includes(name))) {
56
44
  this.throwNotPermitted(`command.call:${name}`, `无命令[${name}]的调用权限`);
57
45
  }
58
46
  return super.call(name, ...args);
59
47
  }
60
48
  callWithOptions(name, options, ...args) {
61
- if (!__classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["command.call.advanced"]) {
49
+ if (!this.#permissions["command.call.advanced"]) {
62
50
  this.throwNotPermitted("command.call.advanced", "无命令高级调用权限");
63
51
  }
64
52
  return super.callWithOptions(name, options, ...args);
65
53
  }
66
54
  registerCommand(name, func, options) {
67
- if (!__classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["command.register"]?.includes(name)) {
55
+ if (!this.#permissions["command.register"]?.includes(name)) {
68
56
  this.throwNotPermitted(`command.call:${name}`, `无命令[${name}]的注册权限`);
69
57
  }
70
58
  return super.registerCommand(name, func, options);
71
59
  }
72
60
  useHook(name, func, options) {
73
- if (!__classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["hook.use"]?.includes(name)) {
61
+ if (!this.#permissions["hook.use"]?.includes(name)) {
74
62
  this.throwNotPermitted(`hook.use:${name}`, `无钩子[${name}]的挂载权限`);
75
63
  }
76
64
  return super.useHook(name, func, options);
77
65
  }
78
66
  unuseHook(name, func) {
79
- if (!__classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["hook.use"]?.includes(name)) {
67
+ if (!this.#permissions["hook.use"]?.includes(name)) {
80
68
  this.throwNotPermitted(`hook.use:${name}`, `无钩子[${name}]的挂载权限`);
81
69
  }
82
70
  return super.unuseHook(name, func);
83
71
  }
84
72
  callHook(name, ctx, options) {
85
- if (!__classPrivateFieldGet(this, _SafePluginContext_permissions, "f")["hook.call"]?.includes(name)) {
73
+ if (!this.#permissions["hook.call"]?.includes(name)) {
86
74
  this.throwNotPermitted(`hook.call:${name}`, `无钩子[${name}]的调用权限`);
87
75
  }
88
76
  return super.callHook(name, ctx);
89
77
  }
90
78
  }
91
- _SafePluginContext_permissions = new WeakMap();
@@ -1,11 +1,8 @@
1
- import { AppError } from "../error/index.js";
2
1
  import type { PluginItem, PluginContext } from "./types.js";
3
2
  export type * from "./types.js";
4
3
  export declare class BasePlugin implements PluginItem {
5
4
  static pluginName: string;
6
5
  get pluginName(): string;
7
6
  ctx: PluginContext;
8
- readonly Error: typeof AppError;
9
- readonly throw: (err?: Error) => void;
10
7
  constructor(ctx: PluginContext, options?: any);
11
8
  }
@@ -1,11 +1,10 @@
1
- import { AppError } from "../error/index.js";
2
1
  export class BasePlugin {
2
+ static pluginName;
3
3
  get pluginName() {
4
4
  return this.constructor.pluginName;
5
5
  }
6
+ ctx;
6
7
  constructor(ctx, options) {
7
- this.Error = AppError;
8
8
  this.ctx = ctx;
9
- this.throw = ctx.throw;
10
9
  }
11
10
  }
@@ -1,29 +1,14 @@
1
1
  import type { App } from "../app.js";
2
- import { PluginItem, PluginConstructor, PluginContext, PluginRegisterOptions } from "./types.js";
3
- export interface PluginData<T> {
4
- plugin: PluginItem;
5
- context: PluginContext;
6
- exposes?: T;
7
- unremovable: boolean;
8
- /** 可访问App实例 */
9
- accessApp: boolean;
10
- /** 插件类型 */
11
- pluginType: "core" | "framework" | "";
12
- }
13
- /** 插件注册监听回调
14
- * 当目标插件已经注册时,回调会被立即调用,否则将在目标插件注册时调用。
15
- * 回调可以返回一个函数作为注销时的回调。
16
- */
17
- export type WhenRegisterCallback = () => (() => void) | void;
2
+ import { PluginItem, PluginConstructor, PluginRegisterOptions, PluginData, WhenRegisterCallback } from "./types.js";
18
3
  export declare class PluginManager {
19
4
  /** 插件列表 */
20
5
  private list;
21
6
  private whenRegisterMap;
22
7
  protected readonly app: App;
23
8
  constructor(app: App);
24
- registerSync<P extends PluginItem>(pluginConstructor: PluginConstructor<P>, options?: Record<string, any>, { context, core, unremovable, accessApp, pluginType }?: PluginRegisterOptions): P;
9
+ registerSync<P extends PluginItem>(pluginConstructor: PluginConstructor<P>, options?: Record<string, any>, { context, core, unremovable, accessApp, pluginType, }?: PluginRegisterOptions): P;
25
10
  /** 注册插件 */
26
- register<P extends PluginItem>(pluginConstructor: PluginConstructor<P>, options?: Record<string, any>, { context, core, unremovable, accessApp, pluginType }?: PluginRegisterOptions): Promise<P>;
11
+ register<P extends PluginItem>(pluginConstructor: PluginConstructor<P>, options?: Record<string, any>, { context, core, unremovable, accessApp, pluginType, }?: PluginRegisterOptions): Promise<P>;
27
12
  /** 移除插件 */
28
13
  unregister(pluginName: string): Promise<void>;
29
14
  /** 获取插件实例 */
@@ -1,25 +1,26 @@
1
1
  import { CommonPluginContext } from "./context/CommonPluginContext.js";
2
- import { AppError } from "../error/index.js";
2
+ import { AppErrorLegacy } from "../error/index.js";
3
3
  import { bindCommand } from "../utils/index.js";
4
4
  export class PluginManager {
5
+ /** 插件列表 */
6
+ list = new Map();
7
+ whenRegisterMap = new Map();
8
+ app;
5
9
  constructor(app) {
6
- /** 插件列表 */
7
- this.list = new Map();
8
- this.whenRegisterMap = new Map();
9
10
  this.app = app;
10
11
  app.registerCommand("plugin.snapshot", bindCommand(this.toSnapshot, this));
11
12
  }
12
- registerSync(pluginConstructor, options = {}, { context, core, unremovable, accessApp, pluginType } = {}) {
13
+ registerSync(pluginConstructor, options = {}, { context, core, unremovable, accessApp, pluginType, } = {}) {
13
14
  const pluginName = pluginConstructor.pluginName;
14
15
  if (!pluginName) {
15
- throw new AppError("plugin:register_id_missing", {
16
+ throw new AppErrorLegacy("plugin:register_id_missing", {
16
17
  message: "插件注册失败",
17
18
  cause: "插件缺少pluginName字段",
18
19
  target: "plugin/#unnamed",
19
20
  });
20
21
  }
21
22
  if (this.list.has(pluginName)) {
22
- throw new AppError("plugin:register_id_duplicate", {
23
+ throw new AppErrorLegacy("plugin:register_id_duplicate", {
23
24
  message: `插件注册失败: ${pluginName}`,
24
25
  cause: "已存在相同id的插件",
25
26
  target: `plugin/${pluginName}`,
@@ -30,7 +31,7 @@ export class PluginManager {
30
31
  try {
31
32
  const ctx = this.app.callHookSync("plugin.register", registerCtx);
32
33
  if (ctx.defaultPrevented)
33
- throw new AppError("plugin:register_hook_prevented", {
34
+ throw new AppErrorLegacy("plugin:register_hook_prevented", {
34
35
  message: `插件注册失败: ${pluginName}`,
35
36
  cause: "插件注册被钩子函数阻止",
36
37
  target: `plugin/${pluginName}`,
@@ -62,7 +63,7 @@ export class PluginManager {
62
63
  return plugin;
63
64
  }
64
65
  catch (err) {
65
- throw new AppError("plugin:register_fail", {
66
+ throw new AppErrorLegacy("plugin:register_fail", {
66
67
  message: `插件注册失败: ${pluginName}`,
67
68
  cause: err,
68
69
  target: `plugin/${pluginName}`,
@@ -70,17 +71,17 @@ export class PluginManager {
70
71
  }
71
72
  }
72
73
  /** 注册插件 */
73
- async register(pluginConstructor, options = {}, { context, core, unremovable, accessApp, pluginType } = {}) {
74
+ async register(pluginConstructor, options = {}, { context, core, unremovable, accessApp, pluginType, } = {}) {
74
75
  const pluginName = pluginConstructor.pluginName;
75
76
  if (!pluginName) {
76
- throw new AppError("plugin:register_id_missing", {
77
+ throw new AppErrorLegacy("plugin:register_id_missing", {
77
78
  message: "插件注册失败",
78
79
  cause: "插件缺少pluginName字段",
79
80
  target: "plugin/#unnamed",
80
81
  });
81
82
  }
82
83
  if (this.list.has(pluginName)) {
83
- throw new AppError("plugin:register_id_duplicate", {
84
+ throw new AppErrorLegacy("plugin:register_id_duplicate", {
84
85
  message: `插件注册失败: ${pluginName}`,
85
86
  cause: "已存在相同id的插件",
86
87
  target: `plugin/${pluginName}`,
@@ -92,7 +93,7 @@ export class PluginManager {
92
93
  .callHook("plugin.register", registerCtx)
93
94
  .then(async (ctx) => {
94
95
  if (ctx.defaultPrevented)
95
- throw new AppError("plugin:register_hook_prevented", {
96
+ throw new AppErrorLegacy("plugin:register_hook_prevented", {
96
97
  message: `插件注册失败: ${pluginName}`,
97
98
  cause: "插件注册被钩子函数阻止",
98
99
  target: `plugin/${pluginName}`,
@@ -123,7 +124,7 @@ export class PluginManager {
123
124
  return plugin;
124
125
  })
125
126
  .catch((err) => {
126
- throw new AppError("plugin:register_fail", {
127
+ throw new AppErrorLegacy("plugin:register_fail", {
127
128
  message: `插件注册失败: ${pluginName}`,
128
129
  cause: err,
129
130
  target: `plugin/${pluginName}`,
@@ -135,7 +136,7 @@ export class PluginManager {
135
136
  const pluginData = this.list.get(pluginName);
136
137
  // 检测插件是否存在
137
138
  if (!pluginData) {
138
- throw new AppError("plugin:unregister_unexist", {
139
+ throw new AppErrorLegacy("plugin:unregister_unexist", {
139
140
  message: `插件移除失败: ${pluginName}`,
140
141
  cause: "插件不存在",
141
142
  target: `plugin/${pluginName}`,
@@ -144,7 +145,7 @@ export class PluginManager {
144
145
  else {
145
146
  const { plugin, context, unremovable } = pluginData;
146
147
  if (unremovable) {
147
- throw new AppError("plugin:unregister_unremovable", {
148
+ throw new AppErrorLegacy("plugin:unregister_unremovable", {
148
149
  message: `插件移除失败: ${pluginName}`,
149
150
  cause: "不可移除的插件",
150
151
  target: `plugin/${pluginName}`,
@@ -199,7 +200,9 @@ export class PluginManager {
199
200
  // 如果目标插件已经注册,直接调用回调
200
201
  if (this.has(pluginName)) {
201
202
  const whenUnregistered = callback();
202
- this.whenRegisterMap.get(pluginName).set(callback, whenUnregistered || null);
203
+ this.whenRegisterMap
204
+ .get(pluginName)
205
+ .set(callback, whenUnregistered || null);
203
206
  }
204
207
  }
205
208
  /** 取消插件已注册时的执行回调 */