floating-live 0.16.0-alpha.2 → 0.16.0-alpha.20
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/dist/FloatingLive.d.ts +18 -0
- package/dist/FloatingLive.js +36 -1
- package/dist/app.d.ts +14 -7
- package/dist/app.js +33 -9
- package/dist/command/manager.d.ts +1 -1
- package/dist/command/manager.js +7 -5
- package/dist/command/types.d.ts +16 -0
- package/dist/error/index.d.ts +4 -2
- package/dist/error/types.d.ts +1 -0
- package/dist/event/types.d.ts +7 -0
- package/dist/hook/manager.d.ts +6 -9
- package/dist/hook/manager.js +12 -19
- package/dist/live/room.d.ts +67 -25
- package/dist/live/room.js +23 -21
- package/dist/plugin/context/CommonPluginContext.d.ts +10 -7
- package/dist/plugin/context/CommonPluginContext.js +36 -13
- package/dist/plugin/context/CorePluginContext.d.ts +8 -0
- package/dist/plugin/context/CorePluginContext.js +25 -0
- package/dist/plugin/context/SafePluginContext.d.ts +2 -2
- package/dist/plugin/index.d.ts +3 -0
- package/dist/plugin/index.js +3 -0
- package/dist/plugin/manager.d.ts +4 -10
- package/dist/plugin/manager.js +83 -21
- package/dist/plugin/types.d.ts +35 -10
- package/dist/plugins/room/index.d.ts +22 -6
- package/dist/plugins/room/index.js +65 -26
- package/dist/utils/EventEmitter.d.ts +2 -2
- package/dist/utils/EventEmitter.js +4 -4
- package/dist/value/manager.d.ts +6 -3
- package/dist/value/manager.js +15 -7
- package/package.json +1 -1
|
@@ -3,8 +3,8 @@ import type { CommandOptions, CommandCallOptions, CommandFunction, AppCommandMap
|
|
|
3
3
|
import { AppError, ErrorOptions } from "../../error";
|
|
4
4
|
import { AppEventListener, AppEventEmitOptions, AppEventDetailMap } from "../../event";
|
|
5
5
|
import { HookUseOptions, HookCallOptions, AppHookMap, HookFunction, HookContext } from "../../hook";
|
|
6
|
-
import { AppValueMap, ValueOptions } from "../../value";
|
|
7
|
-
import type { PluginContext, PluginConstructor,
|
|
6
|
+
import { AppValueMap, ValueContext, ValueOptions } from "../../value";
|
|
7
|
+
import type { PluginContext, PluginConstructor, PluginInitOptions, AppPluginExposesMap, PluginItem } from "../types";
|
|
8
8
|
export declare class CommonPluginContext implements PluginContext {
|
|
9
9
|
#private;
|
|
10
10
|
get safe(): boolean;
|
|
@@ -18,15 +18,16 @@ export declare class CommonPluginContext implements PluginContext {
|
|
|
18
18
|
*/
|
|
19
19
|
throw(err: AppError): void;
|
|
20
20
|
error(id: string, options: ErrorOptions): AppError;
|
|
21
|
-
register(plugin: PluginConstructor
|
|
21
|
+
register<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): Promise<P>;
|
|
22
22
|
unregister(pluginName: string): void;
|
|
23
|
-
getPluginExposes<
|
|
24
|
-
whenRegister
|
|
23
|
+
getPluginExposes<K extends keyof AppPluginExposesMap>(pluginName: K): AppPluginExposesMap[K];
|
|
24
|
+
whenRegister(pluginName: string, callback: (exposes: any) => (() => void) | void): void;
|
|
25
25
|
hasPlugin(pluginName: string): boolean;
|
|
26
|
-
registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>):
|
|
26
|
+
registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): ValueContext<AppValueMap[K]>;
|
|
27
27
|
unregisterValue(name: string): void;
|
|
28
28
|
registerCommand<T extends keyof AppCommandMap>(name: T, func: CommandFunction<AppCommandMap[T]>, options?: CommandOptions): void;
|
|
29
29
|
unregisterCommand(name: string): void;
|
|
30
|
+
hasCommand(name: string): boolean;
|
|
30
31
|
on<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
|
|
31
32
|
once<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
|
|
32
33
|
off<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
|
|
@@ -39,7 +40,9 @@ export declare class CommonPluginContext implements PluginContext {
|
|
|
39
40
|
callHookSync<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): HookContext<AppHookMap[T]>;
|
|
40
41
|
watch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
|
|
41
42
|
unwatch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
|
|
42
|
-
|
|
43
|
+
hasValue<K extends keyof AppValueMap>(name: K): boolean;
|
|
44
|
+
getValue<K extends keyof AppValueMap>(name: K): AppValueMap[K];
|
|
43
45
|
setValue<K extends keyof AppValueMap>(name: K, value: AppValueMap[K]): void;
|
|
44
46
|
destroy(): void;
|
|
47
|
+
get signal(): AbortSignal;
|
|
45
48
|
}
|
|
@@ -9,7 +9,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
9
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
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
11
|
};
|
|
12
|
-
var _CommonPluginContext_app, _CommonPluginContext_registered;
|
|
12
|
+
var _CommonPluginContext_app, _CommonPluginContext_abortController, _CommonPluginContext_registered;
|
|
13
13
|
import { AppError } from "../../error";
|
|
14
14
|
class ListMap extends Map {
|
|
15
15
|
addItem(key, value) {
|
|
@@ -35,6 +35,8 @@ export class CommonPluginContext {
|
|
|
35
35
|
constructor(app, pluginName) {
|
|
36
36
|
/** 主程序,插件不可访问 */
|
|
37
37
|
_CommonPluginContext_app.set(this, void 0);
|
|
38
|
+
/** AbortController,插件不可访问 */
|
|
39
|
+
_CommonPluginContext_abortController.set(this, new AbortController());
|
|
38
40
|
_CommonPluginContext_registered.set(this, {
|
|
39
41
|
plugins: new Set(),
|
|
40
42
|
values: new Set(),
|
|
@@ -58,11 +60,12 @@ export class CommonPluginContext {
|
|
|
58
60
|
}
|
|
59
61
|
register(plugin, options) {
|
|
60
62
|
try {
|
|
61
|
-
__classPrivateFieldGet(this, _CommonPluginContext_app, "f").register(plugin, options);
|
|
63
|
+
const p = __classPrivateFieldGet(this, _CommonPluginContext_app, "f").register(plugin, options);
|
|
62
64
|
__classPrivateFieldGet(this, _CommonPluginContext_registered, "f").plugins.add(plugin.name);
|
|
65
|
+
return p;
|
|
63
66
|
}
|
|
64
67
|
catch (err) {
|
|
65
|
-
this.throw(err);
|
|
68
|
+
throw this.throw(err);
|
|
66
69
|
}
|
|
67
70
|
}
|
|
68
71
|
unregister(pluginName) {
|
|
@@ -105,11 +108,12 @@ export class CommonPluginContext {
|
|
|
105
108
|
}
|
|
106
109
|
registerValue(name, options) {
|
|
107
110
|
try {
|
|
108
|
-
__classPrivateFieldGet(this, _CommonPluginContext_app, "f").registerValue(name, options);
|
|
111
|
+
const ctx = __classPrivateFieldGet(this, _CommonPluginContext_app, "f").registerValue(name, options);
|
|
109
112
|
__classPrivateFieldGet(this, _CommonPluginContext_registered, "f").values.add(name);
|
|
113
|
+
return ctx;
|
|
110
114
|
}
|
|
111
115
|
catch (err) {
|
|
112
|
-
this.throw(err);
|
|
116
|
+
throw this.throw(err);
|
|
113
117
|
}
|
|
114
118
|
}
|
|
115
119
|
unregisterValue(name) {
|
|
@@ -139,6 +143,14 @@ export class CommonPluginContext {
|
|
|
139
143
|
this.throw(err);
|
|
140
144
|
}
|
|
141
145
|
}
|
|
146
|
+
hasCommand(name) {
|
|
147
|
+
try {
|
|
148
|
+
return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").hasCommand(name);
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
throw this.throw(err);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
142
154
|
on(type, listener) {
|
|
143
155
|
try {
|
|
144
156
|
__classPrivateFieldGet(this, _CommonPluginContext_app, "f").on(type, listener);
|
|
@@ -234,7 +246,7 @@ export class CommonPluginContext {
|
|
|
234
246
|
__classPrivateFieldGet(this, _CommonPluginContext_app, "f").watch(name, watcher);
|
|
235
247
|
}
|
|
236
248
|
catch (err) {
|
|
237
|
-
this.throw(err);
|
|
249
|
+
throw this.throw(err);
|
|
238
250
|
}
|
|
239
251
|
}
|
|
240
252
|
unwatch(name, watcher) {
|
|
@@ -242,7 +254,15 @@ export class CommonPluginContext {
|
|
|
242
254
|
__classPrivateFieldGet(this, _CommonPluginContext_app, "f").unwatch(name, watcher);
|
|
243
255
|
}
|
|
244
256
|
catch (err) {
|
|
245
|
-
this.throw(err);
|
|
257
|
+
throw this.throw(err);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
hasValue(name) {
|
|
261
|
+
try {
|
|
262
|
+
return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").hasValue(name);
|
|
263
|
+
}
|
|
264
|
+
catch (err) {
|
|
265
|
+
throw this.throw(err);
|
|
246
266
|
}
|
|
247
267
|
}
|
|
248
268
|
getValue(name) {
|
|
@@ -250,24 +270,27 @@ export class CommonPluginContext {
|
|
|
250
270
|
return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").getValue(name);
|
|
251
271
|
}
|
|
252
272
|
catch (err) {
|
|
253
|
-
this.throw(err);
|
|
273
|
+
throw this.throw(err);
|
|
254
274
|
}
|
|
255
275
|
}
|
|
256
276
|
setValue(name, value) {
|
|
257
277
|
try {
|
|
258
|
-
__classPrivateFieldGet(this, _CommonPluginContext_app, "f").setValue(name, value);
|
|
278
|
+
return __classPrivateFieldGet(this, _CommonPluginContext_app, "f").setValue(name, value);
|
|
259
279
|
}
|
|
260
280
|
catch (err) {
|
|
261
|
-
this.throw(err);
|
|
281
|
+
throw this.throw(err);
|
|
262
282
|
}
|
|
263
283
|
}
|
|
264
284
|
destroy() {
|
|
265
|
-
const { plugins,
|
|
285
|
+
const { plugins, values, hooks, commands } = __classPrivateFieldGet(this, _CommonPluginContext_registered, "f");
|
|
266
286
|
plugins.forEach((pluginName) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregister(pluginName));
|
|
267
|
-
events.forEach((listenerList, eventName) => listenerList.forEach((listener) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").off(eventName, listener)));
|
|
268
287
|
hooks.forEach((hookList, name) => hookList.forEach((hookFunc) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").off(name, hookFunc)));
|
|
269
288
|
commands.forEach((name) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregisterCommand(name));
|
|
270
289
|
values.forEach((name) => __classPrivateFieldGet(this, _CommonPluginContext_app, "f").unregisterValue(name));
|
|
290
|
+
__classPrivateFieldGet(this, _CommonPluginContext_abortController, "f").abort();
|
|
291
|
+
}
|
|
292
|
+
get signal() {
|
|
293
|
+
return __classPrivateFieldGet(this, _CommonPluginContext_abortController, "f").signal;
|
|
271
294
|
}
|
|
272
295
|
}
|
|
273
|
-
_CommonPluginContext_app = new WeakMap(), _CommonPluginContext_registered = new WeakMap();
|
|
296
|
+
_CommonPluginContext_app = new WeakMap(), _CommonPluginContext_abortController = new WeakMap(), _CommonPluginContext_registered = new WeakMap();
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { App } from "../..";
|
|
2
|
+
import { CommonPluginContext } from "./CommonPluginContext";
|
|
3
|
+
/** 核心插件,具有对程序的访问权限 */
|
|
4
|
+
export declare class CorePluginContext extends CommonPluginContext {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(app: App, pluginName: string);
|
|
7
|
+
get app(): App;
|
|
8
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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 _CorePluginContext_app;
|
|
13
|
+
import { CommonPluginContext } from "./CommonPluginContext";
|
|
14
|
+
/** 核心插件,具有对程序的访问权限 */
|
|
15
|
+
export class CorePluginContext extends CommonPluginContext {
|
|
16
|
+
constructor(app, pluginName) {
|
|
17
|
+
super(app, pluginName);
|
|
18
|
+
_CorePluginContext_app.set(this, void 0);
|
|
19
|
+
__classPrivateFieldSet(this, _CorePluginContext_app, app, "f");
|
|
20
|
+
}
|
|
21
|
+
get app() {
|
|
22
|
+
return __classPrivateFieldGet(this, _CorePluginContext_app, "f");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
_CorePluginContext_app = new WeakMap();
|
|
@@ -2,7 +2,7 @@ import { App } from "../../app";
|
|
|
2
2
|
import { AppCommandMap, CommandCallOptions, CommandFunction, CommandOptions } from "../../command";
|
|
3
3
|
import { AppEventDetailMap, AppEventEmitOptions } from "../../event";
|
|
4
4
|
import { AppHookMap, HookCallOptions, HookContext, HookFunction, HookUseOptions } from "../../hook";
|
|
5
|
-
import { PluginConstructor,
|
|
5
|
+
import { PluginConstructor, PluginItem, PluginInitOptions } from "../types";
|
|
6
6
|
import { CommonPluginContext } from "./CommonPluginContext";
|
|
7
7
|
export interface PluginPermissions {
|
|
8
8
|
"plugin.register"?: boolean;
|
|
@@ -22,7 +22,7 @@ export declare class SafePluginContext extends CommonPluginContext {
|
|
|
22
22
|
permissions: PluginPermissions;
|
|
23
23
|
});
|
|
24
24
|
private throwNotPermitted;
|
|
25
|
-
register(plugin: PluginConstructor
|
|
25
|
+
register<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): Promise<P>;
|
|
26
26
|
unregister(pluginName: string): void;
|
|
27
27
|
emit<K extends keyof AppEventDetailMap>(type: K, detail: AppEventDetailMap[K], options?: AppEventEmitOptions & EventInit): void;
|
|
28
28
|
call<T extends keyof AppCommandMap>(name: T, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
|
package/dist/plugin/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { AppError } from "../error";
|
|
1
2
|
import type { PluginItem, PluginContext } from "./types";
|
|
2
3
|
export type * from "./types";
|
|
3
4
|
export declare class BasePlugin implements PluginItem {
|
|
4
5
|
static pluginName: string;
|
|
5
6
|
pluginName: string;
|
|
6
7
|
ctx: PluginContext;
|
|
8
|
+
readonly Error: typeof AppError;
|
|
9
|
+
readonly throw: (err?: Error) => void;
|
|
7
10
|
constructor(ctx: PluginContext, options?: any);
|
|
8
11
|
}
|
package/dist/plugin/index.js
CHANGED
package/dist/plugin/manager.d.ts
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
1
|
import type { App } from "../app";
|
|
2
|
-
import {
|
|
3
|
-
import { PluginItem, PluginConstructor, PluginContext } from "./types";
|
|
4
|
-
export interface PluginErrorOptions extends ErrorOptions {
|
|
5
|
-
pluginName: string;
|
|
6
|
-
}
|
|
7
|
-
export declare class PluginError extends AppError {
|
|
8
|
-
pluginName: string;
|
|
9
|
-
constructor(id: string, options: PluginErrorOptions);
|
|
10
|
-
}
|
|
2
|
+
import { PluginItem, PluginConstructor, PluginContext, PluginRegisterOptions } from "./types";
|
|
11
3
|
export interface PluginData<T> {
|
|
12
4
|
plugin: PluginItem;
|
|
13
5
|
context: PluginContext;
|
|
14
6
|
exposes?: T;
|
|
7
|
+
core?: boolean;
|
|
15
8
|
}
|
|
16
9
|
export declare class PluginManager {
|
|
17
10
|
/** 插件列表 */
|
|
18
11
|
private list;
|
|
19
12
|
protected readonly app: App;
|
|
20
13
|
constructor(app: App);
|
|
14
|
+
registerSync<P extends PluginItem>(pluginConstructor: PluginConstructor<P>, options?: Record<string, any>, { context, core }?: PluginRegisterOptions): P;
|
|
21
15
|
/** 注册插件 */
|
|
22
|
-
register(pluginConstructor: PluginConstructor
|
|
16
|
+
register<P extends PluginItem>(pluginConstructor: PluginConstructor<P>, options?: Record<string, any>, { context, core }?: PluginRegisterOptions): Promise<P>;
|
|
23
17
|
/** 移除插件 */
|
|
24
18
|
unregister(pluginName: string): Promise<void>;
|
|
25
19
|
/** 获取插件实例 */
|
package/dist/plugin/manager.js
CHANGED
|
@@ -1,54 +1,109 @@
|
|
|
1
1
|
import { CommonPluginContext } from "./context/CommonPluginContext";
|
|
2
2
|
import { AppError } from "../error";
|
|
3
|
-
|
|
4
|
-
constructor(id, options) {
|
|
5
|
-
super(id, options);
|
|
6
|
-
this.pluginName = options.pluginName;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
3
|
+
import { CorePluginContext } from "./context/CorePluginContext";
|
|
9
4
|
export class PluginManager {
|
|
10
5
|
constructor(app) {
|
|
11
6
|
/** 插件列表 */
|
|
12
7
|
this.list = new Map();
|
|
13
8
|
this.app = app;
|
|
14
9
|
}
|
|
10
|
+
registerSync(pluginConstructor, options = {}, { context, core } = {}) {
|
|
11
|
+
const pluginName = pluginConstructor.pluginName;
|
|
12
|
+
if (!pluginName) {
|
|
13
|
+
throw new AppError("plugin:register_id_missing", {
|
|
14
|
+
message: "插件注册失败",
|
|
15
|
+
cause: "插件缺少pluginName字段",
|
|
16
|
+
target: "plugin/#unnamed",
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
if (this.list.has(pluginName)) {
|
|
20
|
+
throw new AppError("plugin:register_id_duplicate", {
|
|
21
|
+
message: `插件注册失败: ${pluginName}`,
|
|
22
|
+
cause: "已存在相同id的插件",
|
|
23
|
+
target: `plugin/${pluginName}`,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
const registerCtx = { pluginName, options };
|
|
27
|
+
const pluginCtx = context || new CorePluginContext(this.app, pluginName);
|
|
28
|
+
try {
|
|
29
|
+
const ctx = this.app.callHookSync("plugin.register", registerCtx);
|
|
30
|
+
if (ctx.defaultPrevented)
|
|
31
|
+
throw new AppError("plugin:register_hook_prevented", {
|
|
32
|
+
message: `插件注册失败: ${pluginName}`,
|
|
33
|
+
cause: "插件注册被钩子函数阻止",
|
|
34
|
+
target: `plugin/${pluginName}`,
|
|
35
|
+
});
|
|
36
|
+
// 执行插件函数
|
|
37
|
+
const plugin = new pluginConstructor(pluginCtx, registerCtx.options || {});
|
|
38
|
+
// 调用插件的init钩子
|
|
39
|
+
plugin.init?.(pluginCtx, registerCtx.options || {});
|
|
40
|
+
// 调用插件的expose钩子
|
|
41
|
+
const exposes = plugin.expose?.(pluginCtx);
|
|
42
|
+
this.list.set(pluginName, {
|
|
43
|
+
plugin,
|
|
44
|
+
context: pluginCtx,
|
|
45
|
+
exposes,
|
|
46
|
+
core,
|
|
47
|
+
});
|
|
48
|
+
this.app.emit("plugin:register", { pluginName });
|
|
49
|
+
return plugin;
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
throw new AppError("plugin:register_fail", {
|
|
53
|
+
message: `插件注册失败: ${pluginName}`,
|
|
54
|
+
cause: err,
|
|
55
|
+
target: `plugin/${pluginName}`,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
15
59
|
/** 注册插件 */
|
|
16
|
-
async register(pluginConstructor, options = {}) {
|
|
60
|
+
async register(pluginConstructor, options = {}, { context, core } = {}) {
|
|
17
61
|
const pluginName = pluginConstructor.pluginName;
|
|
18
62
|
if (!pluginName) {
|
|
19
|
-
throw new
|
|
63
|
+
throw new AppError("plugin:register_id_missing", {
|
|
20
64
|
message: "插件注册失败",
|
|
21
65
|
cause: "插件缺少pluginName字段",
|
|
22
|
-
|
|
66
|
+
target: "plugin/#unnamed",
|
|
23
67
|
});
|
|
24
68
|
}
|
|
25
69
|
if (this.list.has(pluginName)) {
|
|
26
|
-
throw new
|
|
70
|
+
throw new AppError("plugin:register_id_duplicate", {
|
|
27
71
|
message: `插件注册失败: ${pluginName}`,
|
|
28
72
|
cause: "已存在相同id的插件",
|
|
29
|
-
pluginName
|
|
73
|
+
target: `plugin/${pluginName}`,
|
|
30
74
|
});
|
|
31
75
|
}
|
|
32
76
|
const registerCtx = { pluginName, options };
|
|
33
|
-
const pluginCtx = new CommonPluginContext(this.app, pluginName);
|
|
77
|
+
const pluginCtx = context || new CommonPluginContext(this.app, pluginName);
|
|
34
78
|
return await this.app
|
|
35
79
|
.callHook("plugin.register", registerCtx)
|
|
36
80
|
.then(async (ctx) => {
|
|
37
81
|
if (ctx.defaultPrevented)
|
|
38
|
-
|
|
82
|
+
throw new AppError("plugin:register_hook_prevented", {
|
|
83
|
+
message: `插件注册失败: ${pluginName}`,
|
|
84
|
+
cause: "插件注册被钩子函数阻止",
|
|
85
|
+
target: `plugin/${pluginName}`,
|
|
86
|
+
});
|
|
39
87
|
// 执行插件函数
|
|
40
88
|
const plugin = new pluginConstructor(pluginCtx, registerCtx.options || {});
|
|
41
|
-
// 调用插件的
|
|
42
|
-
await plugin.
|
|
43
|
-
|
|
89
|
+
// 调用插件的init钩子
|
|
90
|
+
await plugin.init?.(pluginCtx, registerCtx.options || {});
|
|
91
|
+
// 调用插件的expose钩子
|
|
92
|
+
const exposes = await plugin.expose?.(pluginCtx);
|
|
93
|
+
this.list.set(pluginName, {
|
|
94
|
+
plugin,
|
|
95
|
+
context: pluginCtx,
|
|
96
|
+
exposes,
|
|
97
|
+
core,
|
|
98
|
+
});
|
|
44
99
|
this.app.emit("plugin:register", { pluginName });
|
|
45
100
|
return plugin;
|
|
46
101
|
})
|
|
47
102
|
.catch((err) => {
|
|
48
|
-
throw new
|
|
103
|
+
throw new AppError("plugin:register_fail", {
|
|
49
104
|
message: `插件注册失败: ${pluginName}`,
|
|
50
105
|
cause: err,
|
|
51
|
-
pluginName
|
|
106
|
+
target: `plugin/${pluginName}`,
|
|
52
107
|
});
|
|
53
108
|
});
|
|
54
109
|
}
|
|
@@ -57,14 +112,21 @@ export class PluginManager {
|
|
|
57
112
|
const pluginData = this.list.get(pluginName);
|
|
58
113
|
// 检测插件是否存在
|
|
59
114
|
if (!pluginData) {
|
|
60
|
-
throw new
|
|
115
|
+
throw new AppError("plugin:unregister_unexist", {
|
|
61
116
|
message: `插件移除失败: ${pluginName}`,
|
|
62
117
|
cause: "插件不存在",
|
|
63
|
-
pluginName
|
|
118
|
+
target: `plugin/${pluginName}`,
|
|
64
119
|
});
|
|
65
120
|
}
|
|
66
121
|
else {
|
|
67
|
-
const { plugin, context } = pluginData;
|
|
122
|
+
const { plugin, context, core } = pluginData;
|
|
123
|
+
if (core) {
|
|
124
|
+
throw new AppError("plugin:unregister_core", {
|
|
125
|
+
message: `插件移除失败: ${pluginName}`,
|
|
126
|
+
cause: "无法移除核心插件",
|
|
127
|
+
target: `plugin/${pluginName}`,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
68
130
|
// 调用插件和上下文的destroy钩子
|
|
69
131
|
plugin.destroy?.(context);
|
|
70
132
|
context.destroy();
|
package/dist/plugin/types.d.ts
CHANGED
|
@@ -1,24 +1,40 @@
|
|
|
1
|
+
import { App } from "../app";
|
|
1
2
|
import { CommandOptions, CommandCallOptions, AppCommandMap, CommandFunction } from "../command";
|
|
2
3
|
import { AppError, ErrorOptions } from "../error";
|
|
3
4
|
import { AppEventListener, AppEventEmitOptions, AppEventDetailMap } from "../event";
|
|
4
5
|
import { AppHookMap, HookCallOptions, HookContext, HookFunction, HookUseOptions } from "../hook";
|
|
5
|
-
import { AppValueMap, ValueOptions } from "../value";
|
|
6
|
+
import { AppValueMap, ValueContext, ValueOptions } from "../value";
|
|
6
7
|
/** 插件对象 */
|
|
7
8
|
export interface PluginItem {
|
|
8
9
|
pluginName: string;
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
/** 初始化插件 */
|
|
11
|
+
init?(ctx: PluginContext, options?: Record<string, any>): void | Promise<void>;
|
|
12
|
+
/** 插件销毁 */
|
|
13
|
+
destroy?(ctx: PluginContext): void | Promise<void>;
|
|
14
|
+
/** 插件暴露 */
|
|
15
|
+
expose?(ctx: PluginContext): any;
|
|
11
16
|
}
|
|
12
17
|
/** 插件构造器 */
|
|
13
|
-
export interface PluginConstructor {
|
|
18
|
+
export interface PluginConstructor<P extends PluginItem> {
|
|
14
19
|
pluginName: string;
|
|
15
|
-
new (ctx: PluginContext, options: any):
|
|
20
|
+
new (ctx: PluginContext, options: any): P;
|
|
21
|
+
}
|
|
22
|
+
/** 插件初始化选项 */
|
|
23
|
+
export interface PluginInitOptions {
|
|
16
24
|
}
|
|
17
25
|
/** 插件注册选项 */
|
|
18
26
|
export interface PluginRegisterOptions {
|
|
27
|
+
/** 提供的插件上下文 */
|
|
28
|
+
context?: PluginContext;
|
|
29
|
+
/** 核心插件,不可解除注册 */
|
|
30
|
+
core?: boolean;
|
|
19
31
|
}
|
|
20
32
|
/** 插件上下文 */
|
|
21
33
|
export interface PluginContext {
|
|
34
|
+
/** 安全插件上下文 */
|
|
35
|
+
safe?: boolean;
|
|
36
|
+
/** App实例,仅在核心插件上下文提供 */
|
|
37
|
+
app?: App;
|
|
22
38
|
/** 监听事件 */
|
|
23
39
|
on<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
|
|
24
40
|
/** 单次监听事件 */
|
|
@@ -29,22 +45,25 @@ export interface PluginContext {
|
|
|
29
45
|
emit<K extends keyof AppEventDetailMap>(type: K, detail: AppEventDetailMap[K], options?: AppEventEmitOptions & EventInit): void;
|
|
30
46
|
/** 抛出错误 */
|
|
31
47
|
throw(err?: Error): void;
|
|
32
|
-
/** 生成错误 */
|
|
48
|
+
/** 生成错误 @deprecated */
|
|
33
49
|
error(id: string, options: ErrorOptions): AppError;
|
|
34
50
|
/** 注册插件 */
|
|
35
|
-
register(plugin: PluginConstructor
|
|
51
|
+
register<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): Promise<P>;
|
|
36
52
|
/** 取消注册插件 */
|
|
37
53
|
unregister(pluginName: string): void;
|
|
38
54
|
/** 获取插件暴露对象 */
|
|
39
|
-
getPluginExposes<
|
|
55
|
+
getPluginExposes<K extends keyof AppPluginExposesMap>(pluginName: K): AppPluginExposesMap[K];
|
|
40
56
|
/** 等待plugin注册 */
|
|
41
|
-
whenRegister(pluginName:
|
|
57
|
+
whenRegister<K extends keyof AppPluginExposesMap>(pluginName: K, callback: (exposes: AppPluginExposesMap[K]) => (() => void) | void): void;
|
|
58
|
+
whenRegister(pluginName: string, callback: () => (() => void) | void): void;
|
|
42
59
|
/** 是否存在插件 */
|
|
43
60
|
hasPlugin(pluginName: string): boolean;
|
|
44
61
|
/** 注册指令 */
|
|
45
62
|
registerCommand<T extends keyof AppCommandMap>(name: T, func: CommandFunction<AppCommandMap[T]>, options?: CommandOptions): void;
|
|
46
63
|
/** 取消注册指令 */
|
|
47
64
|
unregisterCommand(name: string): void;
|
|
65
|
+
/** 检测值是否存在 */
|
|
66
|
+
hasCommand(name: string): boolean;
|
|
48
67
|
/** 调用指令 */
|
|
49
68
|
call<T extends keyof AppCommandMap>(name: T, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
|
|
50
69
|
/** 调用指令(可配置) */
|
|
@@ -58,16 +77,22 @@ export interface PluginContext {
|
|
|
58
77
|
/** 调用同步钩子 */
|
|
59
78
|
callHookSync<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): HookContext<AppHookMap[T]>;
|
|
60
79
|
/** 注册值 */
|
|
61
|
-
registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>):
|
|
80
|
+
registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): ValueContext<AppValueMap[K]>;
|
|
62
81
|
/** 取消注册值 */
|
|
63
82
|
unregisterValue(name: string): void;
|
|
64
83
|
/** 监听值 */
|
|
65
84
|
watch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
|
|
66
85
|
/** 取消监听值 */
|
|
67
86
|
unwatch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
|
|
87
|
+
/** 值是否存在 */
|
|
88
|
+
hasValue(name: string): boolean;
|
|
68
89
|
/** 获取值 */
|
|
69
90
|
getValue<K extends keyof AppValueMap>(name: K): AppValueMap[K];
|
|
70
91
|
/** 设置值 */
|
|
71
92
|
setValue<K extends keyof AppValueMap>(name: K, value: AppValueMap[K]): void;
|
|
72
93
|
destroy(): void;
|
|
94
|
+
get signal(): AbortSignal;
|
|
95
|
+
}
|
|
96
|
+
export interface AppPluginExposesMap {
|
|
97
|
+
"": {};
|
|
73
98
|
}
|
|
@@ -2,6 +2,17 @@ import { LiveMessage } from "../../live/message";
|
|
|
2
2
|
import { LiveRoom, LiveRoomDetailInfo, LiveRoomData, LiveRoomStatsInfo, LiveRoomStatus } from "../../live/room";
|
|
3
3
|
import { BasePlugin } from "../../plugin";
|
|
4
4
|
import { PluginContext } from "../../plugin/types";
|
|
5
|
+
interface PluginExposes {
|
|
6
|
+
add(platform: string, id: number, options?: boolean | Record<string, any>): void;
|
|
7
|
+
remove(key: string): void;
|
|
8
|
+
get(key: string): LiveRoom | undefined;
|
|
9
|
+
has(key: string): boolean;
|
|
10
|
+
data(key: string): LiveRoomData | undefined;
|
|
11
|
+
update(key: string): void;
|
|
12
|
+
open(key: string): void;
|
|
13
|
+
close(key: string): void;
|
|
14
|
+
getList(): LiveRoom[];
|
|
15
|
+
}
|
|
5
16
|
declare module "../.." {
|
|
6
17
|
interface AppCommandMap {
|
|
7
18
|
add: (platform: string, id: number, options?: boolean | Record<string, any>) => void;
|
|
@@ -12,6 +23,7 @@ declare module "../.." {
|
|
|
12
23
|
update: (key: string) => void;
|
|
13
24
|
"room.snapshot": () => LiveRoomData[];
|
|
14
25
|
[name: `${string}.room.create`]: (id: string | number, options: Record<string, any>) => LiveRoom | Promise<LiveRoom>;
|
|
26
|
+
[name: `${string}.room.data`]: (id: string | number) => LiveRoomData | Promise<LiveRoomData>;
|
|
15
27
|
}
|
|
16
28
|
interface AppEventDetailMap {
|
|
17
29
|
"live:message": {
|
|
@@ -82,14 +94,18 @@ declare module "../.." {
|
|
|
82
94
|
options: Record<string, any>;
|
|
83
95
|
};
|
|
84
96
|
}
|
|
97
|
+
interface AppPluginExposesMap {
|
|
98
|
+
room: PluginExposes;
|
|
99
|
+
}
|
|
85
100
|
}
|
|
86
101
|
export declare class Room extends BasePlugin {
|
|
87
102
|
static pluginName: string;
|
|
88
103
|
private map;
|
|
89
|
-
private
|
|
90
|
-
|
|
104
|
+
private symbolAbortController;
|
|
105
|
+
init(ctx: PluginContext): void;
|
|
106
|
+
expose(): PluginExposes;
|
|
91
107
|
/** 添加房间监听实例 */
|
|
92
|
-
|
|
108
|
+
attach(room: LiveRoom, open?: boolean): void;
|
|
93
109
|
/** 添加房间 */
|
|
94
110
|
add(platform: string, id: number, options?: boolean | Record<string, any>): Promise<void>;
|
|
95
111
|
/** 移除房间 */
|
|
@@ -98,13 +114,13 @@ export declare class Room extends BasePlugin {
|
|
|
98
114
|
get(key: string): LiveRoom | undefined;
|
|
99
115
|
has(key: string): boolean;
|
|
100
116
|
/** 获取房间信息 */
|
|
101
|
-
|
|
117
|
+
data(key: string): LiveRoomData | undefined;
|
|
102
118
|
/** 更新房间信息 */
|
|
103
119
|
update(key: string): void;
|
|
104
120
|
/** 获取房间key列表 */
|
|
105
|
-
get keys():
|
|
121
|
+
get keys(): string[];
|
|
106
122
|
/** 获取房间列表 */
|
|
107
|
-
getList():
|
|
123
|
+
getList(): LiveRoom[];
|
|
108
124
|
/** 打开房间连接 */
|
|
109
125
|
open(key: string): void;
|
|
110
126
|
/** 关闭房间连接 */
|