floating-live 0.16.0-alpha.2 → 0.16.0-alpha.20
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/FloatingLive.d.ts +18 -0
- package/dist/FloatingLive.js +36 -1
- package/dist/app.d.ts +14 -7
- package/dist/app.js +33 -9
- package/dist/command/manager.d.ts +1 -1
- package/dist/command/manager.js +7 -5
- package/dist/command/types.d.ts +16 -0
- package/dist/error/index.d.ts +4 -2
- package/dist/error/types.d.ts +1 -0
- package/dist/event/types.d.ts +7 -0
- package/dist/hook/manager.d.ts +6 -9
- package/dist/hook/manager.js +12 -19
- package/dist/live/room.d.ts +67 -25
- package/dist/live/room.js +23 -21
- package/dist/plugin/context/CommonPluginContext.d.ts +10 -7
- package/dist/plugin/context/CommonPluginContext.js +36 -13
- package/dist/plugin/context/CorePluginContext.d.ts +8 -0
- package/dist/plugin/context/CorePluginContext.js +25 -0
- package/dist/plugin/context/SafePluginContext.d.ts +2 -2
- package/dist/plugin/index.d.ts +3 -0
- package/dist/plugin/index.js +3 -0
- package/dist/plugin/manager.d.ts +4 -10
- package/dist/plugin/manager.js +83 -21
- package/dist/plugin/types.d.ts +35 -10
- package/dist/plugins/room/index.d.ts +22 -6
- package/dist/plugins/room/index.js +65 -26
- package/dist/utils/EventEmitter.d.ts +2 -2
- package/dist/utils/EventEmitter.js +4 -4
- package/dist/value/manager.d.ts +6 -3
- package/dist/value/manager.js +15 -7
- package/package.json +1 -1
package/dist/FloatingLive.d.ts
CHANGED
|
@@ -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
|
}
|
package/dist/FloatingLive.js
CHANGED
|
@@ -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 =
|
|
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
|
@@ -2,7 +2,7 @@ import { CommandOptions, CommandCallOptions, AppCommandMap, CommandFunction } fr
|
|
|
2
2
|
import { AppError } from "./error";
|
|
3
3
|
import { AppEventDetailMap, AppEventEmitOptions, AppEventListener } from "./event";
|
|
4
4
|
import { AppHookMap, HookCallOptions, HookContext, HookFunction, HookUseOptions } from "./hook";
|
|
5
|
-
import { PluginContext,
|
|
5
|
+
import { AppPluginExposesMap, PluginConstructor, PluginContext, PluginInitOptions, PluginItem } from "./plugin";
|
|
6
6
|
import { CustomEventEmitter } from "./utils/EventEmitter";
|
|
7
7
|
import { AppValueMap, ValueOptions } from "./value";
|
|
8
8
|
export declare class App extends CustomEventEmitter implements PluginContext {
|
|
@@ -10,37 +10,44 @@ export declare class App extends CustomEventEmitter implements PluginContext {
|
|
|
10
10
|
private pluginManager;
|
|
11
11
|
private commandManager;
|
|
12
12
|
private valueManager;
|
|
13
|
+
private abortController;
|
|
13
14
|
constructor();
|
|
14
|
-
on<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]
|
|
15
|
-
once<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]
|
|
15
|
+
on<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>, signal?: AbortSignal): void;
|
|
16
|
+
once<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>, signal?: AbortSignal): void;
|
|
16
17
|
off<K extends keyof AppEventDetailMap>(type: K, listener: AppEventListener<AppEventDetailMap[K]>): void;
|
|
17
18
|
emit<K extends keyof AppEventDetailMap>(type: K, detail: AppEventDetailMap[K], options?: AppEventEmitOptions & EventInit): void;
|
|
18
19
|
/** 发送错误 */
|
|
19
20
|
throw(err?: Error): void;
|
|
20
21
|
error(id: string, detail?: Record<string, any>): AppError;
|
|
21
22
|
/** 注册插件 */
|
|
22
|
-
register(plugin:
|
|
23
|
+
register<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): Promise<P>;
|
|
24
|
+
/** 同步注册插件 */
|
|
25
|
+
registerSync<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): P;
|
|
23
26
|
/** 取消注册插件 */
|
|
24
27
|
unregister(pluginName: string): void;
|
|
25
28
|
getPlugin(pluginName: string): void;
|
|
26
|
-
whenRegister(pluginName:
|
|
29
|
+
whenRegister<K extends keyof AppPluginExposesMap>(pluginName: K, callback: (exposes: AppPluginExposesMap[K]) => (() => void) | void): void;
|
|
30
|
+
whenRegister(pluginName: string, callback: () => (() => void) | void): void;
|
|
27
31
|
getPluginExposes(pluginName: string): any;
|
|
28
32
|
hasPlugin(pluginName: string): boolean;
|
|
29
33
|
registerCommand<T extends keyof AppCommandMap>(name: T, func: CommandFunction<AppCommandMap[T]>, options?: CommandOptions): void;
|
|
30
34
|
unregisterCommand(name: string): void;
|
|
35
|
+
hasCommand(name: string): boolean;
|
|
31
36
|
call<T extends keyof AppCommandMap>(name: T, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
|
|
32
37
|
callWithOptions<T extends keyof AppCommandMap>(name: T, options: CommandCallOptions, ...args: Parameters<AppCommandMap[T]>): ReturnType<AppCommandMap[T]>;
|
|
33
38
|
useHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>, options?: HookUseOptions): void;
|
|
34
39
|
unuseHook<T extends keyof AppHookMap>(name: T, func: HookFunction<AppHookMap[T]>): void;
|
|
35
40
|
callHook<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): Promise<HookContext<AppHookMap[T]>>;
|
|
36
41
|
callHookSync<T extends keyof AppHookMap>(name: T, ctx: AppHookMap[T], options?: HookCallOptions): HookContext<AppHookMap[T]>;
|
|
37
|
-
registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>):
|
|
42
|
+
registerValue<K extends keyof AppValueMap>(name: K, options: ValueOptions<AppValueMap[K]>): import("./value").ValueContext<AppValueMap[K]>;
|
|
38
43
|
unregisterValue(name: string): void;
|
|
39
44
|
watch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
|
|
40
45
|
unwatch<K extends keyof AppValueMap>(name: K, watcher: (value: AppValueMap[K]) => void): void;
|
|
46
|
+
hasValue(name: string): boolean;
|
|
41
47
|
getValue<K extends keyof AppValueMap>(name: K): AppValueMap[K];
|
|
42
48
|
setValue(name: string, value: any): void;
|
|
43
49
|
destroy(): void;
|
|
44
50
|
/** 拓展值 */
|
|
45
|
-
define(): void;
|
|
51
|
+
define(key: string, value: ((...args: any[]) => any) | PropertyDescriptor): void;
|
|
52
|
+
get signal(): AbortSignal;
|
|
46
53
|
}
|
package/dist/app.js
CHANGED
|
@@ -7,16 +7,17 @@ import { ValueManager } from "./value/manager";
|
|
|
7
7
|
export class App extends CustomEventEmitter {
|
|
8
8
|
constructor() {
|
|
9
9
|
super();
|
|
10
|
+
this.abortController = new AbortController();
|
|
10
11
|
this.hookManager = new HookManager(this);
|
|
11
12
|
this.pluginManager = new PluginManager(this);
|
|
12
13
|
this.commandManager = new CommandManager(this);
|
|
13
14
|
this.valueManager = new ValueManager(this);
|
|
14
15
|
}
|
|
15
|
-
on(type, listener) {
|
|
16
|
-
super.on(type, listener);
|
|
16
|
+
on(type, listener, signal = this.signal) {
|
|
17
|
+
super.on(type, listener, signal);
|
|
17
18
|
}
|
|
18
|
-
once(type, listener) {
|
|
19
|
-
super.once(type, listener);
|
|
19
|
+
once(type, listener, signal = this.signal) {
|
|
20
|
+
super.once(type, listener, signal);
|
|
20
21
|
}
|
|
21
22
|
off(type, listener) {
|
|
22
23
|
super.off(type, listener);
|
|
@@ -24,6 +25,7 @@ export class App extends CustomEventEmitter {
|
|
|
24
25
|
emit(type, detail, options) {
|
|
25
26
|
const { source, remote } = options || {};
|
|
26
27
|
super.emit(type, { ...detail, source, remote }, options);
|
|
28
|
+
super.emit("event", { name: type, detail: { ...detail, source, remote } });
|
|
27
29
|
}
|
|
28
30
|
//--- 错误机制 ---//
|
|
29
31
|
/** 发送错误 */
|
|
@@ -36,7 +38,11 @@ export class App extends CustomEventEmitter {
|
|
|
36
38
|
//--- 插件机制 ---//
|
|
37
39
|
/** 注册插件 */
|
|
38
40
|
register(plugin, options) {
|
|
39
|
-
this.pluginManager.register(plugin, options);
|
|
41
|
+
return this.pluginManager.register(plugin, options);
|
|
42
|
+
}
|
|
43
|
+
/** 同步注册插件 */
|
|
44
|
+
registerSync(plugin, options) {
|
|
45
|
+
return this.pluginManager.registerSync(plugin, options);
|
|
40
46
|
}
|
|
41
47
|
/** 取消注册插件 */
|
|
42
48
|
unregister(pluginName) {
|
|
@@ -69,7 +75,7 @@ export class App extends CustomEventEmitter {
|
|
|
69
75
|
});
|
|
70
76
|
}
|
|
71
77
|
getPluginExposes(pluginName) {
|
|
72
|
-
this.pluginManager.getExposes(pluginName);
|
|
78
|
+
return this.pluginManager.getExposes(pluginName);
|
|
73
79
|
}
|
|
74
80
|
hasPlugin(pluginName) {
|
|
75
81
|
return this.pluginManager.has(pluginName);
|
|
@@ -81,6 +87,9 @@ export class App extends CustomEventEmitter {
|
|
|
81
87
|
unregisterCommand(name) {
|
|
82
88
|
this.commandManager.unregister(name);
|
|
83
89
|
}
|
|
90
|
+
hasCommand(name) {
|
|
91
|
+
return this.commandManager.has(name);
|
|
92
|
+
}
|
|
84
93
|
call(name, ...args) {
|
|
85
94
|
return this.commandManager.call(name, ...args);
|
|
86
95
|
}
|
|
@@ -102,7 +111,7 @@ export class App extends CustomEventEmitter {
|
|
|
102
111
|
}
|
|
103
112
|
//--- 值机制 ---//
|
|
104
113
|
registerValue(name, options) {
|
|
105
|
-
this.valueManager.register(name, options);
|
|
114
|
+
return this.valueManager.register(name, options);
|
|
106
115
|
}
|
|
107
116
|
unregisterValue(name) {
|
|
108
117
|
this.valueManager.unregister(name);
|
|
@@ -113,14 +122,29 @@ export class App extends CustomEventEmitter {
|
|
|
113
122
|
unwatch(name, watcher) {
|
|
114
123
|
this.valueManager.unwatch(name, watcher);
|
|
115
124
|
}
|
|
125
|
+
hasValue(name) {
|
|
126
|
+
return this.valueManager.has(name);
|
|
127
|
+
}
|
|
116
128
|
getValue(name) {
|
|
117
129
|
return this.valueManager.get(name);
|
|
118
130
|
}
|
|
119
131
|
setValue(name, value) {
|
|
120
132
|
this.valueManager.set(name, value);
|
|
121
133
|
}
|
|
122
|
-
destroy() {
|
|
134
|
+
destroy() {
|
|
135
|
+
this.abortController.abort();
|
|
136
|
+
}
|
|
123
137
|
//--- 其他 ---//
|
|
124
138
|
/** 拓展值 */
|
|
125
|
-
define() {
|
|
139
|
+
define(key, value) {
|
|
140
|
+
if (typeof value == "function") {
|
|
141
|
+
Object.defineProperty(this, key, { value });
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
Object.defineProperty(this, key, value);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
get signal() {
|
|
148
|
+
return this.abortController.signal;
|
|
149
|
+
}
|
|
126
150
|
}
|
package/dist/command/manager.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
42
|
+
getSnapshot() {
|
|
41
43
|
return [...this.list].map(([name, { pluginName }]) => ({
|
|
42
44
|
name,
|
|
43
45
|
pluginName,
|
package/dist/command/types.d.ts
CHANGED
|
@@ -22,4 +22,20 @@ 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: string;
|
|
29
|
+
}[];
|
|
30
|
+
/** 获取命令快照 */
|
|
31
|
+
"command.snapshot": () => {
|
|
32
|
+
name: string;
|
|
33
|
+
}[];
|
|
34
|
+
/** 获取hook快照 */
|
|
35
|
+
"hook.snapshot": () => {
|
|
36
|
+
name: string;
|
|
37
|
+
list: {
|
|
38
|
+
pluginName?: string;
|
|
39
|
+
}[];
|
|
40
|
+
}[];
|
|
25
41
|
}
|
package/dist/error/index.d.ts
CHANGED
|
@@ -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);
|
package/dist/error/types.d.ts
CHANGED
package/dist/event/types.d.ts
CHANGED
package/dist/hook/manager.d.ts
CHANGED
|
@@ -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
|
-
|
|
16
|
+
getSnapshot(): {
|
|
17
|
+
name: string;
|
|
18
|
+
list: {
|
|
19
|
+
pluginName?: string;
|
|
20
|
+
}[];
|
|
21
|
+
}[];
|
|
25
22
|
}
|
package/dist/hook/manager.js
CHANGED
|
@@ -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
|
|
18
|
+
throw new AppError("hook:use_duplicate", {
|
|
24
19
|
message: `钩子函数挂载失败: ${name}`,
|
|
25
20
|
cause: "重复挂载同一函数",
|
|
26
|
-
|
|
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
|
-
|
|
35
|
-
|
|
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
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
map
|
|
77
|
-
});
|
|
66
|
+
getSnapshot() {
|
|
67
|
+
return [...this.list].map(([name, fnList]) => ({
|
|
68
|
+
name,
|
|
69
|
+
list: fnList.map(({ pluginName }) => ({ pluginName })),
|
|
70
|
+
}));
|
|
78
71
|
}
|
|
79
72
|
}
|
package/dist/live/room.d.ts
CHANGED
|
@@ -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
|
+
/** 直播间客户端API */
|
|
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
|
-
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
|
35
|
-
/**
|
|
36
|
-
|
|
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
|
-
|
|
46
|
-
|
|
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
|
-
|
|
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:
|
|
127
|
+
readonly off: 0;
|
|
116
128
|
/** 连接中 */
|
|
117
|
-
connecting:
|
|
129
|
+
readonly connecting: 1;
|
|
118
130
|
/** 连接到服务器 */
|
|
119
|
-
connected:
|
|
131
|
+
readonly connected: 2;
|
|
120
132
|
/** 连接到房间 */
|
|
121
|
-
entered:
|
|
133
|
+
readonly entered: 3;
|
|
122
134
|
/** 连接失败或断开 */
|
|
123
|
-
disconnected:
|
|
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
|
-
|
|
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
|
+
/** 直播间客户端API */
|
|
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.
|
|
11
|
-
/**
|
|
12
|
-
this.
|
|
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
|
-
|
|
75
|
+
openStatus: this.openStatus,
|
|
71
76
|
opened: this.opened,
|
|
72
|
-
|
|
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
|
+
};
|