floating-live 0.16.0-alpha.10 → 0.16.0-alpha.12

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,6 +1,24 @@
1
+ import { LiveRoom } from ".";
1
2
  import { App } from "./app";
2
3
  import { Room } from "./plugins/room";
3
4
  export declare class FloatingLive extends App {
4
5
  room: Room;
5
6
  constructor();
7
+ /** 添加房间 */
8
+ add(platform: string, id: number, options?: boolean | Record<string, any>): Promise<void>;
9
+ /** 移除房间 */
10
+ remove(key: string): void;
11
+ /** 获取房间 */
12
+ get(key: string): LiveRoom | undefined;
13
+ has(key: string): boolean;
14
+ /** 获取房间信息 */
15
+ data(key: string): import("./live").LiveRoomData | undefined;
16
+ /** 更新房间信息 */
17
+ update(key: string): void;
18
+ /** 打开房间连接 */
19
+ open(key: string): void;
20
+ /** 关闭房间连接 */
21
+ close(key: string): void;
22
+ /** 获取房间列表 */
23
+ getList(): LiveRoom[];
6
24
  }
@@ -3,6 +3,41 @@ import { Room } from "./plugins/room";
3
3
  export class FloatingLive extends App {
4
4
  constructor() {
5
5
  super();
6
- this.room = new Room(this);
6
+ this.room = this.registerSync(Room, { core: true });
7
+ }
8
+ /** 添加房间 */
9
+ add(platform, id, options) {
10
+ return this.room.add(platform, id, options);
11
+ }
12
+ /** 移除房间 */
13
+ remove(key) {
14
+ return this.room.remove(key);
15
+ }
16
+ /** 获取房间 */
17
+ get(key) {
18
+ return this.room.get(key);
19
+ }
20
+ has(key) {
21
+ return this.room.has(key);
22
+ }
23
+ /** 获取房间信息 */
24
+ data(key) {
25
+ return this.room.data(key);
26
+ }
27
+ /** 更新房间信息 */
28
+ update(key) {
29
+ return this.room.update(key);
30
+ }
31
+ /** 打开房间连接 */
32
+ open(key) {
33
+ return this.room.open(key);
34
+ }
35
+ /** 关闭房间连接 */
36
+ close(key) {
37
+ return this.room.close(key);
38
+ }
39
+ /** 获取房间列表 */
40
+ getList() {
41
+ return this.room.getList();
7
42
  }
8
43
  }
package/dist/app.d.ts CHANGED
@@ -2,7 +2,7 @@ import { CommandOptions, CommandCallOptions, AppCommandMap, CommandFunction } fr
2
2
  import { AppError } from "./error";
3
3
  import { AppEventDetailMap, AppEventEmitOptions, AppEventListener } from "./event";
4
4
  import { AppHookMap, HookCallOptions, HookContext, HookFunction, HookUseOptions } from "./hook";
5
- import { AppPluginExposesMap, PluginContext, PluginInitOptions } from "./plugin";
5
+ import { AppPluginExposesMap, PluginConstructor, PluginContext, PluginInitOptions, PluginItem } from "./plugin";
6
6
  import { CustomEventEmitter } from "./utils/EventEmitter";
7
7
  import { AppValueMap, ValueOptions } from "./value";
8
8
  export declare class App extends CustomEventEmitter implements PluginContext {
@@ -20,7 +20,9 @@ export declare class App extends CustomEventEmitter implements PluginContext {
20
20
  throw(err?: Error): void;
21
21
  error(id: string, detail?: Record<string, any>): AppError;
22
22
  /** 注册插件 */
23
- register(plugin: any, options?: PluginInitOptions): void;
23
+ register<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): Promise<P>;
24
+ /** 同步注册插件 */
25
+ registerSync<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): P;
24
26
  /** 取消注册插件 */
25
27
  unregister(pluginName: string): void;
26
28
  getPlugin(pluginName: string): void;
@@ -37,7 +39,7 @@ export declare class App extends CustomEventEmitter implements PluginContext {
37
39
  unuseHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>): void;
38
40
  callHook<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): Promise<HookContext<AppHookMap[T]>>;
39
41
  callHookSync<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): HookContext<AppHookMap[T]>;
40
- registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): void;
42
+ registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): import("./value").ValueContext<AppValueMap[K]>;
41
43
  unregisterValue(name: string): void;
42
44
  watch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
43
45
  unwatch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
@@ -46,6 +48,6 @@ export declare class App extends CustomEventEmitter implements PluginContext {
46
48
  setValue(name: string, value: any): void;
47
49
  destroy(): void;
48
50
  /** 拓展值 */
49
- define(): void;
51
+ define(key: string, value: ((...args: any[]) => any) | PropertyDescriptor): void;
50
52
  get signal(): AbortSignal;
51
53
  }
package/dist/app.js CHANGED
@@ -37,7 +37,11 @@ export class App extends CustomEventEmitter {
37
37
  //--- 插件机制 ---//
38
38
  /** 注册插件 */
39
39
  register(plugin, options) {
40
- this.pluginManager.register(plugin, options);
40
+ return this.pluginManager.register(plugin, options);
41
+ }
42
+ /** 同步注册插件 */
43
+ registerSync(plugin, options) {
44
+ return this.pluginManager.registerSync(plugin, options);
41
45
  }
42
46
  /** 取消注册插件 */
43
47
  unregister(pluginName) {
@@ -106,7 +110,7 @@ export class App extends CustomEventEmitter {
106
110
  }
107
111
  //--- 值机制 ---//
108
112
  registerValue(name, options) {
109
- this.valueManager.register(name, options);
113
+ return this.valueManager.register(name, options);
110
114
  }
111
115
  unregisterValue(name) {
112
116
  this.valueManager.unregister(name);
@@ -131,7 +135,14 @@ export class App extends CustomEventEmitter {
131
135
  }
132
136
  //--- 其他 ---//
133
137
  /** 拓展值 */
134
- define() { }
138
+ define(key, value) {
139
+ if (typeof value == "function") {
140
+ Object.defineProperty(this, key, { value });
141
+ }
142
+ else {
143
+ Object.defineProperty(this, key, value);
144
+ }
145
+ }
135
146
  get signal() {
136
147
  return this.abortController.signal;
137
148
  }
@@ -2,6 +2,7 @@ import { CustomEventEmitter } from "../utils/EventEmitter";
2
2
  import { EnumValue } from "../utils/types";
3
3
  import { LiveMessage } from "./message";
4
4
  import { UserInfo } from "./messageInfo";
5
+ /** 直播间客户端API */
5
6
  export declare abstract class LiveRoom extends CustomEventEmitter implements LiveRoomData {
6
7
  constructor();
7
8
  /** 平台 */
@@ -30,20 +31,24 @@ export declare abstract class LiveRoom extends CustomEventEmitter implements Liv
30
31
  abstract open(): void;
31
32
  /** 关闭连接 */
32
33
  abstract close(): void;
33
- /** 从服务器获取信息 */
34
- abstract getInfo(): Promise<void>;
35
- /** 销毁事件 */
36
- protected onDestroy?(): void;
34
+ /** 更新房间数据 */
35
+ abstract update(): Promise<void>;
36
+ /** 从服务器获取房间数据 */
37
+ abstract fetchData?(): Promise<void>;
37
38
  /** 销毁 */
38
39
  destroy(): void;
40
+ /** 销毁事件 */
41
+ protected onDestroy?: () => void;
39
42
  protected emitMessage(msg: LiveMessage.All): void;
40
43
  /** 发送源消息 */
41
44
  protected emitRaw(msg: any): void;
42
45
  /** 获取数据 */
43
46
  toData(): LiveRoomData;
44
47
  get key(): string;
45
- on<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void): void;
46
- once<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void): void;
48
+ }
49
+ export interface LiveRoom {
50
+ on<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void, signal?: AbortSignal): void;
51
+ once<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void, signal?: AbortSignal): void;
47
52
  off<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void): void;
48
53
  emit<K extends keyof LiveRoomEventMap>(type: LiveRoomEventMap[K] extends Record<string, never> ? K : never): void;
49
54
  emit<K extends keyof LiveRoomEventMap>(type: K, detail: LiveRoomEventMap[K]): void;
@@ -124,33 +129,50 @@ export declare const LiveConnectionStatus: {
124
129
  };
125
130
  export type LiveConnectionStatus = EnumValue<typeof LiveConnectionStatus>;
126
131
  export interface LiveRoomEventMap {
132
+ /** */
127
133
  status: {
128
134
  status: LiveRoomStatus;
129
135
  timestamp: number;
130
136
  liveId?: string | number;
131
137
  };
138
+ /** 房间数据更新 */
132
139
  update: {
133
140
  room: LiveRoomData;
134
141
  };
142
+ /** 细节改变 */
135
143
  detail: {
136
144
  detail: Partial<LiveRoomDetailInfo>;
137
145
  };
146
+ /** 统计信息改变 */
138
147
  stats: {
139
148
  stats: Partial<LiveRoomStatsInfo>;
140
149
  };
150
+ /** 消息 */
141
151
  message: {
142
152
  message: LiveMessage.All;
143
153
  };
154
+ /** 消息源数据 */
144
155
  raw: {
145
156
  platform: string;
146
157
  roomId: string | number;
147
158
  data: any;
148
159
  };
160
+ /** 初始化 */
149
161
  init: {};
162
+ /** 正在连接 */
150
163
  connecting: {};
164
+ /** 连接到服务器 */
151
165
  connected: {};
166
+ /** 断开连接 */
152
167
  disconnect: {};
168
+ /** 连接到房间 */
153
169
  enter: {};
170
+ /** 连接超时 */
171
+ timeout: {
172
+ time: number;
173
+ };
174
+ /** 开启直播间连接 */
154
175
  open: {};
176
+ /** 关闭直播间连接 */
155
177
  close: {};
156
178
  }
package/dist/live/room.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CustomEventEmitter } from "../utils/EventEmitter";
2
+ /** 直播间客户端API */
2
3
  export class LiveRoom extends CustomEventEmitter {
3
4
  constructor() {
4
5
  super();
@@ -76,19 +77,6 @@ export class LiveRoom extends CustomEventEmitter {
76
77
  get key() {
77
78
  return `${this.platform}:${this.id}`;
78
79
  }
79
- on(type, listener) {
80
- super.on(type, listener);
81
- }
82
- once(type, listener) {
83
- super.once(type, listener);
84
- this.emit("connecting");
85
- }
86
- off(type, listener) {
87
- super.off(type, listener);
88
- }
89
- emit(type, detail) {
90
- super.emit(type, detail);
91
- }
92
80
  }
93
81
  /** 直播间状态 */
94
82
  export const LiveRoomStatus = {
@@ -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,12 +18,12 @@ 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, options?: PluginInitOptions): void;
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]>): void;
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;
@@ -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) {
@@ -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, options?: PluginInitOptions): void;
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]>;
@@ -1,6 +1,6 @@
1
1
  import type { App } from "../app";
2
2
  import { AppError, ErrorOptions } from "../error";
3
- import { PluginItem, PluginConstructor, PluginContext } from "./types";
3
+ import { PluginItem, PluginConstructor, PluginContext, PluginRegisterOptions } from "./types";
4
4
  export interface PluginErrorOptions extends ErrorOptions {
5
5
  pluginName: string;
6
6
  }
@@ -12,14 +12,16 @@ export interface PluginData<T> {
12
12
  plugin: PluginItem;
13
13
  context: PluginContext;
14
14
  exposes?: T;
15
+ core?: boolean;
15
16
  }
16
17
  export declare class PluginManager {
17
18
  /** 插件列表 */
18
19
  private list;
19
20
  protected readonly app: App;
20
21
  constructor(app: App);
22
+ registerSync<P extends PluginItem>(pluginConstructor: PluginConstructor<P>, options?: Record<string, any>, { context, core }?: PluginRegisterOptions): P;
21
23
  /** 注册插件 */
22
- register(pluginConstructor: PluginConstructor, options?: Record<string, any>): Promise<PluginItem | undefined>;
24
+ register<P extends PluginItem>(pluginConstructor: PluginConstructor<P>, options?: Record<string, any>, { context, core }?: PluginRegisterOptions): Promise<P>;
23
25
  /** 移除插件 */
24
26
  unregister(pluginName: string): Promise<void>;
25
27
  /** 获取插件实例 */
@@ -1,5 +1,6 @@
1
1
  import { CommonPluginContext } from "./context/CommonPluginContext";
2
2
  import { AppError } from "../error";
3
+ import { CorePluginContext } from "./context/CorePluginContext";
3
4
  export class PluginError extends AppError {
4
5
  constructor(id, options) {
5
6
  super(id, options);
@@ -12,8 +13,57 @@ export class PluginManager {
12
13
  this.list = new Map();
13
14
  this.app = app;
14
15
  }
16
+ registerSync(pluginConstructor, options = {}, { context, core } = {}) {
17
+ const pluginName = pluginConstructor.pluginName;
18
+ if (!pluginName) {
19
+ throw new PluginError("plugin:register_id_missing", {
20
+ message: "插件注册失败",
21
+ cause: "插件缺少pluginName字段",
22
+ pluginName: "",
23
+ });
24
+ }
25
+ if (this.list.has(pluginName)) {
26
+ throw new PluginError("plugin:register_id_duplicate", {
27
+ message: `插件注册失败: ${pluginName}`,
28
+ cause: "已存在相同id的插件",
29
+ pluginName,
30
+ });
31
+ }
32
+ const registerCtx = { pluginName, options };
33
+ const pluginCtx = context || new CorePluginContext(this.app, pluginName);
34
+ try {
35
+ const ctx = this.app.callHookSync("plugin.register", registerCtx);
36
+ if (ctx.defaultPrevented)
37
+ throw new PluginError("plugin:register_hook_prevented", {
38
+ message: `插件注册失败: ${pluginName}`,
39
+ cause: "插件注册被钩子函数阻止",
40
+ pluginName,
41
+ });
42
+ // 执行插件函数
43
+ const plugin = new pluginConstructor(pluginCtx, registerCtx.options || {});
44
+ // 调用插件的init钩子
45
+ plugin.init?.(pluginCtx, registerCtx.options || {});
46
+ // 调用插件的expose钩子
47
+ const exposes = plugin.expose?.(pluginCtx);
48
+ this.list.set(pluginName, {
49
+ plugin,
50
+ context: pluginCtx,
51
+ exposes,
52
+ core,
53
+ });
54
+ this.app.emit("plugin:register", { pluginName });
55
+ return plugin;
56
+ }
57
+ catch (err) {
58
+ throw new PluginError("plugin:register_fail", {
59
+ message: `插件注册失败: ${pluginName}`,
60
+ cause: err,
61
+ pluginName,
62
+ });
63
+ }
64
+ }
15
65
  /** 注册插件 */
16
- async register(pluginConstructor, options = {}) {
66
+ async register(pluginConstructor, options = {}, { context, core } = {}) {
17
67
  const pluginName = pluginConstructor.pluginName;
18
68
  if (!pluginName) {
19
69
  throw new PluginError("plugin:register_id_missing", {
@@ -30,19 +80,28 @@ export class PluginManager {
30
80
  });
31
81
  }
32
82
  const registerCtx = { pluginName, options };
33
- const pluginCtx = new CommonPluginContext(this.app, pluginName);
83
+ const pluginCtx = context || new CommonPluginContext(this.app, pluginName);
34
84
  return await this.app
35
85
  .callHook("plugin.register", registerCtx)
36
86
  .then(async (ctx) => {
37
87
  if (ctx.defaultPrevented)
38
- return;
88
+ throw new PluginError("plugin:register_hook_prevented", {
89
+ message: `插件注册失败: ${pluginName}`,
90
+ cause: "插件注册被钩子函数阻止",
91
+ pluginName,
92
+ });
39
93
  // 执行插件函数
40
94
  const plugin = new pluginConstructor(pluginCtx, registerCtx.options || {});
41
95
  // 调用插件的init钩子
42
96
  await plugin.init?.(pluginCtx, registerCtx.options || {});
43
97
  // 调用插件的expose钩子
44
98
  const exposes = await plugin.expose?.(pluginCtx);
45
- this.list.set(pluginName, { plugin, context: pluginCtx, exposes });
99
+ this.list.set(pluginName, {
100
+ plugin,
101
+ context: pluginCtx,
102
+ exposes,
103
+ core,
104
+ });
46
105
  this.app.emit("plugin:register", { pluginName });
47
106
  return plugin;
48
107
  })
@@ -66,7 +125,14 @@ export class PluginManager {
66
125
  });
67
126
  }
68
127
  else {
69
- const { plugin, context } = pluginData;
128
+ const { plugin, context, core } = pluginData;
129
+ if (core) {
130
+ throw new PluginError("plugin:unregister_core", {
131
+ message: `插件移除失败: ${pluginName}`,
132
+ cause: "无法移除核心插件",
133
+ pluginName,
134
+ });
135
+ }
70
136
  // 调用插件和上下文的destroy钩子
71
137
  plugin.destroy?.(context);
72
138
  context.destroy();
@@ -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): PluginItem;
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
  /** 单次监听事件 */
@@ -33,7 +48,7 @@ export interface PluginContext {
33
48
  /** 生成错误 */
34
49
  error(id: string, options: ErrorOptions): AppError;
35
50
  /** 注册插件 */
36
- register(plugin: PluginConstructor, options?: PluginInitOptions): void;
51
+ register<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): Promise<P>;
37
52
  /** 取消注册插件 */
38
53
  unregister(pluginName: string): void;
39
54
  /** 获取插件暴露对象 */
@@ -62,7 +77,7 @@ export interface PluginContext {
62
77
  /** 调用同步钩子 */
63
78
  callHookSync<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): HookContext<AppHookMap[T]>;
64
79
  /** 注册值 */
65
- registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): void;
80
+ registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): ValueContext<AppValueMap[K]>;
66
81
  /** 取消注册值 */
67
82
  unregisterValue(name: string): void;
68
83
  /** 监听值 */
@@ -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,18 @@ declare module "../.." {
83
94
  options: Record<string, any>;
84
95
  };
85
96
  }
97
+ interface AppPluginExposesMap {
98
+ room: PluginExposes;
99
+ }
86
100
  }
87
101
  export declare class Room extends BasePlugin {
88
102
  static pluginName: string;
89
103
  private map;
90
- private symbolRoomEventHandlers;
91
- constructor(ctx: PluginContext);
104
+ private symbolAbortController;
105
+ init(ctx: PluginContext): void;
106
+ expose(): PluginExposes;
92
107
  /** 添加房间监听实例 */
93
- attachRoom(room: LiveRoom, open?: boolean): void;
108
+ attach(room: LiveRoom, open?: boolean): void;
94
109
  /** 添加房间 */
95
110
  add(platform: string, id: number, options?: boolean | Record<string, any>): Promise<void>;
96
111
  /** 移除房间 */
@@ -99,13 +114,13 @@ export declare class Room extends BasePlugin {
99
114
  get(key: string): LiveRoom | undefined;
100
115
  has(key: string): boolean;
101
116
  /** 获取房间信息 */
102
- info(key: string): LiveRoomData | undefined;
117
+ data(key: string): LiveRoomData | undefined;
103
118
  /** 更新房间信息 */
104
119
  update(key: string): void;
105
120
  /** 获取房间key列表 */
106
- get keys(): Array<string>;
121
+ get keys(): string[];
107
122
  /** 获取房间列表 */
108
- getList(): Array<LiveRoom>;
123
+ getList(): LiveRoom[];
109
124
  /** 打开房间连接 */
110
125
  open(key: string): void;
111
126
  /** 关闭房间连接 */
@@ -2,10 +2,12 @@ import { AppError } from "../../error";
2
2
  import { BasePlugin } from "../../plugin";
3
3
  import { bindCommand } from "../../utils";
4
4
  export class Room extends BasePlugin {
5
- constructor(ctx) {
6
- super(ctx);
5
+ constructor() {
6
+ super(...arguments);
7
7
  this.map = new Map();
8
- this.symbolRoomEventHandlers = Symbol("room.roomEventHandlers");
8
+ this.symbolAbortController = Symbol("room.abortController");
9
+ }
10
+ init(ctx) {
9
11
  ctx.registerCommand("add", bindCommand(this.add, this));
10
12
  ctx.registerCommand("remove", bindCommand(this.remove, this));
11
13
  ctx.registerCommand("open", bindCommand(this.open, this));
@@ -13,8 +15,21 @@ export class Room extends BasePlugin {
13
15
  ctx.registerCommand("update", bindCommand(this.update, this));
14
16
  ctx.registerCommand("room.snapshot", bindCommand(this.getSnapshot, this));
15
17
  }
18
+ expose() {
19
+ return {
20
+ add: this.add.bind(this),
21
+ remove: this.remove.bind(this),
22
+ get: this.get.bind(this),
23
+ has: this.has.bind(this),
24
+ data: this.data.bind(this),
25
+ update: this.update.bind(this),
26
+ open: this.open.bind(this),
27
+ close: this.close.bind(this),
28
+ getList: this.getList.bind(this),
29
+ };
30
+ }
16
31
  /** 添加房间监听实例 */
17
- attachRoom(room, open) {
32
+ attach(room, open) {
18
33
  let key = room.key;
19
34
  if (this.map.has(key)) {
20
35
  this.ctx.throw(new AppError("room:add_exist", {
@@ -23,11 +38,11 @@ export class Room extends BasePlugin {
23
38
  return;
24
39
  }
25
40
  this.map.set(key, room);
26
- const eventHandlers = [];
27
- room[this.symbolRoomEventHandlers] = eventHandlers;
41
+ const abortController = new AbortController();
42
+ room[this.symbolAbortController] =
43
+ abortController;
28
44
  const setHandler = (type, listener) => {
29
- room.on(type, listener);
30
- eventHandlers.push([type, listener]);
45
+ room.on(type, listener, abortController.signal);
31
46
  };
32
47
  // 添加监听事件
33
48
  // 直播消息
@@ -100,7 +115,7 @@ export class Room extends BasePlugin {
100
115
  this.ctx.throw(this.ctx.error("room:create_fail", { message: "无法创建房间" }));
101
116
  }
102
117
  else {
103
- this.attachRoom(room);
118
+ this.attach(room);
104
119
  }
105
120
  }
106
121
  /** 移除房间 */
@@ -112,9 +127,7 @@ export class Room extends BasePlugin {
112
127
  }));
113
128
  }
114
129
  this.map.delete(key); // 从表中删除房间
115
- room[this.symbolRoomEventHandlers].forEach(([type, listener]) => {
116
- room?.removeEventListener(type, listener);
117
- }); // 移除房间监听实例
130
+ room[this.symbolAbortController]?.abort(); // 移除房间监听实例
118
131
  this.ctx.emit("room:remove", { key });
119
132
  }
120
133
  /** 获取房间 */
@@ -125,14 +138,14 @@ export class Room extends BasePlugin {
125
138
  return this.map.has(key);
126
139
  }
127
140
  /** 获取房间信息 */
128
- info(key) {
141
+ data(key) {
129
142
  let room = this.map.get(key);
130
143
  return room ? room.toData() : undefined;
131
144
  }
132
145
  /** 更新房间信息 */
133
146
  update(key) {
134
147
  let room = this.map.get(key);
135
- room && room.getInfo();
148
+ room && room.update();
136
149
  }
137
150
  /** 获取房间key列表 */
138
151
  get keys() {
@@ -1,11 +1,11 @@
1
1
  import { App } from "../app";
2
- import { ValueAccessOptions, ValueOptions } from "./types";
2
+ import { ValueAccessOptions, ValueContext, ValueOptions } from "./types";
3
3
  export declare class ValueManager {
4
4
  private list;
5
5
  protected readonly app: App;
6
6
  constructor(app: App);
7
7
  /** 注册值 */
8
- register<T>(name: string, { get, set, pluginName }: ValueOptions<T>): void;
8
+ register<T>(name: string, { get, set, pluginName }: ValueOptions<T>): ValueContext<T>;
9
9
  /** 取消注册 */
10
10
  unregister(name: string): void;
11
11
  /** 获取值 */
@@ -10,6 +10,11 @@ export class ValueManager {
10
10
  register(name, { get, set, pluginName }) {
11
11
  this.list.set(name, { get, set, pluginName });
12
12
  this.app.emit("value:register", { name, value: get() }, { source: `plugin:${pluginName}` });
13
+ return {
14
+ emit: (v) => {
15
+ this.emit(name, v);
16
+ },
17
+ };
13
18
  }
14
19
  /** 取消注册 */
15
20
  unregister(name) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floating-live",
3
- "version": "0.16.0-alpha.10",
3
+ "version": "0.16.0-alpha.12",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",