floating-live 0.15.1 → 0.15.3

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,19 +1,14 @@
1
1
  import { FloatingLive } from "../live";
2
- import { PluginConstructor } from "../types/plugin";
2
+ import { IPlugin, PluginConstructor } from "../types/plugin";
3
3
  export declare class ModPlugin {
4
- private current;
5
4
  /** 插件列表 */
6
5
  private list;
7
6
  protected readonly main: FloatingLive;
8
7
  constructor(main: FloatingLive);
9
- /** 获取当前安装插件 */
10
- get currentPlugin(): string | null;
11
8
  /** 注册插件 */
12
- register(pluginFunc: PluginConstructor, options?: any): void;
9
+ register(pluginFunc: PluginConstructor, options?: Record<string, any>): Promise<IPlugin | undefined>;
13
10
  /** 移除插件 */
14
- unregister(name: string): void;
11
+ unregister(name: string): Promise<void>;
15
12
  /** 获取插件实例 */
16
- get(name: string): {
17
- [key: string]: any;
18
- } | undefined;
13
+ get(name: string): IPlugin | undefined;
19
14
  }
@@ -3,51 +3,51 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ModPlugin = void 0;
4
4
  class ModPlugin {
5
5
  constructor(main) {
6
- this.current = null;
7
6
  /** 插件列表 */
8
7
  this.list = new Map();
9
8
  this.main = main;
10
9
  }
11
- /** 获取当前安装插件 */
12
- get currentPlugin() {
13
- return this.current;
14
- }
15
10
  /** 注册插件 */
16
- register(pluginFunc, options) {
11
+ async register(pluginFunc, options = {}) {
17
12
  const name = pluginFunc.pluginName;
18
13
  if (!name) {
19
- this.main.throw(Object.assign(new TypeError("缺少插件id"), {
14
+ throw Object.assign(new TypeError("缺少插件id"), {
20
15
  from: "plugin",
21
16
  signal: "plugin_id_missing",
22
- }));
23
- return;
17
+ });
24
18
  }
25
19
  if (this.list.has(name)) {
26
- this.main.throw(Object.assign(new Error("插件id重复"), {
20
+ throw Object.assign(new Error("插件id重复"), {
27
21
  from: "plugin",
28
22
  signal: "plugin_id_duplicate",
29
- }));
30
- return;
23
+ });
31
24
  }
32
- // 执行插件函数
33
- const plugin = new pluginFunc(this.main, options);
34
- this.list.set(name, plugin);
35
- this.main.emit("plugin:add", name);
36
- this.current = null;
25
+ return await this.main.hook
26
+ .call("plugin.register", { name, options })
27
+ .then(async (res) => {
28
+ if (!res)
29
+ return;
30
+ // 执行插件函数
31
+ const plugin = new pluginFunc(this.main, options);
32
+ this.list.set(name, plugin);
33
+ // 调用插件的register钩子
34
+ await plugin.register?.(this.main, options);
35
+ this.main.emit("plugin:add", name);
36
+ return plugin;
37
+ });
37
38
  }
38
39
  /** 移除插件 */
39
- unregister(name) {
40
+ async unregister(name) {
40
41
  const plugin = this.list.get(name);
41
42
  // 检测插件是否存在
42
43
  if (!plugin) {
43
- this.main.throw(Object.assign(new Error("插件不存在"), {
44
+ throw Object.assign(new Error("插件不存在"), {
44
45
  from: "plugin",
45
46
  signal: "plugin_unexist",
46
- }));
47
- return;
47
+ });
48
48
  }
49
- // 获取该插件的销毁函数
50
- plugin.destroy?.();
49
+ // 调用该插件的destroy钩子
50
+ await plugin.destroy?.();
51
51
  // 从列表中移除插件
52
52
  this.list.delete(name);
53
53
  // 移除插件注册
@@ -27,7 +27,7 @@ class ModRoom {
27
27
  // 添加监听事件
28
28
  // 直播消息
29
29
  room.on("message", (data) => {
30
- this.main.hook.call("message", data).then((res) => {
30
+ this.main.hook.call("message", { message: data }).then((res) => {
31
31
  res && this.main.emit("live:message", data);
32
32
  });
33
33
  });
@@ -84,7 +84,7 @@ class ModRoom {
84
84
  add(platform, id, options) {
85
85
  const opt = typeof options == "boolean" ? { open: options } : options;
86
86
  const ctx = { platform, id, options: opt || {} };
87
- this.main.hook.call("add", ctx).then((res) => {
87
+ this.main.hook.call("room.add", ctx).then((res) => {
88
88
  if (!res)
89
89
  return;
90
90
  const room = this.main.call(`${platform}.room.create`, id, opt);
@@ -9,13 +9,13 @@ export declare class ModValue {
9
9
  protected readonly main: FloatingLive;
10
10
  constructor(main: FloatingLive);
11
11
  /** 注册值 */
12
- register<T>(name: string, config: ValueConfig<T>): void;
12
+ register<T extends keyof FloatingValueMap>(name: T, config: ValueConfig<FloatingValueMap[T]>): void;
13
13
  /** 取消注册 */
14
14
  unregister(name: string): void;
15
15
  /** 获取值 */
16
- get(name: string): any;
16
+ get<T extends keyof FloatingValueMap>(name: T): any;
17
17
  /** 设置值 */
18
- set(name: string, value: any): boolean;
18
+ set<T extends keyof FloatingValueMap>(name: T, value: FloatingValueMap[T]): boolean;
19
19
  /** 获取注册的所有值 */
20
20
  getSnapshot(): Record<string, any>;
21
21
  emit<T extends keyof FloatingValueMap>(name: T, value: FloatingValueMap[T]): void;
@@ -1,9 +1,15 @@
1
1
  import { Message } from "./message";
2
2
  export interface FloatingHookMap {
3
- message: Message.All;
4
- add: {
3
+ message: {
4
+ message: Message.All;
5
+ };
6
+ "room.add": {
5
7
  platform: string;
6
8
  id: string | number;
7
9
  options: Record<string, any>;
8
10
  };
11
+ "plugin.register": {
12
+ name: string;
13
+ options: Record<string, any>;
14
+ };
9
15
  }
@@ -7,8 +7,9 @@ export type FloatingLivePlugin<C extends object = object> = () => {
7
7
  destroy?: (ctx: FloatingLive) => void;
8
8
  };
9
9
  export interface IPlugin {
10
- [key: string]: any;
11
- destroy?(): void;
10
+ register?(ctx: FloatingLive, options: any): void | Promise<void>;
11
+ destroy?(): void | Promise<void>;
12
+ [name: string]: any;
12
13
  }
13
14
  export interface PluginConstructor {
14
15
  pluginName: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floating-live",
3
- "version": "0.15.1",
3
+ "version": "0.15.3",
4
4
  "type": "commonjs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -34,7 +34,8 @@
34
34
  "test": "npm run clear && ts-node demo/demo.ts",
35
35
  "build-test": "npm run build && node demo/demo.js",
36
36
  "build": "npm run clear && npm run tsc",
37
- "prepare": "npm run build"
37
+ "prepack": "npm run build",
38
+ "postpack": "npm run clear"
38
39
  },
39
40
  "devDependencies": {
40
41
  "rimraf": "^3.0.2",