floating-live 0.15.7 → 0.15.9

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/live.d.ts CHANGED
@@ -33,6 +33,7 @@ export declare class FloatingLive extends EventEmitter {
33
33
  value: Record<string, any>;
34
34
  manifest: Record<string, Record<string, any>>;
35
35
  command: string[];
36
- hook: Record<string, any>;
36
+ hook: Record<string, number>;
37
+ plugin: string[];
37
38
  };
38
39
  }
package/dist/live.js CHANGED
@@ -43,7 +43,8 @@ class FloatingLive extends events_1.EventEmitter {
43
43
  value: this.value.getSnapshot(),
44
44
  manifest: this.manifest.getSnapshot(),
45
45
  command: this.command.getSnapshot(),
46
- hook: this.value.getSnapshot(),
46
+ hook: this.hook.getSnapshot(),
47
+ plugin: this.plugin.getSnapshot(),
47
48
  };
48
49
  }
49
50
  }
@@ -11,4 +11,5 @@ export declare class ModHook {
11
11
  unregister<T extends keyof FloatingHookMap>(name: T, func: HookFunction<FloatingHookMap[T]>): void;
12
12
  /** 调用钩子函数 */
13
13
  call<T extends keyof FloatingHookMap>(name: T, ctx: FloatingHookMap[T]): Promise<boolean>;
14
+ getSnapshot(): Record<string, number>;
14
15
  }
@@ -40,5 +40,12 @@ class ModHook {
40
40
  }
41
41
  return true;
42
42
  }
43
+ getSnapshot() {
44
+ const map = {};
45
+ this.list.forEach((fnList, name) => {
46
+ map[name] = fnList.length;
47
+ });
48
+ return map;
49
+ }
43
50
  }
44
51
  exports.ModHook = ModHook;
@@ -6,9 +6,11 @@ export declare class ModPlugin {
6
6
  protected readonly main: FloatingLive;
7
7
  constructor(main: FloatingLive);
8
8
  /** 注册插件 */
9
- register(pluginFunc: PluginConstructor, options?: Record<string, any>): Promise<PluginItem | undefined>;
9
+ register(pluginFunc: PluginConstructor, options?: Record<string, any>): Promise<void | PluginItem | undefined>;
10
10
  /** 移除插件 */
11
11
  unregister(name: string): Promise<void>;
12
12
  /** 获取插件实例 */
13
13
  get(name: string): PluginItem | undefined;
14
+ has(name: string): boolean;
15
+ getSnapshot(): string[];
14
16
  }
@@ -12,14 +12,17 @@ class ModPlugin {
12
12
  const name = pluginFunc.pluginName;
13
13
  if (!name) {
14
14
  this.main.throw({
15
- message: "缺少插件id",
15
+ message: "添加插件失败",
16
+ reason: "缺少插件id",
16
17
  id: "plugin:register_id_missing",
17
18
  });
18
19
  }
19
20
  if (this.list.has(name)) {
20
21
  this.main.throw({
21
- message: "已存在相同id的插件",
22
+ message: `添加插件失败: ${name}`,
23
+ reason: "已存在相同id的插件",
22
24
  id: "plugin:register_id_duplicate",
25
+ pluginName: name,
23
26
  });
24
27
  }
25
28
  return await this.main.hook
@@ -29,11 +32,21 @@ class ModPlugin {
29
32
  return;
30
33
  // 执行插件函数
31
34
  const plugin = new pluginFunc(this.main, options);
32
- this.list.set(name, plugin);
33
35
  // 调用插件的register钩子
34
36
  await plugin.register?.(this.main, options);
37
+ this.list.set(name, plugin);
35
38
  this.main.emit("plugin:add", name);
36
39
  return plugin;
40
+ })
41
+ .catch((err) => {
42
+ this.main.throw({
43
+ message: `添加插件失败: ${name}`,
44
+ reason: err?.message && err?.reason
45
+ ? `${err.message}: ${err.reason}`
46
+ : err?.message || err?.reason,
47
+ id: "plugin:register_fail",
48
+ pluginName: name,
49
+ });
37
50
  });
38
51
  }
39
52
  /** 移除插件 */
@@ -42,8 +55,10 @@ class ModPlugin {
42
55
  // 检测插件是否存在
43
56
  if (!plugin) {
44
57
  this.main.throw({
45
- message: "插件不存在",
58
+ message: `移除插件失败: ${name}`,
59
+ reason: "插件不存在",
46
60
  id: "plugin:unregister_unexist",
61
+ pluginName: name,
47
62
  });
48
63
  }
49
64
  else {
@@ -59,5 +74,11 @@ class ModPlugin {
59
74
  get(name) {
60
75
  return this.list.get(name);
61
76
  }
77
+ has(name) {
78
+ return this.list.has(name);
79
+ }
80
+ getSnapshot() {
81
+ return [...this.list.keys()];
82
+ }
62
83
  }
63
84
  exports.ModPlugin = ModPlugin;
@@ -14,6 +14,7 @@ export declare class ModRoom {
14
14
  remove(roomKey: string): void;
15
15
  /** 获取房间 */
16
16
  get(roomKey: string): LiveRoom | undefined;
17
+ has(roomKey: string): boolean;
17
18
  /** 获取房间信息 */
18
19
  info(roomKey: string): RoomInfo | undefined;
19
20
  /** 更新房间信息 */
@@ -113,6 +113,9 @@ class ModRoom {
113
113
  get(roomKey) {
114
114
  return this.list.get(roomKey);
115
115
  }
116
+ has(roomKey) {
117
+ return this.list.has(roomKey);
118
+ }
116
119
  /** 获取房间信息 */
117
120
  info(roomKey) {
118
121
  let room = this.list.get(roomKey);
@@ -146,10 +149,11 @@ class ModRoom {
146
149
  const [item] = arr.splice(index, 1);
147
150
  arr.splice(position, 0, item);
148
151
  this.list = new Map(arr);
152
+ this.main.emit("room:move", roomKey, position);
149
153
  }
150
154
  /** 获取快照 */
151
155
  getSnapshot() {
152
- return [...this.list].map(([key, room]) => room.info);
156
+ return [...this.list.values()].map((room) => room.info);
153
157
  }
154
158
  }
155
159
  exports.ModRoom = ModRoom;
@@ -16,10 +16,11 @@ export declare class ModValue {
16
16
  get<T extends keyof FloatingValueMap>(name: T): any;
17
17
  /** 设置值 */
18
18
  set<T extends keyof FloatingValueMap>(name: T, value: FloatingValueMap[T]): boolean;
19
- /** 获取注册的所有值 */
20
- getSnapshot(): Record<string, any>;
19
+ has(name: string): boolean;
21
20
  emit<T extends keyof FloatingValueMap>(name: T, value: FloatingValueMap[T]): void;
22
21
  watch<T extends keyof FloatingValueMap>(name: T, listener: (value: FloatingValueMap[T]) => void): void;
23
22
  unwatch<T extends keyof FloatingValueMap>(name: T, listener: (value: FloatingValueMap[T]) => void): void;
23
+ /** 获取注册的所有值 */
24
+ getSnapshot(): Record<string, any>;
24
25
  }
25
26
  export {};
@@ -13,11 +13,13 @@ class ModValue {
13
13
  register(name, config) {
14
14
  this.list.set(name, config);
15
15
  this.main.emit("value:add", name);
16
+ this.main.emit("value:change", name, config.get());
16
17
  }
17
18
  /** 取消注册 */
18
19
  unregister(name) {
19
20
  this.list.delete(name);
20
21
  this.main.emit("value:remove", name);
22
+ this.main.emit("value:change", name, undefined);
21
23
  }
22
24
  /** 获取值 */
23
25
  get(name) {
@@ -34,13 +36,8 @@ class ModValue {
34
36
  return false;
35
37
  }
36
38
  }
37
- /** 获取注册的所有值 */
38
- getSnapshot() {
39
- const map = {};
40
- this.list.forEach((config, name) => {
41
- map[name] = config.get();
42
- });
43
- return map;
39
+ has(name) {
40
+ return this.list.has(name);
44
41
  }
45
42
  emit(name, value) {
46
43
  this.main.emit(`change:${name}`, value);
@@ -52,5 +49,13 @@ class ModValue {
52
49
  unwatch(name, listener) {
53
50
  this.main.off(`change:${name}`, listener);
54
51
  }
52
+ /** 获取注册的所有值 */
53
+ getSnapshot() {
54
+ const map = {};
55
+ this.list.forEach((config, name) => {
56
+ map[name] = config.get();
57
+ });
58
+ return map;
59
+ }
55
60
  }
56
61
  exports.ModValue = ModValue;
@@ -1,6 +1,8 @@
1
1
  export interface ErrorOptions {
2
2
  /** 错误消息 */
3
3
  message?: string;
4
+ /** 错误原因 */
5
+ reason?: string;
4
6
  /** 错误id */
5
7
  id?: string;
6
8
  /** 错误名称 */
@@ -23,12 +23,15 @@ export interface FloatingEventMap {
23
23
  "room:close": (key: string, info: RoomInfo) => void;
24
24
  "room:add": (key: string, info: RoomInfo) => void;
25
25
  "room:remove": (key: string) => void;
26
+ "room:move": (key: string, position: number) => void;
26
27
  "plugin:add": (name: string) => void;
27
28
  "plugin:remove": (name: string) => void;
28
29
  "command:add": (name: string) => void;
29
30
  "command:remove": (name: string) => void;
30
31
  "manifest:add": (name: string, id: string, value: any) => void;
31
32
  "manifest:remove": (name: string, id: string) => void;
33
+ "hook:add": (name: string) => void;
34
+ "hook:remove": (name: string) => void;
32
35
  "value:add": (name: string) => void;
33
36
  "value:remove": (name: string) => void;
34
37
  "value:change": (name: string, value: any) => void;
package/esm/live.js CHANGED
@@ -40,7 +40,8 @@ export class FloatingLive extends EventEmitter {
40
40
  value: this.value.getSnapshot(),
41
41
  manifest: this.manifest.getSnapshot(),
42
42
  command: this.command.getSnapshot(),
43
- hook: this.value.getSnapshot(),
43
+ hook: this.hook.getSnapshot(),
44
+ plugin: this.plugin.getSnapshot(),
44
45
  };
45
46
  }
46
47
  }
@@ -37,4 +37,11 @@ export class ModHook {
37
37
  }
38
38
  return true;
39
39
  }
40
+ getSnapshot() {
41
+ const map = {};
42
+ this.list.forEach((fnList, name) => {
43
+ map[name] = fnList.length;
44
+ });
45
+ return map;
46
+ }
40
47
  }
@@ -9,14 +9,17 @@ export class ModPlugin {
9
9
  const name = pluginFunc.pluginName;
10
10
  if (!name) {
11
11
  this.main.throw({
12
- message: "缺少插件id",
12
+ message: "添加插件失败",
13
+ reason: "缺少插件id",
13
14
  id: "plugin:register_id_missing",
14
15
  });
15
16
  }
16
17
  if (this.list.has(name)) {
17
18
  this.main.throw({
18
- message: "已存在相同id的插件",
19
+ message: `添加插件失败: ${name}`,
20
+ reason: "已存在相同id的插件",
19
21
  id: "plugin:register_id_duplicate",
22
+ pluginName: name,
20
23
  });
21
24
  }
22
25
  return await this.main.hook
@@ -26,11 +29,21 @@ export class ModPlugin {
26
29
  return;
27
30
  // 执行插件函数
28
31
  const plugin = new pluginFunc(this.main, options);
29
- this.list.set(name, plugin);
30
32
  // 调用插件的register钩子
31
33
  await plugin.register?.(this.main, options);
34
+ this.list.set(name, plugin);
32
35
  this.main.emit("plugin:add", name);
33
36
  return plugin;
37
+ })
38
+ .catch((err) => {
39
+ this.main.throw({
40
+ message: `添加插件失败: ${name}`,
41
+ reason: err?.message && err?.reason
42
+ ? `${err.message}: ${err.reason}`
43
+ : err?.message || err?.reason,
44
+ id: "plugin:register_fail",
45
+ pluginName: name,
46
+ });
34
47
  });
35
48
  }
36
49
  /** 移除插件 */
@@ -39,8 +52,10 @@ export class ModPlugin {
39
52
  // 检测插件是否存在
40
53
  if (!plugin) {
41
54
  this.main.throw({
42
- message: "插件不存在",
55
+ message: `移除插件失败: ${name}`,
56
+ reason: "插件不存在",
43
57
  id: "plugin:unregister_unexist",
58
+ pluginName: name,
44
59
  });
45
60
  }
46
61
  else {
@@ -56,4 +71,10 @@ export class ModPlugin {
56
71
  get(name) {
57
72
  return this.list.get(name);
58
73
  }
74
+ has(name) {
75
+ return this.list.has(name);
76
+ }
77
+ getSnapshot() {
78
+ return [...this.list.keys()];
79
+ }
59
80
  }
@@ -110,6 +110,9 @@ export class ModRoom {
110
110
  get(roomKey) {
111
111
  return this.list.get(roomKey);
112
112
  }
113
+ has(roomKey) {
114
+ return this.list.has(roomKey);
115
+ }
113
116
  /** 获取房间信息 */
114
117
  info(roomKey) {
115
118
  let room = this.list.get(roomKey);
@@ -143,9 +146,10 @@ export class ModRoom {
143
146
  const [item] = arr.splice(index, 1);
144
147
  arr.splice(position, 0, item);
145
148
  this.list = new Map(arr);
149
+ this.main.emit("room:move", roomKey, position);
146
150
  }
147
151
  /** 获取快照 */
148
152
  getSnapshot() {
149
- return [...this.list].map(([key, room]) => room.info);
153
+ return [...this.list.values()].map((room) => room.info);
150
154
  }
151
155
  }
@@ -10,11 +10,13 @@ export class ModValue {
10
10
  register(name, config) {
11
11
  this.list.set(name, config);
12
12
  this.main.emit("value:add", name);
13
+ this.main.emit("value:change", name, config.get());
13
14
  }
14
15
  /** 取消注册 */
15
16
  unregister(name) {
16
17
  this.list.delete(name);
17
18
  this.main.emit("value:remove", name);
19
+ this.main.emit("value:change", name, undefined);
18
20
  }
19
21
  /** 获取值 */
20
22
  get(name) {
@@ -31,13 +33,8 @@ export class ModValue {
31
33
  return false;
32
34
  }
33
35
  }
34
- /** 获取注册的所有值 */
35
- getSnapshot() {
36
- const map = {};
37
- this.list.forEach((config, name) => {
38
- map[name] = config.get();
39
- });
40
- return map;
36
+ has(name) {
37
+ return this.list.has(name);
41
38
  }
42
39
  emit(name, value) {
43
40
  this.main.emit(`change:${name}`, value);
@@ -49,4 +46,12 @@ export class ModValue {
49
46
  unwatch(name, listener) {
50
47
  this.main.off(`change:${name}`, listener);
51
48
  }
49
+ /** 获取注册的所有值 */
50
+ getSnapshot() {
51
+ const map = {};
52
+ this.list.forEach((config, name) => {
53
+ map[name] = config.get();
54
+ });
55
+ return map;
56
+ }
52
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floating-live",
3
- "version": "0.15.7",
3
+ "version": "0.15.9",
4
4
  "type": "commonjs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",