@havue/solutions 1.0.0

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 (53) hide show
  1. package/README.md +0 -0
  2. package/bc-connect/README.md +3 -0
  3. package/bc-connect/dist/bc-connect.mjs +342 -0
  4. package/bc-connect/dist/bc-connect.umd.js +346 -0
  5. package/bc-connect/dist/types/src/index.d.ts +1 -0
  6. package/bc-connect/dist/types/src/manager.d.ts +135 -0
  7. package/bc-connect/package.json +38 -0
  8. package/bc-connect/src/index.ts +1 -0
  9. package/bc-connect/src/manager.ts +388 -0
  10. package/bc-connect/vite.config.ts +4 -0
  11. package/dist/solutions.full.js +8504 -0
  12. package/dist/solutions.full.min.js +18 -0
  13. package/dist/solutions.full.min.js.map +1 -0
  14. package/dist/solutions.mjs +2 -0
  15. package/dist/solutions.umd.js +18 -0
  16. package/dist/types/bc-connect/src/index.d.ts +1 -0
  17. package/dist/types/bc-connect/src/manager.d.ts +135 -0
  18. package/dist/types/src/index.d.ts +2 -0
  19. package/dist/types/ws-video-manager/src/hooks/useVideoPlay.d.ts +73 -0
  20. package/dist/types/ws-video-manager/src/index.d.ts +5 -0
  21. package/dist/types/ws-video-manager/src/loader/index.d.ts +2 -0
  22. package/dist/types/ws-video-manager/src/loader/websocket-loader.d.ts +62 -0
  23. package/dist/types/ws-video-manager/src/manager/index.d.ts +144 -0
  24. package/dist/types/ws-video-manager/src/render/drawer.d.ts +44 -0
  25. package/dist/types/ws-video-manager/src/render/index.d.ts +105 -0
  26. package/package.json +44 -0
  27. package/src/index.ts +2 -0
  28. package/vite.config.ts +4 -0
  29. package/ws-video-manager/README.md +3 -0
  30. package/ws-video-manager/dist/types/src/hooks/useVideoPlay.d.ts +73 -0
  31. package/ws-video-manager/dist/types/src/index.d.ts +5 -0
  32. package/ws-video-manager/dist/types/src/loader/index.d.ts +2 -0
  33. package/ws-video-manager/dist/types/src/loader/websocket-loader.d.ts +62 -0
  34. package/ws-video-manager/dist/types/src/manager/index.d.ts +144 -0
  35. package/ws-video-manager/dist/types/src/render/drawer.d.ts +44 -0
  36. package/ws-video-manager/dist/types/src/render/index.d.ts +105 -0
  37. package/ws-video-manager/dist/ws-video-manager.mjs +8132 -0
  38. package/ws-video-manager/dist/ws-video-manager.umd.js +8135 -0
  39. package/ws-video-manager/node_modules/.bin/tsc +17 -0
  40. package/ws-video-manager/node_modules/.bin/tsc.CMD +12 -0
  41. package/ws-video-manager/node_modules/.bin/tsc.ps1 +41 -0
  42. package/ws-video-manager/node_modules/.bin/tsserver +17 -0
  43. package/ws-video-manager/node_modules/.bin/tsserver.CMD +12 -0
  44. package/ws-video-manager/node_modules/.bin/tsserver.ps1 +41 -0
  45. package/ws-video-manager/package.json +41 -0
  46. package/ws-video-manager/src/hooks/useVideoPlay.ts +357 -0
  47. package/ws-video-manager/src/index.ts +6 -0
  48. package/ws-video-manager/src/loader/index.ts +3 -0
  49. package/ws-video-manager/src/loader/websocket-loader.ts +278 -0
  50. package/ws-video-manager/src/manager/index.ts +429 -0
  51. package/ws-video-manager/src/render/drawer.ts +255 -0
  52. package/ws-video-manager/src/render/index.ts +475 -0
  53. package/ws-video-manager/vite.config.ts +4 -0
package/README.md ADDED
File without changes
@@ -0,0 +1,3 @@
1
+ # Havue Broadcast channel connect
2
+
3
+ [documents](https://happypedestrian.github.io/havue/solutions/broadcast-channel-connect.html)
@@ -0,0 +1,342 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ var BcConnectEventTypeEnum = /* @__PURE__ */ ((BcConnectEventTypeEnum2) => {
5
+ BcConnectEventTypeEnum2["Broadcast"] = "Hello world";
6
+ BcConnectEventTypeEnum2["Broadcast_Reply"] = "I can hear you";
7
+ BcConnectEventTypeEnum2["Main_Node_Hearbeat"] = "苍天还在,别立黄天";
8
+ BcConnectEventTypeEnum2["Res_Main_Node_Hearbeat"] = "苍天在上,受我一拜";
9
+ BcConnectEventTypeEnum2["Req_Be_Main_Node"] = "苍天已死,黄天当立";
10
+ BcConnectEventTypeEnum2["Res_Be_Main_Node"] = "我是黄天,尔等退下";
11
+ BcConnectEventTypeEnum2["Node_Type_Change"] = "node type change";
12
+ BcConnectEventTypeEnum2["Friend_List_Update"] = "friend list update";
13
+ return BcConnectEventTypeEnum2;
14
+ })(BcConnectEventTypeEnum || {});
15
+ var BcConnectNodeTypeEnum = /* @__PURE__ */ ((BcConnectNodeTypeEnum2) => {
16
+ BcConnectNodeTypeEnum2["Main"] = "main";
17
+ BcConnectNodeTypeEnum2["Normal"] = "normal";
18
+ return BcConnectNodeTypeEnum2;
19
+ })(BcConnectNodeTypeEnum || {});
20
+ const MessageTimeout = 300;
21
+ class BroadcastChannelManager {
22
+ constructor(name, debug = false) {
23
+ /** 通道名称 */
24
+ __publicField(this, "_bcName");
25
+ /** BroadcastChannel实例 */
26
+ __publicField(this, "_broadcastChannel");
27
+ /** 事件map */
28
+ __publicField(this, "_eventMap");
29
+ /** 主节点发送心跳的interval */
30
+ __publicField(this, "_mainNodeMsgInterval", null);
31
+ /** 认为主节点掉线的timeout */
32
+ __publicField(this, "_mainNoceMsgTimeoutTimer", null);
33
+ /** 当前实例id */
34
+ __publicField(this, "id", Date.now() + Math.random());
35
+ /** 记录的友方id数组 */
36
+ __publicField(this, "_oldFrendChannelIdList", []);
37
+ /** 正在更新的右方id数组 */
38
+ __publicField(this, "_friendChannelIdSet", /* @__PURE__ */ new Set());
39
+ /** 当前节点类型 */
40
+ __publicField(this, "_nodeType");
41
+ /** 是否开启调试模式,会在控制台打印相关信息 */
42
+ __publicField(this, "_debug", false);
43
+ this._debug = debug;
44
+ this._bcName = name;
45
+ this._eventMap = /* @__PURE__ */ new Map();
46
+ this._debug && console.log("BC:id:", this.id);
47
+ }
48
+ get nodeType() {
49
+ return this._nodeType;
50
+ }
51
+ get friendList() {
52
+ return [...this._oldFrendChannelIdList];
53
+ }
54
+ connect() {
55
+ this._debug && console.log("BC:bc connect");
56
+ this.close();
57
+ this._broadcastChannel = new BroadcastChannel(this._bcName);
58
+ this._bindBroadcastChannelEvent();
59
+ this._updateFriendList();
60
+ }
61
+ close() {
62
+ this._debug && console.log("BC:bc close");
63
+ this._broadcastChannel && this._broadcastChannel.close();
64
+ }
65
+ /**
66
+ * 切换节点类型
67
+ * @param {BcConnectNodeTypeEnum} type
68
+ * @returns
69
+ */
70
+ _setNodeType(type) {
71
+ if (this._nodeType === type) {
72
+ return;
73
+ }
74
+ this._nodeType = type;
75
+ this.emit("node type change", {
76
+ type: "node type change",
77
+ data: type,
78
+ id: this.id
79
+ });
80
+ }
81
+ /** 更新友方列表 */
82
+ _updateFriendList() {
83
+ this.send(
84
+ "Hello world"
85
+ /* Broadcast */
86
+ );
87
+ setTimeout(() => {
88
+ this._oldFrendChannelIdList = this._getNewFriendList();
89
+ this._debug && console.log("BC:connect:updateFriendChannelIdList:", this._oldFrendChannelIdList);
90
+ this.emit("friend list update", {
91
+ type: "friend list update",
92
+ data: [...this._oldFrendChannelIdList],
93
+ id: this.id
94
+ });
95
+ this._updataNodeType();
96
+ }, MessageTimeout);
97
+ }
98
+ /** 绑定事件 */
99
+ _bindBroadcastChannelEvent() {
100
+ this._broadcastChannel && (this._broadcastChannel.onmessage = (event) => {
101
+ const { type, targetId } = event.data;
102
+ if (targetId && targetId !== this.id) {
103
+ return;
104
+ }
105
+ this.emit(type, event.data);
106
+ });
107
+ this.on("Hello world", (data) => {
108
+ const { id } = data;
109
+ if (!this._friendChannelIdSet.has(id)) {
110
+ this._friendChannelIdSet.add(id);
111
+ }
112
+ this.sendToTarget("I can hear you", id);
113
+ });
114
+ this.on("I can hear you", (data) => {
115
+ const { id } = data;
116
+ this._addFriend(id);
117
+ });
118
+ this.on("苍天已死,黄天当立", (data) => {
119
+ const { id } = data;
120
+ if (id > this.id) {
121
+ this.sendToTarget("我是黄天,尔等退下", id);
122
+ }
123
+ });
124
+ this.on("苍天在上,受我一拜", (data) => {
125
+ this._addFriend(data.id);
126
+ });
127
+ this._bindNodeEvent();
128
+ }
129
+ /** 监听节点类型切换事件 */
130
+ _bindNodeEvent() {
131
+ const onMainNodeHearbeat = (data) => {
132
+ this._timeoutToBeMainNode();
133
+ this._catchOldFriend();
134
+ this._addFriend(data.id);
135
+ this.send(
136
+ "苍天在上,受我一拜"
137
+ /* Res_Main_Node_Hearbeat */
138
+ );
139
+ };
140
+ this.on("node type change", (info) => {
141
+ const { data } = info;
142
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
143
+ this._debug && console.log("BC:代理类型切换:", info.data);
144
+ if (data === "main") {
145
+ this._mainNodeMsgInterval = setInterval(() => {
146
+ this._catchOldFriend();
147
+ this.send(
148
+ "苍天还在,别立黄天"
149
+ /* Main_Node_Hearbeat */
150
+ );
151
+ }, MessageTimeout);
152
+ } else if (data === "normal") {
153
+ this._timeoutToBeMainNode();
154
+ }
155
+ this.on("苍天还在,别立黄天", onMainNodeHearbeat);
156
+ });
157
+ }
158
+ /** 获取最新的友方列表 */
159
+ _getNewFriendList() {
160
+ return [...this._friendChannelIdSet].sort((a, b) => a - b);
161
+ }
162
+ /**
163
+ * 更新当前节点类型
164
+ */
165
+ _updataNodeType() {
166
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
167
+ if (this._oldFrendChannelIdList.length === 0 || Math.min(...this._oldFrendChannelIdList) > this.id) {
168
+ if (this._nodeType === "main") {
169
+ return;
170
+ }
171
+ this._setNodeType(
172
+ "main"
173
+ /* Main */
174
+ );
175
+ } else {
176
+ if (this._nodeType === "normal") {
177
+ return;
178
+ }
179
+ this._setNodeType(
180
+ "normal"
181
+ /* Normal */
182
+ );
183
+ }
184
+ }
185
+ _timeoutToBeMainNode() {
186
+ this._mainNoceMsgTimeoutTimer && clearTimeout(this._mainNoceMsgTimeoutTimer);
187
+ this._mainNoceMsgTimeoutTimer = setTimeout(() => {
188
+ this._req_beMainNode();
189
+ }, MessageTimeout * 3);
190
+ }
191
+ /**
192
+ * 保持记录的活跃的友方id列表
193
+ * 清空正在记录的友方id列表
194
+ */
195
+ _catchOldFriend() {
196
+ const newFriendList = this._getNewFriendList();
197
+ if (this._oldFrendChannelIdList.join() !== newFriendList.join()) {
198
+ this._debug && console.log("BC:updateFriendChannelIdList:", newFriendList);
199
+ this.emit("friend list update", {
200
+ type: "friend list update",
201
+ data: [...newFriendList],
202
+ id: this.id
203
+ });
204
+ this._oldFrendChannelIdList = [...newFriendList];
205
+ }
206
+ if (this._nodeType === "main" && Math.min(...this._oldFrendChannelIdList) < this.id) {
207
+ this._setNodeType(
208
+ "normal"
209
+ /* Normal */
210
+ );
211
+ }
212
+ this._friendChannelIdSet.clear();
213
+ }
214
+ /**
215
+ * 申请成为主节点
216
+ */
217
+ _req_beMainNode() {
218
+ this._debug && console.log("BC:req_beMainNode");
219
+ this.send(
220
+ "苍天已死,黄天当立"
221
+ /* Req_Be_Main_Node */
222
+ );
223
+ const timer = setTimeout(() => {
224
+ this._setNodeType(
225
+ "main"
226
+ /* Main */
227
+ );
228
+ }, MessageTimeout);
229
+ const handleRes_beMainNode = () => {
230
+ clearTimeout(timer);
231
+ this.off("我是黄天,尔等退下", handleRes_beMainNode);
232
+ };
233
+ this.on("我是黄天,尔等退下", handleRes_beMainNode);
234
+ }
235
+ /**
236
+ * 添加友方
237
+ * @param id 节点id
238
+ */
239
+ _addFriend(id) {
240
+ if (!this._friendChannelIdSet.has(id)) {
241
+ this._friendChannelIdSet.add(id);
242
+ }
243
+ }
244
+ /**
245
+ * 广播消息
246
+ * @param type 消息类型
247
+ * @param data 数据
248
+ */
249
+ send(type, data) {
250
+ var _a;
251
+ (_a = this._broadcastChannel) == null ? void 0 : _a.postMessage({
252
+ type,
253
+ data,
254
+ id: this.id
255
+ });
256
+ }
257
+ /**
258
+ * 给特定id的节点发送消息
259
+ * @param type 消息类型
260
+ * @param targetId 目标节点id
261
+ * @param data 数据
262
+ */
263
+ sendToTarget(type, targetId, data) {
264
+ var _a;
265
+ (_a = this._broadcastChannel) == null ? void 0 : _a.postMessage({
266
+ type,
267
+ data,
268
+ id: this.id,
269
+ targetId
270
+ });
271
+ }
272
+ /**
273
+ * 注册事件
274
+ * @param { string } event 事件类型
275
+ * @param callback 回调
276
+ * @returns void
277
+ */
278
+ on(event, callback) {
279
+ const callbacks = this._eventMap.get(event);
280
+ if (!callbacks) {
281
+ this._eventMap.set(event, [callback]);
282
+ return;
283
+ }
284
+ if (callbacks.includes(callback)) {
285
+ return;
286
+ }
287
+ callbacks.push(callback);
288
+ }
289
+ /**
290
+ * 注销事件
291
+ * @param { string } event 事件类型
292
+ * @param callback 事件回调
293
+ * @returns
294
+ */
295
+ off(event, callback) {
296
+ const callbacks = this._eventMap.get(event);
297
+ if (!callbacks) {
298
+ return;
299
+ }
300
+ if (!callback) {
301
+ callbacks.length = 0;
302
+ return;
303
+ }
304
+ const index = callbacks.indexOf(callback);
305
+ callbacks.splice(index, 1);
306
+ }
307
+ /**
308
+ * 触发事件
309
+ * @param { string } event 事件类型
310
+ * @param data 数据
311
+ */
312
+ emit(event, data) {
313
+ const callbacks = this._eventMap.get(event) || [];
314
+ callbacks.forEach((cb) => {
315
+ cb(data);
316
+ });
317
+ }
318
+ /**
319
+ * 销毁
320
+ */
321
+ destroy() {
322
+ var _a;
323
+ this._bcName = "";
324
+ this._eventMap.clear();
325
+ (_a = this._broadcastChannel) == null ? void 0 : _a.close();
326
+ this._broadcastChannel = void 0;
327
+ this._oldFrendChannelIdList = [];
328
+ this._friendChannelIdSet.clear();
329
+ this.id = -1;
330
+ this._nodeType = void 0;
331
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
332
+ this._mainNodeMsgInterval = null;
333
+ this._mainNoceMsgTimeoutTimer && clearInterval(this._mainNoceMsgTimeoutTimer);
334
+ this._mainNoceMsgTimeoutTimer = null;
335
+ this._debug && console.log("BC:destroy");
336
+ }
337
+ }
338
+ export {
339
+ BcConnectEventTypeEnum,
340
+ BcConnectNodeTypeEnum,
341
+ BroadcastChannelManager
342
+ };
@@ -0,0 +1,346 @@
1
+ (function(global, factory) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["bc-connect"] = {}));
3
+ })(this, function(exports2) {
4
+ "use strict";var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+
8
+ var BcConnectEventTypeEnum = /* @__PURE__ */ ((BcConnectEventTypeEnum2) => {
9
+ BcConnectEventTypeEnum2["Broadcast"] = "Hello world";
10
+ BcConnectEventTypeEnum2["Broadcast_Reply"] = "I can hear you";
11
+ BcConnectEventTypeEnum2["Main_Node_Hearbeat"] = "苍天还在,别立黄天";
12
+ BcConnectEventTypeEnum2["Res_Main_Node_Hearbeat"] = "苍天在上,受我一拜";
13
+ BcConnectEventTypeEnum2["Req_Be_Main_Node"] = "苍天已死,黄天当立";
14
+ BcConnectEventTypeEnum2["Res_Be_Main_Node"] = "我是黄天,尔等退下";
15
+ BcConnectEventTypeEnum2["Node_Type_Change"] = "node type change";
16
+ BcConnectEventTypeEnum2["Friend_List_Update"] = "friend list update";
17
+ return BcConnectEventTypeEnum2;
18
+ })(BcConnectEventTypeEnum || {});
19
+ var BcConnectNodeTypeEnum = /* @__PURE__ */ ((BcConnectNodeTypeEnum2) => {
20
+ BcConnectNodeTypeEnum2["Main"] = "main";
21
+ BcConnectNodeTypeEnum2["Normal"] = "normal";
22
+ return BcConnectNodeTypeEnum2;
23
+ })(BcConnectNodeTypeEnum || {});
24
+ const MessageTimeout = 300;
25
+ class BroadcastChannelManager {
26
+ constructor(name, debug = false) {
27
+ /** 通道名称 */
28
+ __publicField(this, "_bcName");
29
+ /** BroadcastChannel实例 */
30
+ __publicField(this, "_broadcastChannel");
31
+ /** 事件map */
32
+ __publicField(this, "_eventMap");
33
+ /** 主节点发送心跳的interval */
34
+ __publicField(this, "_mainNodeMsgInterval", null);
35
+ /** 认为主节点掉线的timeout */
36
+ __publicField(this, "_mainNoceMsgTimeoutTimer", null);
37
+ /** 当前实例id */
38
+ __publicField(this, "id", Date.now() + Math.random());
39
+ /** 记录的友方id数组 */
40
+ __publicField(this, "_oldFrendChannelIdList", []);
41
+ /** 正在更新的右方id数组 */
42
+ __publicField(this, "_friendChannelIdSet", /* @__PURE__ */ new Set());
43
+ /** 当前节点类型 */
44
+ __publicField(this, "_nodeType");
45
+ /** 是否开启调试模式,会在控制台打印相关信息 */
46
+ __publicField(this, "_debug", false);
47
+ this._debug = debug;
48
+ this._bcName = name;
49
+ this._eventMap = /* @__PURE__ */ new Map();
50
+ this._debug && console.log("BC:id:", this.id);
51
+ }
52
+ get nodeType() {
53
+ return this._nodeType;
54
+ }
55
+ get friendList() {
56
+ return [...this._oldFrendChannelIdList];
57
+ }
58
+ connect() {
59
+ this._debug && console.log("BC:bc connect");
60
+ this.close();
61
+ this._broadcastChannel = new BroadcastChannel(this._bcName);
62
+ this._bindBroadcastChannelEvent();
63
+ this._updateFriendList();
64
+ }
65
+ close() {
66
+ this._debug && console.log("BC:bc close");
67
+ this._broadcastChannel && this._broadcastChannel.close();
68
+ }
69
+ /**
70
+ * 切换节点类型
71
+ * @param {BcConnectNodeTypeEnum} type
72
+ * @returns
73
+ */
74
+ _setNodeType(type) {
75
+ if (this._nodeType === type) {
76
+ return;
77
+ }
78
+ this._nodeType = type;
79
+ this.emit("node type change", {
80
+ type: "node type change",
81
+ data: type,
82
+ id: this.id
83
+ });
84
+ }
85
+ /** 更新友方列表 */
86
+ _updateFriendList() {
87
+ this.send(
88
+ "Hello world"
89
+ /* Broadcast */
90
+ );
91
+ setTimeout(() => {
92
+ this._oldFrendChannelIdList = this._getNewFriendList();
93
+ this._debug && console.log("BC:connect:updateFriendChannelIdList:", this._oldFrendChannelIdList);
94
+ this.emit("friend list update", {
95
+ type: "friend list update",
96
+ data: [...this._oldFrendChannelIdList],
97
+ id: this.id
98
+ });
99
+ this._updataNodeType();
100
+ }, MessageTimeout);
101
+ }
102
+ /** 绑定事件 */
103
+ _bindBroadcastChannelEvent() {
104
+ this._broadcastChannel && (this._broadcastChannel.onmessage = (event) => {
105
+ const { type, targetId } = event.data;
106
+ if (targetId && targetId !== this.id) {
107
+ return;
108
+ }
109
+ this.emit(type, event.data);
110
+ });
111
+ this.on("Hello world", (data) => {
112
+ const { id } = data;
113
+ if (!this._friendChannelIdSet.has(id)) {
114
+ this._friendChannelIdSet.add(id);
115
+ }
116
+ this.sendToTarget("I can hear you", id);
117
+ });
118
+ this.on("I can hear you", (data) => {
119
+ const { id } = data;
120
+ this._addFriend(id);
121
+ });
122
+ this.on("苍天已死,黄天当立", (data) => {
123
+ const { id } = data;
124
+ if (id > this.id) {
125
+ this.sendToTarget("我是黄天,尔等退下", id);
126
+ }
127
+ });
128
+ this.on("苍天在上,受我一拜", (data) => {
129
+ this._addFriend(data.id);
130
+ });
131
+ this._bindNodeEvent();
132
+ }
133
+ /** 监听节点类型切换事件 */
134
+ _bindNodeEvent() {
135
+ const onMainNodeHearbeat = (data) => {
136
+ this._timeoutToBeMainNode();
137
+ this._catchOldFriend();
138
+ this._addFriend(data.id);
139
+ this.send(
140
+ "苍天在上,受我一拜"
141
+ /* Res_Main_Node_Hearbeat */
142
+ );
143
+ };
144
+ this.on("node type change", (info) => {
145
+ const { data } = info;
146
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
147
+ this._debug && console.log("BC:代理类型切换:", info.data);
148
+ if (data === "main") {
149
+ this._mainNodeMsgInterval = setInterval(() => {
150
+ this._catchOldFriend();
151
+ this.send(
152
+ "苍天还在,别立黄天"
153
+ /* Main_Node_Hearbeat */
154
+ );
155
+ }, MessageTimeout);
156
+ } else if (data === "normal") {
157
+ this._timeoutToBeMainNode();
158
+ }
159
+ this.on("苍天还在,别立黄天", onMainNodeHearbeat);
160
+ });
161
+ }
162
+ /** 获取最新的友方列表 */
163
+ _getNewFriendList() {
164
+ return [...this._friendChannelIdSet].sort((a, b) => a - b);
165
+ }
166
+ /**
167
+ * 更新当前节点类型
168
+ */
169
+ _updataNodeType() {
170
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
171
+ if (this._oldFrendChannelIdList.length === 0 || Math.min(...this._oldFrendChannelIdList) > this.id) {
172
+ if (this._nodeType === "main") {
173
+ return;
174
+ }
175
+ this._setNodeType(
176
+ "main"
177
+ /* Main */
178
+ );
179
+ } else {
180
+ if (this._nodeType === "normal") {
181
+ return;
182
+ }
183
+ this._setNodeType(
184
+ "normal"
185
+ /* Normal */
186
+ );
187
+ }
188
+ }
189
+ _timeoutToBeMainNode() {
190
+ this._mainNoceMsgTimeoutTimer && clearTimeout(this._mainNoceMsgTimeoutTimer);
191
+ this._mainNoceMsgTimeoutTimer = setTimeout(() => {
192
+ this._req_beMainNode();
193
+ }, MessageTimeout * 3);
194
+ }
195
+ /**
196
+ * 保持记录的活跃的友方id列表
197
+ * 清空正在记录的友方id列表
198
+ */
199
+ _catchOldFriend() {
200
+ const newFriendList = this._getNewFriendList();
201
+ if (this._oldFrendChannelIdList.join() !== newFriendList.join()) {
202
+ this._debug && console.log("BC:updateFriendChannelIdList:", newFriendList);
203
+ this.emit("friend list update", {
204
+ type: "friend list update",
205
+ data: [...newFriendList],
206
+ id: this.id
207
+ });
208
+ this._oldFrendChannelIdList = [...newFriendList];
209
+ }
210
+ if (this._nodeType === "main" && Math.min(...this._oldFrendChannelIdList) < this.id) {
211
+ this._setNodeType(
212
+ "normal"
213
+ /* Normal */
214
+ );
215
+ }
216
+ this._friendChannelIdSet.clear();
217
+ }
218
+ /**
219
+ * 申请成为主节点
220
+ */
221
+ _req_beMainNode() {
222
+ this._debug && console.log("BC:req_beMainNode");
223
+ this.send(
224
+ "苍天已死,黄天当立"
225
+ /* Req_Be_Main_Node */
226
+ );
227
+ const timer = setTimeout(() => {
228
+ this._setNodeType(
229
+ "main"
230
+ /* Main */
231
+ );
232
+ }, MessageTimeout);
233
+ const handleRes_beMainNode = () => {
234
+ clearTimeout(timer);
235
+ this.off("我是黄天,尔等退下", handleRes_beMainNode);
236
+ };
237
+ this.on("我是黄天,尔等退下", handleRes_beMainNode);
238
+ }
239
+ /**
240
+ * 添加友方
241
+ * @param id 节点id
242
+ */
243
+ _addFriend(id) {
244
+ if (!this._friendChannelIdSet.has(id)) {
245
+ this._friendChannelIdSet.add(id);
246
+ }
247
+ }
248
+ /**
249
+ * 广播消息
250
+ * @param type 消息类型
251
+ * @param data 数据
252
+ */
253
+ send(type, data) {
254
+ var _a;
255
+ (_a = this._broadcastChannel) == null ? void 0 : _a.postMessage({
256
+ type,
257
+ data,
258
+ id: this.id
259
+ });
260
+ }
261
+ /**
262
+ * 给特定id的节点发送消息
263
+ * @param type 消息类型
264
+ * @param targetId 目标节点id
265
+ * @param data 数据
266
+ */
267
+ sendToTarget(type, targetId, data) {
268
+ var _a;
269
+ (_a = this._broadcastChannel) == null ? void 0 : _a.postMessage({
270
+ type,
271
+ data,
272
+ id: this.id,
273
+ targetId
274
+ });
275
+ }
276
+ /**
277
+ * 注册事件
278
+ * @param { string } event 事件类型
279
+ * @param callback 回调
280
+ * @returns void
281
+ */
282
+ on(event, callback) {
283
+ const callbacks = this._eventMap.get(event);
284
+ if (!callbacks) {
285
+ this._eventMap.set(event, [callback]);
286
+ return;
287
+ }
288
+ if (callbacks.includes(callback)) {
289
+ return;
290
+ }
291
+ callbacks.push(callback);
292
+ }
293
+ /**
294
+ * 注销事件
295
+ * @param { string } event 事件类型
296
+ * @param callback 事件回调
297
+ * @returns
298
+ */
299
+ off(event, callback) {
300
+ const callbacks = this._eventMap.get(event);
301
+ if (!callbacks) {
302
+ return;
303
+ }
304
+ if (!callback) {
305
+ callbacks.length = 0;
306
+ return;
307
+ }
308
+ const index = callbacks.indexOf(callback);
309
+ callbacks.splice(index, 1);
310
+ }
311
+ /**
312
+ * 触发事件
313
+ * @param { string } event 事件类型
314
+ * @param data 数据
315
+ */
316
+ emit(event, data) {
317
+ const callbacks = this._eventMap.get(event) || [];
318
+ callbacks.forEach((cb) => {
319
+ cb(data);
320
+ });
321
+ }
322
+ /**
323
+ * 销毁
324
+ */
325
+ destroy() {
326
+ var _a;
327
+ this._bcName = "";
328
+ this._eventMap.clear();
329
+ (_a = this._broadcastChannel) == null ? void 0 : _a.close();
330
+ this._broadcastChannel = void 0;
331
+ this._oldFrendChannelIdList = [];
332
+ this._friendChannelIdSet.clear();
333
+ this.id = -1;
334
+ this._nodeType = void 0;
335
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
336
+ this._mainNodeMsgInterval = null;
337
+ this._mainNoceMsgTimeoutTimer && clearInterval(this._mainNoceMsgTimeoutTimer);
338
+ this._mainNoceMsgTimeoutTimer = null;
339
+ this._debug && console.log("BC:destroy");
340
+ }
341
+ }
342
+ exports2.BcConnectEventTypeEnum = BcConnectEventTypeEnum;
343
+ exports2.BcConnectNodeTypeEnum = BcConnectNodeTypeEnum;
344
+ exports2.BroadcastChannelManager = BroadcastChannelManager;
345
+ Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
346
+ });
@@ -0,0 +1 @@
1
+ export * from './manager';