@havue/solutions 1.1.1 → 1.1.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.
@@ -0,0 +1,73 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { BroadcastChannelManager, BcConnectNodeTypeEnum } from '@havue/bc-connect'
3
+
4
+ async function sleep(time) {
5
+ return new Promise((res) => {
6
+ setTimeout(res, time)
7
+ })
8
+ }
9
+
10
+ const DEBUG = false
11
+
12
+ describe.sequential('BroadcastChannelManager', () => {
13
+ it('should create a new instance', () => {
14
+ const manager = new BroadcastChannelManager('test-channel', DEBUG)
15
+ expect(manager).toBeInstanceOf(BroadcastChannelManager)
16
+ manager.destroy()
17
+ })
18
+
19
+ it('should be Main', async () => {
20
+ const manager = new BroadcastChannelManager('test-channel', DEBUG)
21
+ manager.connect()
22
+ expect(manager.nodeType).toBeUndefined()
23
+ await sleep(400)
24
+ expect(manager.nodeType).toBe(BcConnectNodeTypeEnum.Main)
25
+ manager.close()
26
+ manager.destroy()
27
+ })
28
+
29
+ it('when channel closed', async () => {
30
+ const manager = new BroadcastChannelManager('test-channel', DEBUG)
31
+ manager.connect()
32
+ manager.close()
33
+ await sleep(400)
34
+ expect(manager.nodeType).toBeUndefined()
35
+ manager.destroy()
36
+ })
37
+
38
+ it('should handle messages', async () => {
39
+ const manager1 = new BroadcastChannelManager('test-channel', DEBUG)
40
+ // 确保第二个id生成在第一个之后
41
+ await sleep(10)
42
+ const manager2 = new BroadcastChannelManager('test-channel', DEBUG)
43
+ manager1.connect()
44
+ manager2.connect()
45
+ await sleep(1200)
46
+
47
+ const message1 = { type: 'test', data: 'Hello, manger2!' }
48
+ const message2 = { type: 'test', data: 'Hello, manger1!' }
49
+
50
+ manager1.on('test', (message) => {
51
+ expect(message.data).toBe(message2.data)
52
+ })
53
+ manager2.on('test', (message) => {
54
+ expect(message.data).toBe(message1.data)
55
+ })
56
+
57
+ manager1.send(message1.type, message1.data)
58
+ manager2.send(message2.type, message2.data)
59
+ expect(manager1.nodeType).toBe(BcConnectNodeTypeEnum.Main)
60
+ expect(manager2.nodeType).toBe(BcConnectNodeTypeEnum.Normal)
61
+
62
+ expect(manager1.friendList).toContain(manager2.id)
63
+ expect(manager2.friendList).toContain(manager1.id)
64
+
65
+ // wait for the message to be received
66
+ await sleep(1000)
67
+ manager1.close()
68
+ manager2.close()
69
+
70
+ manager1.destroy()
71
+ manager2.destroy()
72
+ })
73
+ })
@@ -29,7 +29,9 @@ class BroadcastChannelManager {
29
29
  /** 主节点发送心跳的interval */
30
30
  __publicField(this, "_mainNodeMsgInterval", null);
31
31
  /** 认为主节点掉线的timeout */
32
- __publicField(this, "_mainNoceMsgTimeoutTimer", null);
32
+ __publicField(this, "_mainNodeMsgTimeoutTimer", null);
33
+ /** 更新友方列表的timeout */
34
+ __publicField(this, "_updateFriendListTimer", null);
33
35
  /** 当前实例id */
34
36
  __publicField(this, "id", Date.now() + Math.random());
35
37
  /** 记录的友方id数组 */
@@ -61,6 +63,16 @@ class BroadcastChannelManager {
61
63
  close() {
62
64
  this._debug && console.log("BC:bc close");
63
65
  this._broadcastChannel && this._broadcastChannel.close();
66
+ this._broadcastChannel = void 0;
67
+ this._updateFriendListTimer && clearTimeout(this._updateFriendListTimer);
68
+ this._updateFriendListTimer = null;
69
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
70
+ this._mainNodeMsgInterval = null;
71
+ this._mainNodeMsgTimeoutTimer && clearTimeout(this._mainNodeMsgTimeoutTimer);
72
+ this._mainNodeMsgTimeoutTimer = null;
73
+ this._oldFrendChannelIdList = [];
74
+ this._friendChannelIdSet.clear();
75
+ this._nodeType = void 0;
64
76
  }
65
77
  /**
66
78
  * 切换节点类型
@@ -84,7 +96,8 @@ class BroadcastChannelManager {
84
96
  "Hello world"
85
97
  /* Broadcast */
86
98
  );
87
- setTimeout(() => {
99
+ this._updateFriendListTimer && clearTimeout(this._updateFriendListTimer);
100
+ this._updateFriendListTimer = setTimeout(() => {
88
101
  this._oldFrendChannelIdList = this._getNewFriendList();
89
102
  this._debug && console.log("BC:connect:updateFriendChannelIdList:", this._oldFrendChannelIdList);
90
103
  this.emit("friend list update", {
@@ -183,8 +196,8 @@ class BroadcastChannelManager {
183
196
  }
184
197
  }
185
198
  _timeoutToBeMainNode() {
186
- this._mainNoceMsgTimeoutTimer && clearTimeout(this._mainNoceMsgTimeoutTimer);
187
- this._mainNoceMsgTimeoutTimer = setTimeout(() => {
199
+ this._mainNodeMsgTimeoutTimer && clearTimeout(this._mainNodeMsgTimeoutTimer);
200
+ this._mainNodeMsgTimeoutTimer = setTimeout(() => {
188
201
  this._req_beMainNode();
189
202
  }, MessageTimeout * 3);
190
203
  }
@@ -330,8 +343,8 @@ class BroadcastChannelManager {
330
343
  this._nodeType = void 0;
331
344
  this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
332
345
  this._mainNodeMsgInterval = null;
333
- this._mainNoceMsgTimeoutTimer && clearInterval(this._mainNoceMsgTimeoutTimer);
334
- this._mainNoceMsgTimeoutTimer = null;
346
+ this._mainNodeMsgTimeoutTimer && clearInterval(this._mainNodeMsgTimeoutTimer);
347
+ this._mainNodeMsgTimeoutTimer = null;
335
348
  this._debug && console.log("BC:destroy");
336
349
  }
337
350
  }
@@ -33,7 +33,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
33
33
  /** 主节点发送心跳的interval */
34
34
  __publicField(this, "_mainNodeMsgInterval", null);
35
35
  /** 认为主节点掉线的timeout */
36
- __publicField(this, "_mainNoceMsgTimeoutTimer", null);
36
+ __publicField(this, "_mainNodeMsgTimeoutTimer", null);
37
+ /** 更新友方列表的timeout */
38
+ __publicField(this, "_updateFriendListTimer", null);
37
39
  /** 当前实例id */
38
40
  __publicField(this, "id", Date.now() + Math.random());
39
41
  /** 记录的友方id数组 */
@@ -65,6 +67,16 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
65
67
  close() {
66
68
  this._debug && console.log("BC:bc close");
67
69
  this._broadcastChannel && this._broadcastChannel.close();
70
+ this._broadcastChannel = void 0;
71
+ this._updateFriendListTimer && clearTimeout(this._updateFriendListTimer);
72
+ this._updateFriendListTimer = null;
73
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
74
+ this._mainNodeMsgInterval = null;
75
+ this._mainNodeMsgTimeoutTimer && clearTimeout(this._mainNodeMsgTimeoutTimer);
76
+ this._mainNodeMsgTimeoutTimer = null;
77
+ this._oldFrendChannelIdList = [];
78
+ this._friendChannelIdSet.clear();
79
+ this._nodeType = void 0;
68
80
  }
69
81
  /**
70
82
  * 切换节点类型
@@ -88,7 +100,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
88
100
  "Hello world"
89
101
  /* Broadcast */
90
102
  );
91
- setTimeout(() => {
103
+ this._updateFriendListTimer && clearTimeout(this._updateFriendListTimer);
104
+ this._updateFriendListTimer = setTimeout(() => {
92
105
  this._oldFrendChannelIdList = this._getNewFriendList();
93
106
  this._debug && console.log("BC:connect:updateFriendChannelIdList:", this._oldFrendChannelIdList);
94
107
  this.emit("friend list update", {
@@ -187,8 +200,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
187
200
  }
188
201
  }
189
202
  _timeoutToBeMainNode() {
190
- this._mainNoceMsgTimeoutTimer && clearTimeout(this._mainNoceMsgTimeoutTimer);
191
- this._mainNoceMsgTimeoutTimer = setTimeout(() => {
203
+ this._mainNodeMsgTimeoutTimer && clearTimeout(this._mainNodeMsgTimeoutTimer);
204
+ this._mainNodeMsgTimeoutTimer = setTimeout(() => {
192
205
  this._req_beMainNode();
193
206
  }, MessageTimeout * 3);
194
207
  }
@@ -334,8 +347,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
334
347
  this._nodeType = void 0;
335
348
  this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
336
349
  this._mainNodeMsgInterval = null;
337
- this._mainNoceMsgTimeoutTimer && clearInterval(this._mainNoceMsgTimeoutTimer);
338
- this._mainNoceMsgTimeoutTimer = null;
350
+ this._mainNodeMsgTimeoutTimer && clearInterval(this._mainNodeMsgTimeoutTimer);
351
+ this._mainNodeMsgTimeoutTimer = null;
339
352
  this._debug && console.log("BC:destroy");
340
353
  }
341
354
  }
@@ -46,7 +46,9 @@ export declare class BroadcastChannelManager {
46
46
  /** 主节点发送心跳的interval */
47
47
  private _mainNodeMsgInterval;
48
48
  /** 认为主节点掉线的timeout */
49
- private _mainNoceMsgTimeoutTimer;
49
+ private _mainNodeMsgTimeoutTimer;
50
+ /** 更新友方列表的timeout */
51
+ private _updateFriendListTimer;
50
52
  /** 当前实例id */
51
53
  id: number;
52
54
  /** 记录的友方id数组 */
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@havue/bc-connect",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Javascript class for Broadcast channel connect",
5
5
  "keywords": [
6
6
  "havue",
@@ -54,7 +54,9 @@ export class BroadcastChannelManager {
54
54
  /** 主节点发送心跳的interval */
55
55
  private _mainNodeMsgInterval: number | null = null
56
56
  /** 认为主节点掉线的timeout */
57
- private _mainNoceMsgTimeoutTimer: number | null = null
57
+ private _mainNodeMsgTimeoutTimer: number | null = null
58
+ /** 更新友方列表的timeout */
59
+ private _updateFriendListTimer: number | null = null
58
60
  /** 当前实例id */
59
61
  public id: number = Date.now() + Math.random()
60
62
  /** 记录的友方id数组 */
@@ -93,6 +95,16 @@ export class BroadcastChannelManager {
93
95
  public close() {
94
96
  this._debug && console.log('BC:bc close')
95
97
  this._broadcastChannel && this._broadcastChannel.close()
98
+ this._broadcastChannel = undefined
99
+ this._updateFriendListTimer && clearTimeout(this._updateFriendListTimer)
100
+ this._updateFriendListTimer = null
101
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval)
102
+ this._mainNodeMsgInterval = null
103
+ this._mainNodeMsgTimeoutTimer && clearTimeout(this._mainNodeMsgTimeoutTimer)
104
+ this._mainNodeMsgTimeoutTimer = null
105
+ this._oldFrendChannelIdList = []
106
+ this._friendChannelIdSet.clear()
107
+ this._nodeType = undefined
96
108
  }
97
109
 
98
110
  /**
@@ -117,7 +129,9 @@ export class BroadcastChannelManager {
117
129
  // 广播告知己方存在
118
130
  this.send(BcConnectEventTypeEnum.Broadcast)
119
131
 
120
- setTimeout(() => {
132
+ this._updateFriendListTimer && clearTimeout(this._updateFriendListTimer)
133
+
134
+ this._updateFriendListTimer = setTimeout(() => {
121
135
  this._oldFrendChannelIdList = this._getNewFriendList()
122
136
  this._debug && console.log('BC:connect:updateFriendChannelIdList:', this._oldFrendChannelIdList)
123
137
  this.emit(BcConnectEventTypeEnum.Friend_List_Update, {
@@ -221,9 +235,9 @@ export class BroadcastChannelManager {
221
235
  }
222
236
 
223
237
  private _timeoutToBeMainNode() {
224
- this._mainNoceMsgTimeoutTimer && clearTimeout(this._mainNoceMsgTimeoutTimer)
238
+ this._mainNodeMsgTimeoutTimer && clearTimeout(this._mainNodeMsgTimeoutTimer)
225
239
  // 超时未收到心跳,认为主节点掉线,申请为主节点
226
- this._mainNoceMsgTimeoutTimer = setTimeout(() => {
240
+ this._mainNodeMsgTimeoutTimer = setTimeout(() => {
227
241
  this._req_beMainNode()
228
242
  }, MessageTimeout * 3)
229
243
  }
@@ -381,8 +395,8 @@ export class BroadcastChannelManager {
381
395
 
382
396
  this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval)
383
397
  this._mainNodeMsgInterval = null
384
- this._mainNoceMsgTimeoutTimer && clearInterval(this._mainNoceMsgTimeoutTimer)
385
- this._mainNoceMsgTimeoutTimer = null
398
+ this._mainNodeMsgTimeoutTimer && clearInterval(this._mainNodeMsgTimeoutTimer)
399
+ this._mainNodeMsgTimeoutTimer = null
386
400
  this._debug && console.log('BC:destroy')
387
401
  }
388
402
  }
@@ -33,7 +33,9 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
33
33
  /** 主节点发送心跳的interval */
34
34
  __publicField(this, "_mainNodeMsgInterval", null);
35
35
  /** 认为主节点掉线的timeout */
36
- __publicField(this, "_mainNoceMsgTimeoutTimer", null);
36
+ __publicField(this, "_mainNodeMsgTimeoutTimer", null);
37
+ /** 更新友方列表的timeout */
38
+ __publicField(this, "_updateFriendListTimer", null);
37
39
  /** 当前实例id */
38
40
  __publicField(this, "id", Date.now() + Math.random());
39
41
  /** 记录的友方id数组 */
@@ -65,6 +67,16 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
65
67
  close() {
66
68
  this._debug && console.log("BC:bc close");
67
69
  this._broadcastChannel && this._broadcastChannel.close();
70
+ this._broadcastChannel = void 0;
71
+ this._updateFriendListTimer && clearTimeout(this._updateFriendListTimer);
72
+ this._updateFriendListTimer = null;
73
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
74
+ this._mainNodeMsgInterval = null;
75
+ this._mainNodeMsgTimeoutTimer && clearTimeout(this._mainNodeMsgTimeoutTimer);
76
+ this._mainNodeMsgTimeoutTimer = null;
77
+ this._oldFrendChannelIdList = [];
78
+ this._friendChannelIdSet.clear();
79
+ this._nodeType = void 0;
68
80
  }
69
81
  /**
70
82
  * 切换节点类型
@@ -88,7 +100,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
88
100
  "Hello world"
89
101
  /* Broadcast */
90
102
  );
91
- setTimeout(() => {
103
+ this._updateFriendListTimer && clearTimeout(this._updateFriendListTimer);
104
+ this._updateFriendListTimer = setTimeout(() => {
92
105
  this._oldFrendChannelIdList = this._getNewFriendList();
93
106
  this._debug && console.log("BC:connect:updateFriendChannelIdList:", this._oldFrendChannelIdList);
94
107
  this.emit("friend list update", {
@@ -187,8 +200,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
187
200
  }
188
201
  }
189
202
  _timeoutToBeMainNode() {
190
- this._mainNoceMsgTimeoutTimer && clearTimeout(this._mainNoceMsgTimeoutTimer);
191
- this._mainNoceMsgTimeoutTimer = setTimeout(() => {
203
+ this._mainNodeMsgTimeoutTimer && clearTimeout(this._mainNodeMsgTimeoutTimer);
204
+ this._mainNodeMsgTimeoutTimer = setTimeout(() => {
192
205
  this._req_beMainNode();
193
206
  }, MessageTimeout * 3);
194
207
  }
@@ -334,8 +347,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
334
347
  this._nodeType = void 0;
335
348
  this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval);
336
349
  this._mainNodeMsgInterval = null;
337
- this._mainNoceMsgTimeoutTimer && clearInterval(this._mainNoceMsgTimeoutTimer);
338
- this._mainNoceMsgTimeoutTimer = null;
350
+ this._mainNodeMsgTimeoutTimer && clearInterval(this._mainNodeMsgTimeoutTimer);
351
+ this._mainNodeMsgTimeoutTimer = null;
339
352
  this._debug && console.log("BC:destroy");
340
353
  }
341
354
  }