floating-live 0.15.0-beta.1 → 0.15.0-beta.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.
Files changed (44) hide show
  1. package/dist/core/index.d.ts +0 -1
  2. package/dist/core/index.js +0 -1
  3. package/dist/core/plugin.d.ts +17 -0
  4. package/dist/core/plugin.js +28 -0
  5. package/dist/index.d.ts +0 -1
  6. package/dist/index.js +0 -1
  7. package/dist/live.d.ts +24 -16
  8. package/dist/live.js +27 -36
  9. package/dist/module/command.d.ts +16 -0
  10. package/dist/module/command.js +28 -0
  11. package/dist/module/hook.d.ts +14 -0
  12. package/dist/module/hook.js +44 -0
  13. package/dist/module/plugin.d.ts +15 -0
  14. package/dist/module/{ModPlugin.js → plugin.js} +19 -25
  15. package/dist/module/{ModRoom.d.ts → room.d.ts} +6 -10
  16. package/dist/module/{ModRoom.js → room.js} +49 -65
  17. package/dist/module/store.d.ts +15 -0
  18. package/dist/module/store.js +44 -0
  19. package/dist/module/values.d.ts +25 -0
  20. package/dist/module/values.js +51 -0
  21. package/dist/types/command.d.ts +9 -0
  22. package/dist/types/command.js +2 -0
  23. package/dist/types/events.d.ts +34 -0
  24. package/dist/types/events.js +2 -0
  25. package/dist/types/hook.d.ts +5 -0
  26. package/dist/types/hook.js +2 -0
  27. package/dist/types/index.d.ts +2 -0
  28. package/dist/types/index.js +2 -0
  29. package/dist/types/plugin.d.ts +9 -2
  30. package/dist/types/room.d.ts +17 -9
  31. package/dist/types/store.d.ts +4 -0
  32. package/dist/types/store.js +2 -0
  33. package/dist/types/values.d.ts +2 -0
  34. package/dist/types/values.js +2 -0
  35. package/package.json +1 -1
  36. package/dist/core/Reglist.d.ts +0 -29
  37. package/dist/core/Reglist.js +0 -69
  38. package/dist/module/ModCommand.d.ts +0 -9
  39. package/dist/module/ModCommand.js +0 -19
  40. package/dist/module/ModMessage.d.ts +0 -9
  41. package/dist/module/ModMessage.js +0 -16
  42. package/dist/module/ModPlugin.d.ts +0 -20
  43. package/dist/module/ModState.d.ts +0 -12
  44. package/dist/module/ModState.js +0 -29
@@ -1,2 +1 @@
1
1
  export * from "./LiveRoom";
2
- export * from "./Reglist";
@@ -15,4 +15,3 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./LiveRoom"), exports);
18
- __exportStar(require("./Reglist"), exports);
@@ -0,0 +1,17 @@
1
+ import { FloatingLive } from "../live";
2
+ import { FloatingEventMap } from "../types/events";
3
+ export declare abstract class BasePlugin {
4
+ static pluginName: string;
5
+ protected main: FloatingLive;
6
+ private registerList;
7
+ constructor(main: FloatingLive);
8
+ /** 插件监听事件 */
9
+ on<T extends keyof FloatingEventMap>(eventName: T, listener: FloatingEventMap[T]): void;
10
+ /** 插件一次性监听事件 */
11
+ once<T extends keyof FloatingEventMap>(eventName: T, listener: FloatingEventMap[T]): void;
12
+ /** 插件移除监听事件 */
13
+ off<T extends keyof FloatingEventMap>(eventName: T, listener: FloatingEventMap[T]): void;
14
+ /** 插件发送事件 */
15
+ emit<T extends keyof FloatingEventMap>(eventName: T, ...args: Parameters<FloatingEventMap[T]>): void;
16
+ abstract destroy(): void;
17
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BasePlugin = void 0;
4
+ class BasePlugin {
5
+ constructor(main) {
6
+ this.registerList = [];
7
+ this.main = main;
8
+ }
9
+ /** 插件监听事件 */
10
+ on(eventName, listener) {
11
+ this.main.on(eventName, listener);
12
+ this.registerList.push(["event", eventName, listener]);
13
+ }
14
+ /** 插件一次性监听事件 */
15
+ once(eventName, listener) {
16
+ this.main.once(eventName, listener);
17
+ this.registerList.push(["event", eventName, listener]);
18
+ }
19
+ /** 插件移除监听事件 */
20
+ off(eventName, listener) {
21
+ this.main.off(eventName, listener);
22
+ }
23
+ /** 插件发送事件 */
24
+ emit(eventName, ...args) {
25
+ this.main.emit(eventName, ...args);
26
+ }
27
+ }
28
+ exports.BasePlugin = BasePlugin;
package/dist/index.d.ts CHANGED
@@ -2,5 +2,4 @@ export { FloatingLive } from "./live";
2
2
  export * from "./types";
3
3
  export * from "./enums";
4
4
  export * from "./core/LiveRoom";
5
- export * from "./core/Reglist";
6
5
  export * from "./utils";
package/dist/index.js CHANGED
@@ -20,5 +20,4 @@ Object.defineProperty(exports, "FloatingLive", { enumerable: true, get: function
20
20
  __exportStar(require("./types"), exports);
21
21
  __exportStar(require("./enums"), exports);
22
22
  __exportStar(require("./core/LiveRoom"), exports);
23
- __exportStar(require("./core/Reglist"), exports);
24
23
  __exportStar(require("./utils"), exports);
package/dist/live.d.ts CHANGED
@@ -1,27 +1,35 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from "events";
3
- import { ModPlugin } from "./module/ModPlugin";
4
- import { ModRoom } from "./module/ModRoom";
5
- import { ModMessage } from "./module/ModMessage";
6
- import { ModState } from "./module/ModState";
7
- import ModCommand from "./module/ModCommand";
3
+ import { ModPlugin } from "./module/plugin";
4
+ import { ModRoom } from "./module/room";
5
+ import { ModStore } from "./module/store";
6
+ import ModCommand from "./module/command";
7
+ import { ModValues } from "./module/values";
8
+ import { ModHook } from "./module/hook";
9
+ import { FloatingEventMap } from "./types/events";
10
+ import { FloatingCommandMap } from "./types";
8
11
  export declare class FloatingLive extends EventEmitter {
9
12
  /** 房间控制模块 */
10
13
  room: ModRoom;
11
- /** 消息处理模块 */
12
- message: ModMessage;
13
14
  /** 插件处理模块 */
14
- plugin: ModPlugin<this>;
15
+ plugin: ModPlugin;
15
16
  /** 命令模块 */
16
17
  command: ModCommand;
17
18
  /** 状态模块 */
18
- state: ModState;
19
- /** 插件事件注册表 */
20
- private regEventMap;
19
+ store: ModStore;
20
+ /** 数值模块 */
21
+ values: ModValues;
22
+ /** 钩子模块 */
23
+ hook: ModHook;
21
24
  constructor();
22
- /** 初始化 */
23
- private init;
24
- /** 重写事件监听 */
25
- on(eventName: string, listener: (...args: any[]) => void): this;
26
- emit(eventName: string | symbol, ...args: any[]): boolean;
25
+ call<T extends keyof FloatingCommandMap>(name: T, ...args: Parameters<FloatingCommandMap[T]>): ReturnType<FloatingCommandMap[T]>;
26
+ on<T extends keyof FloatingEventMap>(eventName: T, listener: FloatingEventMap[T]): this;
27
+ emit<T extends keyof FloatingEventMap>(eventName: T, ...args: Parameters<FloatingEventMap[T]>): boolean;
28
+ throw(err: Error): void;
29
+ snapshot(): {
30
+ room: import("./types").RoomInfo[];
31
+ values: Record<string, any>;
32
+ store: Record<string, Record<string, any>>;
33
+ command: string[];
34
+ };
27
35
  }
package/dist/live.js CHANGED
@@ -5,50 +5,41 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.FloatingLive = void 0;
7
7
  const events_1 = require("events");
8
- const ModPlugin_1 = require("./module/ModPlugin");
9
- const ModRoom_1 = require("./module/ModRoom");
10
- const ModMessage_1 = require("./module/ModMessage");
11
- const ModState_1 = require("./module/ModState");
12
- const ModCommand_1 = __importDefault(require("./module/ModCommand"));
8
+ const plugin_1 = require("./module/plugin");
9
+ const room_1 = require("./module/room");
10
+ const store_1 = require("./module/store");
11
+ const command_1 = __importDefault(require("./module/command"));
12
+ const values_1 = require("./module/values");
13
+ const hook_1 = require("./module/hook");
13
14
  class FloatingLive extends events_1.EventEmitter {
14
15
  constructor() {
15
16
  super();
16
- /** 插件事件注册表 */
17
- this.regEventMap = new Map();
18
- this.command = new ModCommand_1.default(this);
19
- this.state = new ModState_1.ModState(this);
20
- this.plugin = new ModPlugin_1.ModPlugin(this);
21
- this.room = new ModRoom_1.ModRoom(this);
22
- this.message = new ModMessage_1.ModMessage(this);
23
- this.init();
17
+ this.command = new command_1.default(this);
18
+ this.store = new store_1.ModStore(this);
19
+ this.plugin = new plugin_1.ModPlugin(this);
20
+ this.room = new room_1.ModRoom(this);
21
+ this.values = new values_1.ModValues(this);
22
+ this.hook = new hook_1.ModHook(this);
24
23
  }
25
- /** 初始化 */
26
- init() {
27
- // 设置在插件卸载时移除由插件注册的事件
28
- this.on("plugin:remove", (name) => {
29
- this.regEventMap.get(name)?.forEach(([eventName, listener]) => {
30
- this.removeListener(eventName, listener);
31
- });
32
- });
24
+ call(name, ...args) {
25
+ return this.command.call(name, ...args);
33
26
  }
34
- /** 重写事件监听 */
35
27
  on(eventName, listener) {
36
- super.on(eventName, listener);
37
- // 检测是否为插件添加事件,并添加进事件列表
38
- if (this.plugin.currentPlugin) {
39
- if (!this.regEventMap.has(this.plugin.currentPlugin)) {
40
- this.regEventMap.set(this.plugin.currentPlugin, []);
41
- }
42
- this.regEventMap
43
- .get(this.plugin.currentPlugin)
44
- ?.push([eventName, listener]);
45
- }
46
- return this;
28
+ return super.on(eventName, listener);
47
29
  }
48
30
  emit(eventName, ...args) {
49
- const result = super.emit(eventName, ...args);
50
- super.emit("event", eventName, ...args);
51
- return result;
31
+ return super.emit(eventName, ...args);
32
+ }
33
+ throw(err) {
34
+ this.emit("error", err);
35
+ }
36
+ snapshot() {
37
+ return {
38
+ room: this.room.snapshot(),
39
+ values: this.values.snapshot(),
40
+ store: this.store.snapshot(),
41
+ command: this.command.snapshot(),
42
+ };
52
43
  }
53
44
  }
54
45
  exports.FloatingLive = FloatingLive;
@@ -0,0 +1,16 @@
1
+ import { FloatingLive } from "../live";
2
+ import { FloatingCommandMap } from "../types";
3
+ export default class ModCommand {
4
+ /** 功能列表 */
5
+ private readonly list;
6
+ protected readonly main: FloatingLive;
7
+ constructor(main: FloatingLive);
8
+ /** 注册命令 */
9
+ register<T extends keyof FloatingCommandMap>(name: T, func: FloatingCommandMap[T]): void;
10
+ /** 取消注册命令 */
11
+ unregister(name: string): void;
12
+ /** 调用命令 */
13
+ call<T extends keyof FloatingCommandMap>(name: T, ...args: Parameters<FloatingCommandMap[T]>): any;
14
+ /** 获取快照 */
15
+ snapshot(): string[];
16
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ModCommand {
4
+ constructor(main) {
5
+ /** 功能列表 */
6
+ this.list = new Map();
7
+ this.main = main;
8
+ }
9
+ /** 注册命令 */
10
+ register(name, func) {
11
+ this.list.set(name, func);
12
+ this.main.emit("command:add", name);
13
+ }
14
+ /** 取消注册命令 */
15
+ unregister(name) {
16
+ this.list.delete(name);
17
+ this.main.emit("command:remove", name);
18
+ }
19
+ /** 调用命令 */
20
+ call(name, ...args) {
21
+ return this.list.get(name)?.(...args);
22
+ }
23
+ /** 获取快照 */
24
+ snapshot() {
25
+ return [...this.list].map(([key]) => key);
26
+ }
27
+ }
28
+ exports.default = ModCommand;
@@ -0,0 +1,14 @@
1
+ import { FloatingLive } from "../live";
2
+ import { FloatingHookMap } from "../types/hook";
3
+ export declare type HookFunction<T> = (ctx: T) => boolean | void | Promise<boolean | void>;
4
+ export declare class ModHook {
5
+ private list;
6
+ protected readonly main: FloatingLive;
7
+ constructor(main: FloatingLive);
8
+ /** 注册钩子函数 */
9
+ register<T extends keyof FloatingHookMap>(name: T, func: HookFunction<FloatingHookMap[T]>): void;
10
+ /** 取消注册钩子函数 */
11
+ unregister<T extends keyof FloatingHookMap>(name: T, func: HookFunction<FloatingHookMap[T]>): void;
12
+ /** 调用钩子函数 */
13
+ call<T extends keyof FloatingHookMap>(name: T, ctx: FloatingHookMap[T]): Promise<boolean>;
14
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModHook = void 0;
4
+ class ModHook {
5
+ constructor(main) {
6
+ this.list = new Map();
7
+ this.main = main;
8
+ }
9
+ /** 注册钩子函数 */
10
+ register(name, func) {
11
+ let hooks = this.list.get(name);
12
+ if (!hooks) {
13
+ hooks = [];
14
+ this.list.set(name, hooks);
15
+ }
16
+ hooks.push(func);
17
+ }
18
+ /** 取消注册钩子函数 */
19
+ unregister(name, func) {
20
+ let hooks = this.list.get(name);
21
+ if (hooks) {
22
+ const index = hooks.indexOf(func);
23
+ index > -1 && hooks.splice(index, 1);
24
+ }
25
+ }
26
+ /** 调用钩子函数 */
27
+ async call(name, ctx) {
28
+ const hooks = this.list.get(name);
29
+ if (!hooks) {
30
+ return true;
31
+ }
32
+ for (const func of hooks) {
33
+ const result = await func(ctx);
34
+ if (result == true) {
35
+ return true;
36
+ }
37
+ else if (result == false) {
38
+ return false;
39
+ }
40
+ }
41
+ return false;
42
+ }
43
+ }
44
+ exports.ModHook = ModHook;
@@ -0,0 +1,15 @@
1
+ import { FloatingLive } from "../live";
2
+ import { PluginConstructor } from "../types/plugin";
3
+ export declare class ModPlugin {
4
+ private current;
5
+ /** 插件列表 */
6
+ private list;
7
+ protected readonly main: FloatingLive;
8
+ constructor(main: FloatingLive);
9
+ /** 获取当前安装插件 */
10
+ get currentPlugin(): string | null;
11
+ /** 注册插件 */
12
+ register(pluginFunc: PluginConstructor, config?: any): void;
13
+ /** 移除插件 */
14
+ unregister(name: string): void;
15
+ }
@@ -6,50 +6,44 @@ class ModPlugin {
6
6
  this.current = null;
7
7
  /** 插件列表 */
8
8
  this.list = new Map();
9
- this.pluginEvent = new Map();
10
- this.exports = new Map();
11
9
  this.main = main;
12
10
  }
13
11
  /** 获取当前安装插件 */
14
12
  get currentPlugin() {
15
13
  return this.current;
16
14
  }
17
- addPluginEvent(eventName, listener) {
18
- if (this.currentPlugin) {
19
- if (!this.pluginEvent.has(this.currentPlugin)) {
20
- this.pluginEvent.set(this.currentPlugin, []);
21
- }
22
- this.pluginEvent.get(this.currentPlugin)?.push([eventName, listener]);
23
- }
24
- }
25
- /** 访问插件操作对象 */
26
- access(name) {
27
- return this.exports.get(name);
28
- }
29
15
  /** 注册插件 */
30
- register(pluginFunc, pluginConfig) {
31
- // 执行插件函数
32
- const plugin = pluginFunc();
33
- const name = plugin.name;
16
+ register(pluginFunc, config) {
17
+ const name = pluginFunc.name;
18
+ if (!name) {
19
+ this.main.throw(Object.assign(new TypeError("缺少插件id"), {
20
+ from: "plugin",
21
+ signal: "plugin_id_missing",
22
+ }));
23
+ return;
24
+ }
34
25
  if (this.list.has(name)) {
35
- this.main.emit("plugin:duplicate", name);
26
+ this.main.throw(Object.assign(new Error("插件id重复"), {
27
+ from: "plugin",
28
+ signal: "plugin_id_duplicate",
29
+ }));
36
30
  return;
37
31
  }
32
+ // 执行插件函数
33
+ const plugin = new pluginFunc(this.main, config);
38
34
  this.list.set(name, plugin);
39
- this.current = name;
40
- // 调用register函数,并保存获取值
41
- let pluginExpose = plugin.register?.(this.main, pluginConfig) || {};
42
- this.exports.set(name, pluginExpose);
43
35
  this.main.emit("plugin:add", name);
44
36
  this.current = null;
45
- return pluginExpose;
46
37
  }
47
38
  /** 移除插件 */
48
39
  unregister(name) {
49
40
  const plugin = this.list.get(name);
50
41
  // 检测插件是否存在
51
42
  if (!plugin) {
52
- this.main.emit("plugin:unexist", name);
43
+ this.main.throw(Object.assign(new Error("插件不存在"), {
44
+ from: "plugin",
45
+ signal: "plugin_unexist",
46
+ }));
53
47
  return;
54
48
  }
55
49
  // 获取该插件的销毁函数
@@ -1,23 +1,15 @@
1
- import { Reglist } from "../core/Reglist";
2
1
  import { LiveRoom } from "../core/LiveRoom";
3
2
  import { FloatingLive } from "../live";
4
3
  import { RoomInfo } from "../types";
5
4
  /** 直播间监听实例控制器 */
6
5
  export declare class ModRoom {
6
+ private list;
7
7
  readonly main: FloatingLive;
8
- private roomMap;
9
- private auths;
10
- private configs;
11
- generator: Reglist<(id: string | number, open?: boolean, config?: object) => LiveRoom>;
12
8
  constructor(main: FloatingLive);
13
- /** 设置房间配置 */
14
- setConfig(platform: string, config: any): void;
15
- /** 生成房间 */
16
- generate(platform: string, id: string | number, open?: boolean): LiveRoom | undefined;
17
9
  /** 添加房间监听实例 */
18
10
  addRoom(room: LiveRoom, open?: boolean): void;
19
11
  /** 添加房间 */
20
- add(platform: string, id: number, open: boolean): void;
12
+ add(platform: string, id: number, config?: boolean | Record<string, any>): void;
21
13
  /** 移除房间 */
22
14
  remove(roomKey: string): void;
23
15
  /** 获取房间 */
@@ -32,4 +24,8 @@ export declare class ModRoom {
32
24
  open(roomKey: string): void;
33
25
  /** 关闭房间连接 */
34
26
  close(roomKey: string): void;
27
+ /** 更改某个房间的顺序 */
28
+ move(roomKey: string, position: number): void;
29
+ /** 获取快照 */
30
+ snapshot(): RoomInfo[];
35
31
  }
@@ -1,70 +1,35 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ModRoom = void 0;
4
- const Reglist_1 = require("../core/Reglist");
5
4
  /** 直播间监听实例控制器 */
6
5
  class ModRoom {
7
6
  constructor(main) {
8
- this.roomMap = new Map();
9
- this.auths = new Map();
10
- this.configs = new Map();
7
+ this.list = new Map();
11
8
  this.main = main;
12
- this.generator = new Reglist_1.Reglist(this.main, "room.generator");
13
- this.main.state.register("room", () => {
14
- const list = [];
15
- this.roomMap.forEach((room) => {
16
- list.push(room.info);
17
- });
18
- return { list };
19
- });
20
- this.main.command.batchRegister({
21
- add: (platform, id, open) => {
22
- this.add(platform, id, open);
23
- },
24
- remove: (key) => {
25
- this.remove(key);
26
- },
27
- open: (key) => {
28
- this.open(key);
29
- },
30
- close: (key) => {
31
- this.close(key);
32
- },
33
- update: (key) => {
34
- this.update(key);
35
- },
36
- });
37
- }
38
- /** 设置房间配置 */
39
- setConfig(platform, config) {
40
- this.configs.set(platform, config);
41
- }
42
- /** 生成房间 */
43
- generate(platform, id, open) {
44
- platform = platform.toLowerCase();
45
- const config = this.configs.get(platform) || {};
46
- const auth = this.auths.get(platform);
47
- let platformGenerator = this.generator.get(platform);
48
- if (platformGenerator) {
49
- return platformGenerator(id, open, { auth, ...config });
50
- }
51
- else {
52
- this.main.emit("room:unknown_platform", platform);
53
- }
9
+ const { command } = main;
10
+ command.register("add", this.add.bind(this));
11
+ command.register("remove", this.remove.bind(this));
12
+ command.register("open", this.open.bind(this));
13
+ command.register("close", this.close.bind(this));
14
+ command.register("update", this.update.bind(this));
54
15
  }
55
16
  /** 添加房间监听实例 */
56
17
  addRoom(room, open) {
57
18
  let key = room.key;
58
- if (this.roomMap.has(key)) {
59
- this.main.emit("room:exist", key);
19
+ if (this.list.has(key)) {
20
+ this.main.throw(Object.assign(new Error("房间已存在"), {
21
+ from: "room",
22
+ signal: "room_exist",
23
+ }));
60
24
  return;
61
25
  }
62
- this.roomMap.set(key, room);
26
+ this.list.set(key, room);
63
27
  // 添加监听事件
64
28
  // 直播消息
65
29
  room.on("msg", (data) => {
66
- this.main.message.handle(data);
67
- this.main.emit("live:message", data);
30
+ this.main.hook.call("message", data).then((res) => {
31
+ res && this.main.emit("live:message", data);
32
+ });
68
33
  });
69
34
  // 直播消息源数据
70
35
  room.on("raw", (data, { platform, room }) => {
@@ -88,7 +53,7 @@ class ModRoom {
88
53
  });
89
54
  // 直播状态更改
90
55
  room.on("status", (status, { id, timestamp }) => {
91
- this.main.emit("room:status", key, status, { id, timestamp });
56
+ this.main.emit("room:status", key, { status, id, timestamp });
92
57
  });
93
58
  // 统计数据更新
94
59
  room.on("stats", (data) => {
@@ -108,48 +73,67 @@ class ModRoom {
108
73
  this.main.emit("room:add", key, room.info);
109
74
  }
110
75
  /** 添加房间 */
111
- add(platform, id, open) {
112
- const room = this.generate(platform, id, open);
76
+ add(platform, id, config) {
77
+ if (typeof config == "boolean") {
78
+ config = { open: config };
79
+ }
80
+ const room = this.main.call(`room.create.${platform}`, id, config);
113
81
  room && this.addRoom(room);
114
82
  }
115
83
  /** 移除房间 */
116
84
  remove(roomKey) {
117
- if (!this.roomMap.has(roomKey)) {
118
- this.main.emit("room:unexist", roomKey);
119
- return;
85
+ let room = this.list.get(roomKey);
86
+ if (!room) {
87
+ this.main.throw(Object.assign(new Error("房间不存在"), {
88
+ from: "room",
89
+ signal: "room_unexist",
90
+ }));
120
91
  }
121
- let room = this.roomMap.get(roomKey);
122
- this.roomMap.delete(roomKey); // 从表中删除房间
92
+ this.list.delete(roomKey); // 从表中删除房间
123
93
  room?.removeAllListeners(); // 移除房间监听实例
124
94
  this.main.emit("room:remove", roomKey);
125
95
  }
126
96
  /** 获取房间 */
127
97
  get(roomKey) {
128
- return this.roomMap.get(roomKey);
98
+ return this.list.get(roomKey);
129
99
  }
130
100
  /** 获取房间信息 */
131
101
  info(roomKey) {
132
- let room = this.roomMap.get(roomKey);
102
+ let room = this.list.get(roomKey);
133
103
  return room ? room.info : undefined;
134
104
  }
135
105
  /** 更新房间信息 */
136
106
  update(roomKey) {
137
- let room = this.roomMap.get(roomKey);
107
+ let room = this.list.get(roomKey);
138
108
  room && room.getInfo();
139
109
  }
140
110
  /** 获取roomKey列表 */
141
111
  get keys() {
142
- return [...this.roomMap.keys()];
112
+ return [...this.list.keys()];
143
113
  }
144
114
  /** 打开房间连接 */
145
115
  open(roomKey) {
146
- let room = this.roomMap.get(roomKey);
116
+ let room = this.list.get(roomKey);
147
117
  room?.available && !room.opened && room.open();
148
118
  }
149
119
  /** 关闭房间连接 */
150
120
  close(roomKey) {
151
- let room = this.roomMap.get(roomKey);
121
+ let room = this.list.get(roomKey);
152
122
  room?.opened && room.close();
153
123
  }
124
+ /** 更改某个房间的顺序 */
125
+ move(roomKey, position) {
126
+ const arr = [...this.list];
127
+ const index = arr.findIndex(([key]) => key == roomKey);
128
+ if (index == -1)
129
+ return;
130
+ const [item] = arr.splice(index, 1);
131
+ arr.splice(position, 0, item);
132
+ this.list = new Map(arr);
133
+ }
134
+ /** 获取快照 */
135
+ snapshot() {
136
+ return [...this.list].map(([key, room]) => room.info);
137
+ }
154
138
  }
155
139
  exports.ModRoom = ModRoom;
@@ -0,0 +1,15 @@
1
+ import { FloatingLive } from "../live";
2
+ import { FloatingStoreMap } from "../types/store";
3
+ export declare class ModStore {
4
+ private list;
5
+ protected readonly main: FloatingLive;
6
+ constructor(main: FloatingLive);
7
+ /** 注册store */
8
+ register<T extends keyof FloatingStoreMap>(name: T, id: string, value: FloatingStoreMap[T]): void;
9
+ /** 取消注册store */
10
+ unregister<T extends keyof FloatingStoreMap>(name: T, id: string): void;
11
+ /** 获取值 */
12
+ get(name: string): Map<string, any> | undefined;
13
+ /** 获取快照 */
14
+ snapshot(): Record<string, Record<string, any>>;
15
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModStore = void 0;
4
+ class ModStore {
5
+ constructor(main) {
6
+ this.list = new Map();
7
+ this.main = main;
8
+ }
9
+ /** 注册store */
10
+ register(name, id, value) {
11
+ let store = this.list.get(name);
12
+ if (!store) {
13
+ store = new Map();
14
+ this.list.set(name, store);
15
+ }
16
+ store.set(id, value);
17
+ this.main.emit("store:add", name, id, value);
18
+ }
19
+ /** 取消注册store */
20
+ unregister(name, id) {
21
+ let store = this.list.get(name);
22
+ if (store) {
23
+ store.delete(id);
24
+ this.main.emit("store:remove", name, id);
25
+ }
26
+ }
27
+ /** 获取值 */
28
+ get(name) {
29
+ return this.list.get(name);
30
+ }
31
+ /** 获取快照 */
32
+ snapshot() {
33
+ const map = {};
34
+ this.list.forEach((store, name) => {
35
+ const part = {};
36
+ map[name] = part;
37
+ store.forEach((value, id) => {
38
+ part[id] = value;
39
+ });
40
+ });
41
+ return map;
42
+ }
43
+ }
44
+ exports.ModStore = ModStore;
@@ -0,0 +1,25 @@
1
+ import { FloatingLive } from "../live";
2
+ import { FloatingValueMap } from "../types/values";
3
+ interface ValueConfig<T> {
4
+ get: () => T;
5
+ set?: (value: T) => void;
6
+ }
7
+ export declare class ModValues {
8
+ private list;
9
+ protected readonly main: FloatingLive;
10
+ constructor(main: FloatingLive);
11
+ /** 注册值 */
12
+ register<T>(name: string, config: ValueConfig<T>): void;
13
+ /** 取消注册 */
14
+ unregister(name: string): void;
15
+ /** 获取值 */
16
+ get(name: string): any;
17
+ /** 设置值 */
18
+ set(name: string, value: any): boolean;
19
+ /** 获取注册的所有值 */
20
+ snapshot(): Record<string, any>;
21
+ emit<T extends keyof FloatingValueMap>(name: T, value: FloatingValueMap[T]): void;
22
+ watch<T extends keyof FloatingValueMap>(name: T, listener: (value: FloatingValueMap[T]) => void): void;
23
+ unwatch<T extends keyof FloatingValueMap>(name: T, listener: (value: FloatingValueMap[T]) => void): void;
24
+ }
25
+ export {};
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModValues = void 0;
4
+ class ModValues {
5
+ constructor(main) {
6
+ this.list = new Map();
7
+ this.main = main;
8
+ }
9
+ /** 注册值 */
10
+ register(name, config) {
11
+ this.list.set(name, config);
12
+ }
13
+ /** 取消注册 */
14
+ unregister(name) {
15
+ this.list.delete(name);
16
+ }
17
+ /** 获取值 */
18
+ get(name) {
19
+ return this.list.get(name)?.get();
20
+ }
21
+ /** 设置值 */
22
+ set(name, value) {
23
+ const valueConfig = this.list.get(name);
24
+ if (valueConfig?.set) {
25
+ valueConfig?.set(value);
26
+ return true;
27
+ }
28
+ else {
29
+ return false;
30
+ }
31
+ }
32
+ /** 获取注册的所有值 */
33
+ snapshot() {
34
+ const map = {};
35
+ this.list.forEach((config, name) => {
36
+ map[name] = config.get();
37
+ });
38
+ return map;
39
+ }
40
+ emit(name, value) {
41
+ this.main.emit("values:change", name, value);
42
+ this.main.emit(`change:${name}`, value);
43
+ }
44
+ watch(name, listener) {
45
+ this.main.on(`change:${name}`, listener);
46
+ }
47
+ unwatch(name, listener) {
48
+ this.main.off(`change:${name}`, listener);
49
+ }
50
+ }
51
+ exports.ModValues = ModValues;
@@ -0,0 +1,9 @@
1
+ import { LiveRoom } from "../core";
2
+ export interface FloatingCommandMap {
3
+ add: (platform: string, id: number, config?: boolean | Record<string, any>) => void;
4
+ remove: (key: string) => void;
5
+ open: (key: string) => void;
6
+ close: (key: string) => void;
7
+ update: (key: string) => void;
8
+ [name: `room.create.${string}`]: (id: string | number, config?: Record<string, any>) => LiveRoom;
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,34 @@
1
+ import { RoomStatus } from "../enums";
2
+ import { Message } from "./message";
3
+ import { RoomDetail, RoomInfo, RoomStatsInfo } from "./room";
4
+ export interface FloatingEventMap {
5
+ "live:message": (msg: Message.All) => void;
6
+ "live:raw": (data: any, { platform, room }: {
7
+ platform: string;
8
+ room: string | number;
9
+ }) => void;
10
+ "room:connect": (key: string) => void;
11
+ "room:disconnect": (key: string) => void;
12
+ "room:info": (key: string, info: RoomInfo) => void;
13
+ "room:detail": (key: string, info: RoomDetail) => void;
14
+ "room:status": (key: string, info: {
15
+ status: RoomStatus;
16
+ id?: string;
17
+ timestamp?: number;
18
+ }) => void;
19
+ "room:stats": (key: string, info: RoomStatsInfo) => void;
20
+ "room:open": (key: string, info: RoomInfo) => void;
21
+ "room:close": (key: string, info: RoomInfo) => void;
22
+ "room:add": (key: string, info: RoomInfo) => void;
23
+ "room:remove": (key: string) => void;
24
+ "plugin:add": (name: string) => void;
25
+ "plugin:remove": (name: string) => void;
26
+ "command:add": (name: string) => void;
27
+ "command:remove": (name: string) => void;
28
+ "store:add": (name: string, id: string, value: any) => void;
29
+ "store:remove": (name: string, id: string) => void;
30
+ "values:register": (name: string) => void;
31
+ "values:change": (name: string, value: any) => void;
32
+ [name: `change:${string}`]: (value: any) => void;
33
+ error: (err: Error) => void;
34
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ import { Message } from "./message";
2
+ export interface FloatingHookMap {
3
+ message: Message.All;
4
+ add: Record<string, any>;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,5 @@
1
1
  export * from "./message";
2
2
  export * from "./plugin";
3
3
  export * from "./room";
4
+ export * from "./command";
5
+ export * from "./events";
@@ -17,3 +17,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./message"), exports);
18
18
  __exportStar(require("./plugin"), exports);
19
19
  __exportStar(require("./room"), exports);
20
+ __exportStar(require("./command"), exports);
21
+ __exportStar(require("./events"), exports);
@@ -1,8 +1,15 @@
1
1
  import { FloatingLive } from "../live";
2
2
  /** 插件模块, 加载时调用 */
3
- export declare type FloatingLivePlugin<C extends object = object, E extends object | void = object | void> = () => {
3
+ export declare type FloatingLivePlugin<C extends object = object> = () => {
4
4
  /** 插件名称 */
5
5
  name: string;
6
- register: (ctx: FloatingLive, config?: C) => E;
6
+ register: (ctx: FloatingLive, config?: C) => void;
7
7
  destroy?: (ctx: FloatingLive) => void;
8
8
  };
9
+ export interface IPlugin {
10
+ destroy: () => void;
11
+ }
12
+ export interface PluginConstructor {
13
+ pluginName: string;
14
+ new (ctx: FloatingLive, config: any): IPlugin;
15
+ }
@@ -1,5 +1,15 @@
1
1
  import { RoomStatus } from "../enums";
2
2
  import { UserInfo } from "./message";
3
+ /** 房间配置 */
4
+ export interface RoomConfig {
5
+ /** 添加后是否打开房间 */
6
+ open?: boolean;
7
+ /** 口令 */
8
+ tokens?: Record<string, number>;
9
+ /** 凭据 */
10
+ credentials?: string;
11
+ }
12
+ /** 房间信息 */
3
13
  export interface RoomInfo {
4
14
  /** 平台id */
5
15
  platform: string;
@@ -49,10 +59,8 @@ export interface RoomStatsInfo {
49
59
  export interface PlatformInfo {
50
60
  /** 平台名称 */
51
61
  name: string;
52
- /** 平台id */
53
- id: string;
54
62
  /** 平台vip信息 */
55
- vip: {
63
+ vip?: {
56
64
  /** 平台vip对应id */
57
65
  id: string;
58
66
  /** 平台vip名称 */
@@ -61,7 +69,7 @@ export interface PlatformInfo {
61
69
  level?: string[];
62
70
  };
63
71
  /** 粉丝vip信息 */
64
- membership: {
72
+ membership?: {
65
73
  /** 粉丝vip对应id */
66
74
  id: string;
67
75
  /** 粉丝vip名称 */
@@ -70,16 +78,16 @@ export interface PlatformInfo {
70
78
  level?: string[];
71
79
  };
72
80
  /** 礼物信息 */
73
- gift: {
81
+ gift?: {
74
82
  action: string;
75
83
  };
76
84
  /** 货币信息 */
77
- currency: Record<string, {
85
+ currency?: Record<string, {
78
86
  /** 货币名称 */
79
87
  name: string;
80
88
  /** 1货币面值等值value (1面值/1数值) */
81
- face: number;
82
- /** 1人民币等值value (1.00CNY/1数值) (若为0则为免费货币) */
83
- cny?: number;
89
+ ratio: number;
90
+ /** 1单位消费现金等值value (1.00CNY/1数值) (若为0则为免费货币) */
91
+ cash?: number;
84
92
  }>;
85
93
  }
@@ -0,0 +1,4 @@
1
+ import { PlatformInfo } from "./room";
2
+ export interface FloatingStoreMap {
3
+ platform: PlatformInfo;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export interface FloatingValueMap {
2
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floating-live",
3
- "version": "0.15.0-beta.1",
3
+ "version": "0.15.0-beta.3",
4
4
  "type": "commonjs",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,29 +0,0 @@
1
- import { FloatingLive } from "../live";
2
- /** 插件可注册对象模块 */
3
- export declare class Reglist<T> {
4
- /** 功能列表 */
5
- private readonly list;
6
- /** 插件与功能名称绑定表 */
7
- private readonly pluginIdMap;
8
- private readonly name;
9
- protected readonly main: FloatingLive;
10
- constructor(main: FloatingLive, name: string);
11
- /** 注册一个对象 */
12
- register(id: string, item: T): void;
13
- /** 取消某一插件在该模块中的注册 */
14
- unregister(pluginName: string): void;
15
- /** 获取名称 */
16
- getName(): string;
17
- /** 根据id获取注册对象 */
18
- get(id: string): T | undefined;
19
- /** 检查注册对象是否存在 */
20
- has(id: string): boolean;
21
- /** 获取注册对象列表 */
22
- getList(): T[];
23
- /** 遍历 */
24
- forEach(callbackfn: (value: T, key: string) => void): void;
25
- /** 获取注册id列表 */
26
- getIdList(): string[];
27
- /** 获取注册id列表 */
28
- getIdByPlugin(name: string): string[];
29
- }
@@ -1,69 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Reglist = void 0;
4
- /** 插件可注册对象模块 */
5
- class Reglist {
6
- constructor(main, name) {
7
- this.main = main;
8
- this.name = name;
9
- this.list = new Map();
10
- this.pluginIdMap = new Map();
11
- }
12
- /** 注册一个对象 */
13
- register(id, item) {
14
- // 如果组件缺少id, 或者id与已安装插件重复, 则报错
15
- if (!id)
16
- throw new TypeError("id is required!");
17
- if (this.list.has(id))
18
- throw new TypeError(`${this.name} duplicate id: ${id}!`);
19
- // 将组件添加至列表中
20
- this.list.set(id, item);
21
- // 将插件名称与注册id关联
22
- if (this.main.plugin.currentPlugin) {
23
- if (this.pluginIdMap.has(this.main.plugin.currentPlugin)) {
24
- this.pluginIdMap.get(this.main.plugin.currentPlugin)?.push(id);
25
- }
26
- else {
27
- this.pluginIdMap.set(this.main.plugin.currentPlugin, [id]);
28
- }
29
- }
30
- }
31
- /** 取消某一插件在该模块中的注册 */
32
- unregister(pluginName) {
33
- if (this.pluginIdMap.has(pluginName)) {
34
- const handlerList = this.pluginIdMap.get(pluginName);
35
- handlerList?.forEach((plugin) => {
36
- this.list.delete(plugin);
37
- });
38
- }
39
- }
40
- /** 获取名称 */
41
- getName() {
42
- return this.name;
43
- }
44
- /** 根据id获取注册对象 */
45
- get(id) {
46
- return this.list.get(id);
47
- }
48
- /** 检查注册对象是否存在 */
49
- has(id) {
50
- return this.list.has(id);
51
- }
52
- /** 获取注册对象列表 */
53
- getList() {
54
- return [...this.list.values()];
55
- }
56
- /** 遍历 */
57
- forEach(callbackfn) {
58
- this.list.forEach(callbackfn);
59
- }
60
- /** 获取注册id列表 */
61
- getIdList() {
62
- return [...this.list.keys()];
63
- }
64
- /** 获取注册id列表 */
65
- getIdByPlugin(name) {
66
- return [...(this.pluginIdMap.get(name) || [])];
67
- }
68
- }
69
- exports.Reglist = Reglist;
@@ -1,9 +0,0 @@
1
- import { FloatingLive } from "../live";
2
- import { Reglist } from "../core/Reglist";
3
- export default class ModCommand extends Reglist<(...args: any[]) => any> {
4
- constructor(main: FloatingLive);
5
- /** 批量注册命令 */
6
- batchRegister(list: Record<string, (...args: any) => any>): void;
7
- /** 执行命令 */
8
- execute(name: string, ...args: any[]): any;
9
- }
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const Reglist_1 = require("../core/Reglist");
4
- class ModCommand extends Reglist_1.Reglist {
5
- constructor(main) {
6
- super(main, "command");
7
- }
8
- /** 批量注册命令 */
9
- batchRegister(list) {
10
- for (const key in list) {
11
- this.register(key, list[key]);
12
- }
13
- }
14
- /** 执行命令 */
15
- execute(name, ...args) {
16
- return this.get(name)?.(...args);
17
- }
18
- }
19
- exports.default = ModCommand;
@@ -1,9 +0,0 @@
1
- import { Message } from "../types/message";
2
- import { FloatingLive } from "../live";
3
- import { Reglist } from "../core/Reglist";
4
- export declare class ModMessage {
5
- main: FloatingLive;
6
- handler: Reglist<(msg: Message.All) => void>;
7
- constructor(main: FloatingLive);
8
- handle(msg: Message.All): void;
9
- }
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModMessage = void 0;
4
- const Reglist_1 = require("../core/Reglist");
5
- class ModMessage {
6
- constructor(main) {
7
- this.main = main;
8
- this.handler = new Reglist_1.Reglist(main, "message.handler");
9
- }
10
- handle(msg) {
11
- this.handler.getList().forEach((handler) => {
12
- handler(msg);
13
- });
14
- }
15
- }
16
- exports.ModMessage = ModMessage;
@@ -1,20 +0,0 @@
1
- import { FloatingLive } from "../live";
2
- import { FloatingLivePlugin } from "../types/plugin";
3
- export declare class ModPlugin<T extends FloatingLive> {
4
- private current;
5
- /** 插件列表 */
6
- private list;
7
- private pluginEvent;
8
- private main;
9
- private exports;
10
- constructor(main: T);
11
- /** 获取当前安装插件 */
12
- get currentPlugin(): string | null;
13
- addPluginEvent(eventName: string, listener: (...args: any[]) => void): void;
14
- /** 访问插件操作对象 */
15
- access(name: string): object | undefined;
16
- /** 注册插件 */
17
- register<C extends object, E extends void | object>(pluginFunc: FloatingLivePlugin<C, E>, pluginConfig?: C): {} | undefined;
18
- /** 移除插件 */
19
- unregister(name: string): void;
20
- }
@@ -1,12 +0,0 @@
1
- import { FloatingLive } from "../live";
2
- import { Reglist } from "../core/Reglist";
3
- export declare class ModState extends Reglist<() => object> {
4
- constructor(main: FloatingLive);
5
- generate(): Record<string, any>;
6
- /** 设置一个值 */
7
- set(name: string, value: any): void;
8
- /** 在数组中添加一个值 */
9
- arrayPush(name: string, value: any): void;
10
- /** 在数组中移除一个值 */
11
- arrayRemove(name: string, key: string, val: string): void;
12
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ModState = void 0;
4
- const Reglist_1 = require("../core/Reglist");
5
- class ModState extends Reglist_1.Reglist {
6
- constructor(main) {
7
- super(main, "state");
8
- }
9
- generate() {
10
- const state = {};
11
- this.forEach((gen, key) => {
12
- state[key] = gen();
13
- });
14
- return state;
15
- }
16
- /** 设置一个值 */
17
- set(name, value) {
18
- this.main.emit("state:set", name, value);
19
- }
20
- /** 在数组中添加一个值 */
21
- arrayPush(name, value) {
22
- this.main.emit("state:array_push", name, value);
23
- }
24
- /** 在数组中移除一个值 */
25
- arrayRemove(name, key, val) {
26
- this.main.emit("state:array_remove", name, key, val);
27
- }
28
- }
29
- exports.ModState = ModState;