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