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.
@@ -2,10 +2,12 @@ import { AppError } from "../../error";
2
2
  import { BasePlugin } from "../../plugin";
3
3
  import { bindCommand } from "../../utils";
4
4
  export class Room extends BasePlugin {
5
- constructor(ctx) {
6
- super(ctx);
5
+ constructor() {
6
+ super(...arguments);
7
7
  this.map = new Map();
8
- this.symbolRoomEventHandlers = Symbol("room.roomEventHandlers");
8
+ this.symbolAbortController = Symbol("room.abortController");
9
+ }
10
+ init(ctx) {
9
11
  ctx.registerCommand("add", bindCommand(this.add, this));
10
12
  ctx.registerCommand("remove", bindCommand(this.remove, this));
11
13
  ctx.registerCommand("open", bindCommand(this.open, this));
@@ -13,21 +15,34 @@ export class Room extends BasePlugin {
13
15
  ctx.registerCommand("update", bindCommand(this.update, this));
14
16
  ctx.registerCommand("room.snapshot", bindCommand(this.getSnapshot, this));
15
17
  }
18
+ expose() {
19
+ return {
20
+ add: this.add.bind(this),
21
+ remove: this.remove.bind(this),
22
+ get: this.get.bind(this),
23
+ has: this.has.bind(this),
24
+ data: this.data.bind(this),
25
+ update: this.update.bind(this),
26
+ open: this.open.bind(this),
27
+ close: this.close.bind(this),
28
+ getList: this.getList.bind(this),
29
+ };
30
+ }
16
31
  /** 添加房间监听实例 */
17
- attachRoom(room, open) {
32
+ attach(room, open) {
18
33
  let key = room.key;
19
34
  if (this.map.has(key)) {
20
- this.ctx.throw(new AppError("room:add_exist", {
35
+ this.throw(new AppError("room:add_exist", {
21
36
  message: "房间已存在",
22
37
  }));
23
38
  return;
24
39
  }
25
40
  this.map.set(key, room);
26
- const eventHandlers = [];
27
- room[this.symbolRoomEventHandlers] = eventHandlers;
41
+ const abortController = new AbortController();
42
+ room[this.symbolAbortController] =
43
+ abortController;
28
44
  const setHandler = (type, listener) => {
29
- room.on(type, listener);
30
- eventHandlers.push([type, listener]);
45
+ room.on(type, listener, abortController.signal);
31
46
  };
32
47
  // 添加监听事件
33
48
  // 直播消息
@@ -92,29 +107,37 @@ export class Room extends BasePlugin {
92
107
  const opt = typeof options == "boolean" ? { open: options } : options || {};
93
108
  const ctx = { platform, id, options: opt };
94
109
  const res = await this.ctx.callHook("room.add", ctx);
95
- if (!res.defaultPrevented) {
96
- this.ctx.throw(this.ctx.error("room:add_fail", { message: "无法添加房间" }));
110
+ if (res.defaultPrevented) {
111
+ this.throw(new this.Error("room:add_hook_prevented", {
112
+ message: "无法添加房间",
113
+ cause: "房间添加被钩子函数阻止",
114
+ target: `room/${platform}:${id}`,
115
+ }));
97
116
  }
98
- const room = await this.ctx.call(`${platform}.room.create`, id, opt);
99
- if (!room) {
100
- this.ctx.throw(this.ctx.error("room:create_fail", { message: "无法创建房间" }));
117
+ let room;
118
+ try {
119
+ room = await this.ctx.call(`${platform}.room.create`, id, opt);
101
120
  }
102
- else {
103
- this.attachRoom(room);
121
+ catch (e) {
122
+ this.throw(new this.Error("room:create_fail", {
123
+ message: "无法创建房间",
124
+ cause: e,
125
+ target: `room/${platform}:${id}`,
126
+ }));
104
127
  }
128
+ this.attach(room);
105
129
  }
106
130
  /** 移除房间 */
107
131
  remove(key) {
108
132
  let room = this.map.get(key);
109
133
  if (!room) {
110
- this.ctx.throw(this.ctx.error("room:remove_unexist", {
134
+ this.throw(new this.Error("room:remove_unexist", {
111
135
  message: "房间不存在",
136
+ target: `room/${key}`,
112
137
  }));
113
138
  }
114
139
  this.map.delete(key); // 从表中删除房间
115
- room[this.symbolRoomEventHandlers].forEach(([type, listener]) => {
116
- room?.removeEventListener(type, listener);
117
- }); // 移除房间监听实例
140
+ room[this.symbolAbortController]?.abort(); // 移除房间监听实例
118
141
  this.ctx.emit("room:remove", { key });
119
142
  }
120
143
  /** 获取房间 */
@@ -125,14 +148,14 @@ export class Room extends BasePlugin {
125
148
  return this.map.has(key);
126
149
  }
127
150
  /** 获取房间信息 */
128
- info(key) {
151
+ data(key) {
129
152
  let room = this.map.get(key);
130
153
  return room ? room.toData() : undefined;
131
154
  }
132
155
  /** 更新房间信息 */
133
156
  update(key) {
134
157
  let room = this.map.get(key);
135
- room && room.getInfo();
158
+ room && room.update();
136
159
  }
137
160
  /** 获取房间key列表 */
138
161
  get keys() {
@@ -145,19 +168,35 @@ export class Room extends BasePlugin {
145
168
  /** 打开房间连接 */
146
169
  open(key) {
147
170
  let room = this.map.get(key);
148
- room?.available && !room.opened && room.open();
171
+ if (!room) {
172
+ this.throw(new this.Error("room:open_unexist", {
173
+ message: "打开的房间不存在",
174
+ target: `room/${key}`,
175
+ }));
176
+ }
177
+ room.open();
149
178
  }
150
179
  /** 关闭房间连接 */
151
180
  close(key) {
152
181
  let room = this.map.get(key);
153
- room?.opened && room.close();
182
+ if (!room) {
183
+ this.throw(new this.Error("room:close_unexist", {
184
+ message: "关闭的房间不存在",
185
+ target: `room/${key}`,
186
+ }));
187
+ }
188
+ room.close();
154
189
  }
155
190
  /** 更改某个房间的顺序 */
156
191
  move(key, position) {
157
192
  const arr = [...this.map];
158
193
  const index = arr.findIndex(([k]) => k == key);
159
- if (index == -1)
160
- return;
194
+ if (index == -1) {
195
+ this.throw(new this.Error("room:move_unexist", {
196
+ message: "要移动的房间不存在",
197
+ target: `room/${key}`,
198
+ }));
199
+ }
161
200
  const [item] = arr.splice(index, 1);
162
201
  arr.splice(position, 0, item);
163
202
  this.map = new Map(arr);
@@ -1,11 +1,11 @@
1
1
  import { App } from "../app";
2
- import { ValueAccessOptions, ValueOptions } from "./types";
2
+ import { ValueAccessOptions, ValueContext, ValueOptions } from "./types";
3
3
  export declare class ValueManager {
4
4
  private list;
5
5
  protected readonly app: App;
6
6
  constructor(app: App);
7
7
  /** 注册值 */
8
- register<T>(name: string, { get, set, pluginName }: ValueOptions<T>): void;
8
+ register<T>(name: string, { get, set, pluginName }: ValueOptions<T>): ValueContext<T>;
9
9
  /** 取消注册 */
10
10
  unregister(name: string): void;
11
11
  /** 获取值 */
@@ -20,5 +20,8 @@ export declare class ValueManager {
20
20
  watch(name: string, listener: (value: any) => void): void;
21
21
  unwatch(name: string, listener: (value: any) => void): void;
22
22
  /** 获取注册的所有值 */
23
- getData(): Record<string, any>;
23
+ getSnapshot(): {
24
+ name: string;
25
+ value: string;
26
+ }[];
24
27
  }
@@ -1,15 +1,20 @@
1
+ import { AppError } from "../error";
1
2
  import { bindCommand } from "../utils";
2
3
  export class ValueManager {
3
4
  constructor(app) {
4
5
  this.list = new Map();
5
6
  this.app = app;
6
- app.registerCommand("get", bindCommand(this.get, this));
7
- app.registerCommand("set", bindCommand(this.set, this));
7
+ app.registerCommand("value.snapshot", bindCommand(this.getSnapshot, this));
8
8
  }
9
9
  /** 注册值 */
10
10
  register(name, { get, set, pluginName }) {
11
11
  this.list.set(name, { get, set, pluginName });
12
12
  this.app.emit("value:register", { name, value: get() }, { source: `plugin:${pluginName}` });
13
+ return {
14
+ emit: (v) => {
15
+ this.emit(name, v);
16
+ },
17
+ };
13
18
  }
14
19
  /** 取消注册 */
15
20
  unregister(name) {
@@ -20,7 +25,9 @@ export class ValueManager {
20
25
  get(name, options) {
21
26
  const item = this.list.get(name);
22
27
  if (!item)
23
- throw {};
28
+ throw new AppError("value:get_unexist", {
29
+ message: "无法获取未注册的值",
30
+ });
24
31
  return item.get(options);
25
32
  }
26
33
  /** 获取值(不存在则返回undefined) */
@@ -53,11 +60,10 @@ export class ValueManager {
53
60
  this.app.off(`change:${name}`, listener);
54
61
  }
55
62
  /** 获取注册的所有值 */
56
- getData() {
57
- const map = {};
58
- this.list.forEach((config, name) => {
59
- map[name] = config.get();
60
- });
61
- return map;
63
+ getSnapshot() {
64
+ return [...this.list].map(([name, config]) => ({
65
+ name,
66
+ value: config.get(),
67
+ }));
62
68
  }
63
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floating-live",
3
- "version": "0.16.0-alpha.9",
3
+ "version": "0.16.0-beta.2",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",