floating-live 0.15.2 → 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,16 +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
4
  /** 插件列表 */
5
5
  private list;
6
6
  protected readonly main: FloatingLive;
7
7
  constructor(main: FloatingLive);
8
8
  /** 注册插件 */
9
- register(pluginFunc: PluginConstructor, options?: any): Promise<void> | undefined;
9
+ register(pluginFunc: PluginConstructor, options?: Record<string, any>): Promise<IPlugin | undefined>;
10
10
  /** 移除插件 */
11
- unregister(name: string): void;
11
+ unregister(name: string): Promise<void>;
12
12
  /** 获取插件实例 */
13
- get(name: string): {
14
- [key: string]: any;
15
- } | undefined;
13
+ get(name: string): IPlugin | undefined;
16
14
  }
@@ -8,48 +8,46 @@ class ModPlugin {
8
8
  this.main = main;
9
9
  }
10
10
  /** 注册插件 */
11
- register(pluginFunc, options) {
11
+ async register(pluginFunc, options = {}) {
12
12
  const name = pluginFunc.pluginName;
13
13
  if (!name) {
14
- this.main.throw(Object.assign(new TypeError("缺少插件id"), {
14
+ throw Object.assign(new TypeError("缺少插件id"), {
15
15
  from: "plugin",
16
16
  signal: "plugin_id_missing",
17
- }));
18
- return;
17
+ });
19
18
  }
20
19
  if (this.list.has(name)) {
21
- this.main.throw(Object.assign(new Error("插件id重复"), {
20
+ throw Object.assign(new Error("插件id重复"), {
22
21
  from: "plugin",
23
22
  signal: "plugin_id_duplicate",
24
- }));
25
- return;
23
+ });
26
24
  }
27
- return this.main.hook
25
+ return await this.main.hook
28
26
  .call("plugin.register", { name, options })
29
- .then((res) => {
27
+ .then(async (res) => {
30
28
  if (!res)
31
29
  return;
32
30
  // 执行插件函数
33
31
  const plugin = new pluginFunc(this.main, options);
34
32
  this.list.set(name, plugin);
35
33
  // 调用插件的register钩子
36
- plugin.register?.(this.main, options);
34
+ await plugin.register?.(this.main, options);
37
35
  this.main.emit("plugin:add", name);
36
+ return plugin;
38
37
  });
39
38
  }
40
39
  /** 移除插件 */
41
- unregister(name) {
40
+ async unregister(name) {
42
41
  const plugin = this.list.get(name);
43
42
  // 检测插件是否存在
44
43
  if (!plugin) {
45
- this.main.throw(Object.assign(new Error("插件不存在"), {
44
+ throw Object.assign(new Error("插件不存在"), {
46
45
  from: "plugin",
47
46
  signal: "plugin_unexist",
48
- }));
49
- return;
47
+ });
50
48
  }
51
49
  // 调用该插件的destroy钩子
52
- plugin.destroy?.();
50
+ await plugin.destroy?.();
53
51
  // 从列表中移除插件
54
52
  this.list.delete(name);
55
53
  // 移除插件注册
@@ -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
- register?(ctx: FloatingLive, options: any): void;
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.2",
3
+ "version": "0.15.3",
4
4
  "type": "commonjs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",