floating-live 0.16.0-alpha.9 → 0.16.0-beta.2

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
@@ -1,16 +1,20 @@
1
1
  import { CommandOptions, CommandCallOptions, AppCommandMap, CommandFunction } from "./command";
2
+ import { CommandManager } from "./command/manager";
2
3
  import { AppError } from "./error";
3
4
  import { AppEventDetailMap, AppEventEmitOptions, AppEventListener } from "./event";
4
5
  import { AppHookMap, HookCallOptions, HookContext, HookFunction, HookUseOptions } from "./hook";
5
- import { AppPluginExposesMap, PluginContext, PluginInitOptions } from "./plugin";
6
+ import { HookManager } from "./hook/manager";
7
+ import { AppPluginExposesMap, PluginConstructor, PluginContext, PluginInitOptions, PluginItem } from "./plugin";
8
+ import { PluginManager } from "./plugin/manager";
6
9
  import { CustomEventEmitter } from "./utils/EventEmitter";
7
10
  import { AppValueMap, ValueOptions } from "./value";
11
+ import { ValueManager } from "./value/manager";
8
12
  export declare class App extends CustomEventEmitter implements PluginContext {
9
- private hookManager;
10
- private pluginManager;
11
- private commandManager;
12
- private valueManager;
13
- private abortController;
13
+ protected hookManager: HookManager;
14
+ protected pluginManager: PluginManager;
15
+ protected commandManager: CommandManager;
16
+ protected valueManager: ValueManager;
17
+ protected abortController: AbortController;
14
18
  constructor();
15
19
  on<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>, signal?: AbortSignal): void;
16
20
  once<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>, signal?: AbortSignal): void;
@@ -20,7 +24,9 @@ export declare class App extends CustomEventEmitter implements PluginContext {
20
24
  throw(err?: Error): void;
21
25
  error(id: string, detail?: Record<string, any>): AppError;
22
26
  /** 注册插件 */
23
- register(plugin: any, options?: PluginInitOptions): void;
27
+ register<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): Promise<P>;
28
+ /** 同步注册插件 */
29
+ registerSync<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): P;
24
30
  /** 取消注册插件 */
25
31
  unregister(pluginName: string): void;
26
32
  getPlugin(pluginName: string): void;
@@ -30,20 +36,22 @@ export declare class App extends CustomEventEmitter implements PluginContext {
30
36
  hasPlugin(pluginName: string): boolean;
31
37
  registerCommand<T extends keyof AppCommandMap>(name: T, func: CommandFunction<AppCommandMap[T]>, options?: CommandOptions): void;
32
38
  unregisterCommand(name: string): void;
39
+ hasCommand(name: string): boolean;
33
40
  call<T extends keyof AppCommandMap>(name: T, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
34
41
  callWithOptions<T extends keyof AppCommandMap>(name: T, options: CommandCallOptions, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
35
42
  useHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>, options?: HookUseOptions): void;
36
43
  unuseHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>): void;
37
44
  callHook<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): Promise<HookContext<AppHookMap[T]>>;
38
45
  callHookSync<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): HookContext<AppHookMap[T]>;
39
- registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): void;
46
+ registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): import("./value").ValueContext<AppValueMap[K]>;
40
47
  unregisterValue(name: string): void;
41
48
  watch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
42
49
  unwatch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
50
+ hasValue(name: string): boolean;
43
51
  getValue<K extends keyof AppValueMap>(name: K): AppValueMap[K];
44
- setValue(name: string, value: any): void;
52
+ setValue(name: string, value: any): boolean;
45
53
  destroy(): void;
46
54
  /** 拓展值 */
47
- define(): void;
55
+ define(key: string, value: ((...args: any[]) => any) | PropertyDescriptor): void;
48
56
  get signal(): AbortSignal;
49
57
  }
package/dist/app.js CHANGED
@@ -8,10 +8,13 @@ export class App extends CustomEventEmitter {
8
8
  constructor() {
9
9
  super();
10
10
  this.abortController = new AbortController();
11
- this.hookManager = new HookManager(this);
12
- this.pluginManager = new PluginManager(this);
13
11
  this.commandManager = new CommandManager(this);
12
+ this.hookManager = new HookManager(this);
14
13
  this.valueManager = new ValueManager(this);
14
+ this.pluginManager = new PluginManager(this);
15
+ // 初始化命令
16
+ this.registerCommand("get", (e, name) => this.getValue(name));
17
+ this.registerCommand("set", (e, name, value) => this.setValue(name, value));
15
18
  }
16
19
  on(type, listener, signal = this.signal) {
17
20
  super.on(type, listener, signal);
@@ -25,6 +28,7 @@ export class App extends CustomEventEmitter {
25
28
  emit(type, detail, options) {
26
29
  const { source, remote } = options || {};
27
30
  super.emit(type, { ...detail, source, remote }, options);
31
+ super.emit("event", { name: type, detail: { ...detail, source, remote } });
28
32
  }
29
33
  //--- 错误机制 ---//
30
34
  /** 发送错误 */
@@ -37,7 +41,11 @@ export class App extends CustomEventEmitter {
37
41
  //--- 插件机制 ---//
38
42
  /** 注册插件 */
39
43
  register(plugin, options) {
40
- this.pluginManager.register(plugin, options);
44
+ return this.pluginManager.register(plugin, options);
45
+ }
46
+ /** 同步注册插件 */
47
+ registerSync(plugin, options) {
48
+ return this.pluginManager.registerSync(plugin, options);
41
49
  }
42
50
  /** 取消注册插件 */
43
51
  unregister(pluginName) {
@@ -70,7 +78,7 @@ export class App extends CustomEventEmitter {
70
78
  });
71
79
  }
72
80
  getPluginExposes(pluginName) {
73
- this.pluginManager.getExposes(pluginName);
81
+ return this.pluginManager.getExposes(pluginName);
74
82
  }
75
83
  hasPlugin(pluginName) {
76
84
  return this.pluginManager.has(pluginName);
@@ -82,6 +90,9 @@ export class App extends CustomEventEmitter {
82
90
  unregisterCommand(name) {
83
91
  this.commandManager.unregister(name);
84
92
  }
93
+ hasCommand(name) {
94
+ return this.commandManager.has(name);
95
+ }
85
96
  call(name, ...args) {
86
97
  return this.commandManager.call(name, ...args);
87
98
  }
@@ -103,7 +114,7 @@ export class App extends CustomEventEmitter {
103
114
  }
104
115
  //--- 值机制 ---//
105
116
  registerValue(name, options) {
106
- this.valueManager.register(name, options);
117
+ return this.valueManager.register(name, options);
107
118
  }
108
119
  unregisterValue(name) {
109
120
  this.valueManager.unregister(name);
@@ -114,18 +125,28 @@ export class App extends CustomEventEmitter {
114
125
  unwatch(name, watcher) {
115
126
  this.valueManager.unwatch(name, watcher);
116
127
  }
128
+ hasValue(name) {
129
+ return this.valueManager.has(name);
130
+ }
117
131
  getValue(name) {
118
132
  return this.valueManager.get(name);
119
133
  }
120
134
  setValue(name, value) {
121
- this.valueManager.set(name, value);
135
+ return this.valueManager.set(name, value);
122
136
  }
123
137
  destroy() {
124
138
  this.abortController.abort();
125
139
  }
126
140
  //--- 其他 ---//
127
141
  /** 拓展值 */
128
- define() { }
142
+ define(key, value) {
143
+ if (typeof value == "function") {
144
+ Object.defineProperty(this, key, { value });
145
+ }
146
+ else {
147
+ Object.defineProperty(this, key, value);
148
+ }
149
+ }
129
150
  get signal() {
130
151
  return this.abortController.signal;
131
152
  }
@@ -16,7 +16,7 @@ export declare class CommandManager {
16
16
  /** 检测命令是否存在 */
17
17
  has(name: string): boolean;
18
18
  /** 获取数据 */
19
- getData(): {
19
+ getSnapshot(): {
20
20
  name: string;
21
21
  pluginName: string | undefined;
22
22
  }[];
@@ -1,8 +1,10 @@
1
+ import { AppError } from "../error";
1
2
  export class CommandManager {
2
3
  constructor(app) {
3
4
  /** 功能列表 */
4
5
  this.list = new Map();
5
6
  this.app = app;
7
+ app.registerCommand("command.snapshot", () => this.getSnapshot());
6
8
  }
7
9
  /** 注册命令 */
8
10
  register(name, call, options) {
@@ -23,10 +25,10 @@ export class CommandManager {
23
25
  callWithOptions(name, options, ...args) {
24
26
  const command = this.list.get(name);
25
27
  if (!command) {
26
- throw {
27
- message: "命令不存在",
28
- id: "command:call_unexist",
29
- };
28
+ throw new AppError("command:call_unexist", {
29
+ message: `命令不存在: ${name}`,
30
+ target: `command/${name}`,
31
+ });
30
32
  }
31
33
  else {
32
34
  return command.call({ source: options.source, client: options.client }, ...args);
@@ -37,7 +39,7 @@ export class CommandManager {
37
39
  return this.list.has(name);
38
40
  }
39
41
  /** 获取数据 */
40
- getData() {
42
+ getSnapshot() {
41
43
  return [...this.list].map(([name, { pluginName }]) => ({
42
44
  name,
43
45
  pluginName,
@@ -22,4 +22,24 @@ export interface CommandItem {
22
22
  export interface AppCommandMap {
23
23
  get: <T extends keyof AppValueMap>(name: T) => AppValueMap[T];
24
24
  set: <T extends keyof AppValueMap>(name: T, value: AppValueMap[T]) => boolean;
25
+ /** 获取注册值快照 */
26
+ "value.snapshot": () => {
27
+ name: string;
28
+ value: any;
29
+ }[];
30
+ /** 获取命令快照 */
31
+ "command.snapshot": () => {
32
+ name: string;
33
+ }[];
34
+ /** 获取插件快照 */
35
+ "plugin.snapshot": () => {
36
+ pluginName: string;
37
+ }[];
38
+ /** 获取hook快照 */
39
+ "hook.snapshot": () => {
40
+ name: string;
41
+ list: {
42
+ pluginName?: string;
43
+ }[];
44
+ }[];
25
45
  }
@@ -1,10 +1,12 @@
1
1
  import { ErrorOptions } from "./types";
2
2
  export * from "./types";
3
3
  export declare class AppError extends Error {
4
- /** 错误来源 */
5
- source?: string;
6
4
  /** 错误id */
7
5
  id: string;
6
+ /** 错误来源 */
7
+ source?: string;
8
+ /** 错误目标 */
9
+ target?: string;
8
10
  /** 错误原因 */
9
11
  cause?: Error | string;
10
12
  constructor(id: string, options: ErrorOptions);
@@ -2,5 +2,6 @@ export interface ErrorOptions {
2
2
  message?: string;
3
3
  cause?: string | Error;
4
4
  source?: string;
5
+ target?: string;
5
6
  [key: string]: any;
6
7
  }
@@ -36,6 +36,13 @@ export interface AppEventDetailMap {
36
36
  "value:unregister": {
37
37
  name: string;
38
38
  };
39
+ event: {
40
+ name: string;
41
+ detail: AppEventDetail<any>;
42
+ };
43
+ error: {
44
+ error: Error;
45
+ };
39
46
  [name: `change:${string}`]: {
40
47
  value: any;
41
48
  };
@@ -1,13 +1,5 @@
1
1
  import { App } from "../app";
2
- import { AppError, ErrorOptions } from "../error";
3
2
  import { HookFunction, HookUseOptions } from "./types";
4
- export interface HookErrorOptions extends ErrorOptions {
5
- hookName: string;
6
- }
7
- export declare class HookError extends AppError {
8
- hookName: string;
9
- constructor(id: string, options: HookErrorOptions);
10
- }
11
3
  export declare class HookManager {
12
4
  private list;
13
5
  private pluginMap;
@@ -21,5 +13,10 @@ export declare class HookManager {
21
13
  call(name: string, ctx: any): Promise<any>;
22
14
  /** 同步调用钩子函数 */
23
15
  callSync(name: string, ctx: any): any;
24
- getData(): void[];
16
+ getSnapshot(): {
17
+ name: string;
18
+ list: {
19
+ pluginName?: string;
20
+ }[];
21
+ }[];
25
22
  }
@@ -1,15 +1,10 @@
1
1
  import { AppError } from "../error";
2
- export class HookError extends AppError {
3
- constructor(id, options) {
4
- super(id, options);
5
- this.hookName = options.hookName;
6
- }
7
- }
8
2
  export class HookManager {
9
3
  constructor(app) {
10
4
  this.list = new Map();
11
5
  this.pluginMap = new Map();
12
6
  this.app = app;
7
+ app.registerCommand("hook.snapshot", () => this.getSnapshot());
13
8
  }
14
9
  /** 挂载钩子函数 */
15
10
  use(name, call, options) {
@@ -20,10 +15,10 @@ export class HookManager {
20
15
  this.list.set(name, hooks);
21
16
  }
22
17
  else if (hooks.includes(call)) {
23
- throw new HookError("hook:use_duplicate", {
18
+ throw new AppError("hook:use_duplicate", {
24
19
  message: `钩子函数挂载失败: ${name}`,
25
20
  cause: "重复挂载同一函数",
26
- hookName: name,
21
+ target: `hook/${name}`,
27
22
  });
28
23
  }
29
24
  hooks.push({ call, pluginName });
@@ -31,15 +26,13 @@ export class HookManager {
31
26
  /** 取消挂载钩子函数 */
32
27
  unuse(name, call) {
33
28
  let hooks = this.list.get(name);
34
- if (!hooks)
35
- throw new HookError("hook:unuse_unexisted", {
29
+ const index = hooks?.findIndex((item) => item.call == call) ?? -1;
30
+ if (!hooks || index < 0)
31
+ throw new AppError("hook:unuse_unexist", {
36
32
  message: `钩子函数卸载失败: ${name}`,
37
33
  cause: "卸载不存在的钩子函数",
38
- hookName: name,
34
+ target: `hook/${name}`,
39
35
  });
40
- const index = hooks.findIndex((item) => item.call == call);
41
- if (index < 0)
42
- throw {};
43
36
  hooks.splice(index, 1);
44
37
  }
45
38
  /** 调用钩子函数 */
@@ -70,10 +63,10 @@ export class HookManager {
70
63
  }
71
64
  return ctx;
72
65
  }
73
- getData() {
74
- const map = {};
75
- return [...this.list].map(([name, fnList]) => {
76
- map[name] = fnList.length;
77
- });
66
+ getSnapshot() {
67
+ return [...this.list].map(([name, fnList]) => ({
68
+ name,
69
+ list: fnList.map(({ pluginName }) => ({ pluginName })),
70
+ }));
78
71
  }
79
72
  }
@@ -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
+ /** 直播间信息流监听类 */
5
6
  export declare abstract class LiveRoom extends CustomEventEmitter implements LiveRoomData {
6
7
  constructor();
7
8
  /** 平台 */
@@ -20,30 +21,39 @@ export declare abstract class LiveRoom extends CustomEventEmitter implements Liv
20
21
  timestamp: number;
21
22
  /** 当前直播id */
22
23
  liveId?: string;
23
- /** 房间是否打开 */
24
- opened: boolean;
25
- /** 是否连接上房间 */
26
- connection: LiveConnectionStatus;
27
- /** 房间是否可用 */
28
- abstract available: boolean;
24
+ /** 房间连接状态 */
25
+ connectionStatus: LiveConnectionStatus;
26
+ /** 房间开启状态 */
27
+ openStatus: LiveRoomOpenStatus;
28
+ get opened(): boolean;
29
+ available: boolean;
29
30
  /** 打开连接 */
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 reconnect?(): void;
36
+ /** 更新房间数据 */
37
+ abstract update(): Promise<LiveRoomData>;
38
+ /** 从服务器获取房间数据 */
39
+ abstract fetchData?(): Promise<LiveRoomData>;
40
+ /** 设置房间数据 */
41
+ abstract setData?(data: LiveRoomData): LiveRoomData;
37
42
  /** 销毁 */
38
43
  destroy(): void;
44
+ /** 销毁事件 */
45
+ protected onDestroy?: () => void;
46
+ /** 发送消息 */
39
47
  protected emitMessage(msg: LiveMessage.All): void;
40
48
  /** 发送源消息 */
41
49
  protected emitRaw(msg: any): void;
42
- /** 获取数据 */
50
+ /** 生成房间数据 */
43
51
  toData(): LiveRoomData;
44
52
  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;
53
+ }
54
+ export interface LiveRoom {
55
+ on<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void, signal?: AbortSignal): void;
56
+ once<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void, signal?: AbortSignal): void;
47
57
  off<K extends keyof LiveRoomEventMap>(type: K, listener: (e: LiveRoomEventMap[K]) => void): void;
48
58
  emit<K extends keyof LiveRoomEventMap>(type: LiveRoomEventMap[K] extends Record<string, never> ? K : never): void;
49
59
  emit<K extends keyof LiveRoomEventMap>(type: K, detail: LiveRoomEventMap[K]): void;
@@ -68,12 +78,14 @@ export interface LiveRoomData {
68
78
  liveId?: string;
69
79
  /** 直播状态 */
70
80
  status: LiveRoomStatus;
71
- /** 直播间是否可用 */
72
- available: boolean;
73
- /** 房间监听是否打开 */
74
- opened: boolean;
75
81
  /** 房间连接状态 */
76
- connection: LiveConnectionStatus;
82
+ connectionStatus: LiveConnectionStatus;
83
+ /** 房间监听状态 */
84
+ openStatus: LiveRoomOpenStatus;
85
+ /** 房间是否开启 */
86
+ opened: boolean;
87
+ /** 房间是否可用 */
88
+ available: boolean;
77
89
  }
78
90
  /** 房间展示信息 */
79
91
  export interface LiveRoomDetailInfo {
@@ -112,42 +124,72 @@ export type LiveRoomStatus = EnumValue<typeof LiveRoomStatus>;
112
124
  /** 直播间状态 */
113
125
  export declare const LiveConnectionStatus: {
114
126
  /** 未连接 */
115
- off: number;
127
+ readonly off: 0;
116
128
  /** 连接中 */
117
- connecting: number;
129
+ readonly connecting: 1;
118
130
  /** 连接到服务器 */
119
- connected: number;
131
+ readonly connected: 2;
120
132
  /** 连接到房间 */
121
- entered: number;
133
+ readonly entered: 3;
122
134
  /** 连接失败或断开 */
123
- disconnected: number;
135
+ readonly disconnected: -1;
124
136
  };
125
137
  export type LiveConnectionStatus = EnumValue<typeof LiveConnectionStatus>;
138
+ /** 直播间开启状态 */
139
+ export declare const LiveRoomOpenStatus: {
140
+ /** 已关闭 */
141
+ readonly closed: 0;
142
+ /** 正在开启 */
143
+ readonly opening: 1;
144
+ /** 已开启 */
145
+ readonly opened: 2;
146
+ };
147
+ export type LiveRoomOpenStatus = EnumValue<typeof LiveRoomOpenStatus>;
126
148
  export interface LiveRoomEventMap {
149
+ /** */
127
150
  status: {
128
151
  status: LiveRoomStatus;
129
152
  timestamp: number;
130
153
  liveId?: string | number;
131
154
  };
132
- update: {};
155
+ /** 房间数据更新 */
156
+ update: {
157
+ room: LiveRoomData;
158
+ };
159
+ /** 细节改变 */
133
160
  detail: {
134
161
  detail: Partial<LiveRoomDetailInfo>;
135
162
  };
163
+ /** 统计信息改变 */
136
164
  stats: {
137
- stats: LiveRoomStatsInfo;
165
+ stats: Partial<LiveRoomStatsInfo>;
138
166
  };
167
+ /** 消息 */
139
168
  message: {
140
169
  message: LiveMessage.All;
141
170
  };
171
+ /** 消息源数据 */
142
172
  raw: {
143
173
  platform: string;
144
174
  roomId: string | number;
145
175
  data: any;
146
176
  };
177
+ /** 初始化 */
178
+ init: {};
179
+ /** 正在连接 */
147
180
  connecting: {};
181
+ /** 连接到服务器 */
148
182
  connected: {};
183
+ /** 断开连接 */
149
184
  disconnect: {};
185
+ /** 连接到房间 */
150
186
  enter: {};
187
+ /** 连接超时 */
188
+ timeout: {
189
+ time: number;
190
+ };
191
+ /** 开启直播间连接 */
151
192
  open: {};
193
+ /** 关闭直播间连接 */
152
194
  close: {};
153
195
  }
package/dist/live/room.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CustomEventEmitter } from "../utils/EventEmitter";
2
+ /** 直播间信息流监听类 */
2
3
  export class LiveRoom extends CustomEventEmitter {
3
4
  constructor() {
4
5
  super();
@@ -6,17 +7,21 @@ export class LiveRoom extends CustomEventEmitter {
6
7
  this.status = LiveRoomStatus.off;
7
8
  /** 时间戳 */
8
9
  this.timestamp = 0;
9
- /** 房间是否打开 */
10
- this.opened = false;
11
- /** 是否连接上房间 */
12
- this.connection = LiveConnectionStatus.off;
10
+ /** 房间连接状态 */
11
+ this.connectionStatus = LiveConnectionStatus.off;
12
+ /** 房间开启状态 */
13
+ this.openStatus = 0;
14
+ this.available = false;
15
+ }
16
+ get opened() {
17
+ return this.openStatus == 2;
13
18
  }
14
19
  /** 销毁 */
15
20
  destroy() {
16
21
  this.close();
17
22
  this.onDestroy?.();
18
23
  }
19
- // 发送消息
24
+ /** 发送消息 */
20
25
  emitMessage(msg) {
21
26
  // 其他消息 => 先更改状态再发送消息
22
27
  // 直播结束消息 => 先发送消息再更改状态
@@ -56,7 +61,7 @@ export class LiveRoom extends CustomEventEmitter {
56
61
  emitRaw(msg) {
57
62
  this.emit("raw", { platform: this.platform, roomId: this.id, data: msg });
58
63
  }
59
- /** 获取数据 */
64
+ /** 生成房间数据 */
60
65
  toData() {
61
66
  return {
62
67
  platform: this.platform,
@@ -67,28 +72,16 @@ export class LiveRoom extends CustomEventEmitter {
67
72
  timestamp: this.timestamp,
68
73
  status: this.status,
69
74
  liveId: this.liveId,
70
- available: this.available,
75
+ openStatus: this.openStatus,
71
76
  opened: this.opened,
72
- connection: this.connection,
77
+ available: this.available,
78
+ connectionStatus: this.connectionStatus,
73
79
  key: this.key,
74
80
  };
75
81
  }
76
82
  get key() {
77
83
  return `${this.platform}:${this.id}`;
78
84
  }
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
85
  }
93
86
  /** 直播间状态 */
94
87
  export const LiveRoomStatus = {
@@ -116,3 +109,12 @@ export const LiveConnectionStatus = {
116
109
  /** 连接失败或断开 */
117
110
  disconnected: -1,
118
111
  };
112
+ /** 直播间开启状态 */
113
+ export const LiveRoomOpenStatus = {
114
+ /** 已关闭 */
115
+ closed: 0,
116
+ /** 正在开启 */
117
+ opening: 1,
118
+ /** 已开启 */
119
+ opened: 2,
120
+ };