@havue/solutions 1.1.1 → 1.2.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 (28) hide show
  1. package/bc-connect/__tests__/bc-connect.spec.ts +73 -0
  2. package/bc-connect/dist/bc-connect.mjs +89 -73
  3. package/bc-connect/dist/bc-connect.umd.js +89 -73
  4. package/bc-connect/dist/types/src/manager.d.ts +72 -62
  5. package/bc-connect/package.json +1 -1
  6. package/bc-connect/src/manager.ts +103 -78
  7. package/dist/solutions.full.js +156 -127
  8. package/dist/solutions.full.min.js +4 -4
  9. package/dist/solutions.full.min.js.map +1 -1
  10. package/dist/types/bc-connect/src/manager.d.ts +72 -62
  11. package/dist/types/ws-video-manager/src/loader/websocket-loader.d.ts +13 -10
  12. package/dist/types/ws-video-manager/src/manager/index.d.ts +54 -37
  13. package/dist/types/ws-video-manager/src/render/drawer.d.ts +7 -7
  14. package/dist/types/ws-video-manager/src/render/index.d.ts +35 -19
  15. package/package.json +4 -4
  16. package/vite.config.ts +1 -1
  17. package/ws-video-manager/dist/types/src/loader/websocket-loader.d.ts +13 -10
  18. package/ws-video-manager/dist/types/src/manager/index.d.ts +54 -37
  19. package/ws-video-manager/dist/types/src/render/drawer.d.ts +7 -7
  20. package/ws-video-manager/dist/types/src/render/index.d.ts +35 -19
  21. package/ws-video-manager/dist/ws-video-manager.mjs +67 -54
  22. package/ws-video-manager/dist/ws-video-manager.umd.js +67 -54
  23. package/ws-video-manager/package.json +1 -1
  24. package/ws-video-manager/src/loader/websocket-loader.ts +15 -11
  25. package/ws-video-manager/src/manager/index.ts +57 -40
  26. package/ws-video-manager/src/render/drawer.ts +22 -20
  27. package/ws-video-manager/src/render/index.ts +61 -27
  28. package/ws-video-manager/src/render/mp4box.ts +1 -1
@@ -1,70 +1,80 @@
1
1
  // #region typedefine
2
- /** 事件数据类型 */
2
+ /** 事件数据类型 | Event Datatypes */
3
3
  export type BcConnectSendMessageType = {
4
- /** 事件类型 */
4
+ /** 事件类型 | type */
5
5
  type: string
6
- /** 数据 */
6
+ /** 数据 | data */
7
7
  data: any
8
- /** 发送事件的实例id */
8
+ /** 发送事件的实例id | BroadcastChannelManager instance id */
9
9
  id: number
10
- /** 消息是发送给目标实例id */
10
+ /** 此消息的目标实例id | The target instance id for this message */
11
11
  targetId?: number
12
12
  }
13
13
 
14
- /** 事件类型 */
14
+ /** 事件类型 | Event types */
15
15
  export enum BcConnectEventTypeEnum {
16
- /** 初始广播 */
17
- Broadcast = 'Hello world',
18
- /** 回复初始广播 */
19
- Broadcast_Reply = 'I can hear you',
20
- /** 主节点心跳 */
21
- Main_Node_Hearbeat = '苍天还在,别立黄天',
22
- /** 回复主节点心跳 */
23
- Res_Main_Node_Hearbeat = '苍天在上,受我一拜',
24
- /** 长时间未收到主节点心跳,我想当主节点,你们同意吗 */
25
- Req_Be_Main_Node = '苍天已死,黄天当立',
26
- /** 排资论辈,我应是主节点,不同意 */
27
- Res_Be_Main_Node = '我是黄天,尔等退下',
28
- /** 当前BC节点类型更改 */
29
- Node_Type_Change = 'node type change',
30
- /** 其他标签页BC节点id列表更新 */
31
- Friend_List_Update = 'friend list update'
16
+ /** 初始广播 | Initial broadcast */
17
+ Broadcast = '__BCM_INIT__',
18
+ /** 回复初始广播 | Reply to initial broadcast */
19
+ Broadcast_Reply = '__BCM_INIT_REPLY__',
20
+ /** 主节点心跳 | Master node heartbeat */
21
+ Main_Node_Hearbeat = '__BCM_MAIN_NODE_HEARBEAT__',
22
+ /** 回复主节点心跳 | Reply to the master node heartbeat */
23
+ Res_Main_Node_Hearbeat = '__BCM_MAIN_NODE_HEARBEAT_REPLY__',
24
+ /**
25
+ * 长时间未收到主节点心跳,申请成为主节点
26
+ * It has not received the heartbeat of the master node for a long time.
27
+ * Apply to become the master node
28
+ */
29
+ Req_Be_Main_Node = '__BCM_REQ_BE_MAIN_NODE__',
30
+ /** 拒绝其他节点成为主节点 | Reject other nodes as master nodes */
31
+ Res_Be_Main_Node = '__BCM_REQ_BE_MAIN_NODE_REJECT__',
32
+ /** 当前节点类型更改 | The current node type has changed */
33
+ Node_Type_Change = '__BCM_NODE_TYPE_CHANGE__',
34
+ /** 其他标签页BC节点id列表更新 | Other TAB node id list updated */
35
+ Friend_List_Update = '__BCM_FRIEND_LIST_UPDATE__'
32
36
  }
33
37
 
34
- /** 当前webview BroadcastChannel节点类型 */
38
+ /** BroadcastChannel节点类型 | BroadcastChannel node type */
35
39
  export enum BcConnectNodeTypeEnum {
36
40
  Main = 'main',
37
41
  Normal = 'normal'
38
42
  }
39
43
  // #endregion typedefine
40
44
 
41
- // 消息超时时间
45
+ // 消息超时时间 | Message timeout time
42
46
  const MessageTimeout = 300
43
47
 
44
48
  /**
45
49
  * 使用BroadcastChannel与其他标签页进行通信
50
+ * Use BroadcastChannel to communicate with other tabs
46
51
  */
47
52
  export class BroadcastChannelManager {
48
- /** 通道名称 */
53
+ /** 通道名称 | Channel name */
49
54
  private _bcName: string
50
- /** BroadcastChannel实例 */
55
+ /** BroadcastChannel instance */
51
56
  private _broadcastChannel: BroadcastChannel | undefined = undefined
52
- /** 事件map */
57
+ /** Event map */
53
58
  private _eventMap: Map<string, Array<(_: BcConnectSendMessageType) => void>>
54
- /** 主节点发送心跳的interval */
59
+ /** 主节点发送心跳的interval | The interval at which the master node sends the heartbeat */
55
60
  private _mainNodeMsgInterval: number | null = null
56
- /** 认为主节点掉线的timeout */
57
- private _mainNoceMsgTimeoutTimer: number | null = null
58
- /** 当前实例id */
61
+ /** 认为主节点掉线的timeout | timeout to consider the primary node to be offline */
62
+ private _mainNodeMsgTimeoutTimer: number | null = null
63
+ /** 更新友方列表的timeout | Update the timeout of the friend list */
64
+ private _updateFriendListTimer: number | null = null
65
+ /** 当前实例id | Current instance id */
59
66
  public id: number = Date.now() + Math.random()
60
- /** 记录的友方id数组 */
67
+ /** 其他广播通道id列表 | List of other broadcast channel ids */
61
68
  private _oldFrendChannelIdList: Array<number> = []
62
- /** 正在更新的右方id数组 */
69
+ /** 正在更新的id数组 | The id array being updated */
63
70
  private _friendChannelIdSet: Set<number> = new Set()
64
- /** 当前节点类型 */
71
+ /** 当前节点类型 | Current node type */
65
72
  private _nodeType: BcConnectNodeTypeEnum | undefined = undefined
66
73
 
67
- /** 是否开启调试模式,会在控制台打印相关信息 */
74
+ /**
75
+ * 是否开启调试模式,会在控制台打印相关信息
76
+ * If debug mode is enabled, it will print information to the console
77
+ */
68
78
  private _debug: boolean = false
69
79
 
70
80
  constructor(name: string, debug: boolean = false) {
@@ -93,10 +103,20 @@ export class BroadcastChannelManager {
93
103
  public close() {
94
104
  this._debug && console.log('BC:bc close')
95
105
  this._broadcastChannel && this._broadcastChannel.close()
106
+ this._broadcastChannel = undefined
107
+ this._updateFriendListTimer && clearTimeout(this._updateFriendListTimer)
108
+ this._updateFriendListTimer = null
109
+ this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval)
110
+ this._mainNodeMsgInterval = null
111
+ this._mainNodeMsgTimeoutTimer && clearTimeout(this._mainNodeMsgTimeoutTimer)
112
+ this._mainNodeMsgTimeoutTimer = null
113
+ this._oldFrendChannelIdList = []
114
+ this._friendChannelIdSet.clear()
115
+ this._nodeType = undefined
96
116
  }
97
117
 
98
118
  /**
99
- * 切换节点类型
119
+ * 切换节点类型 | Switching node type
100
120
  * @param {BcConnectNodeTypeEnum} type
101
121
  * @returns
102
122
  */
@@ -112,12 +132,14 @@ export class BroadcastChannelManager {
112
132
  })
113
133
  }
114
134
 
115
- /** 更新友方列表 */
135
+ /** 更新广播id列表 | Update the list of broadcast ids */
116
136
  private _updateFriendList() {
117
- // 广播告知己方存在
137
+ // 广播告知己方存在 | Broadcast your presence
118
138
  this.send(BcConnectEventTypeEnum.Broadcast)
119
139
 
120
- setTimeout(() => {
140
+ this._updateFriendListTimer && clearTimeout(this._updateFriendListTimer)
141
+
142
+ this._updateFriendListTimer = setTimeout(() => {
121
143
  this._oldFrendChannelIdList = this._getNewFriendList()
122
144
  this._debug && console.log('BC:connect:updateFriendChannelIdList:', this._oldFrendChannelIdList)
123
145
  this.emit(BcConnectEventTypeEnum.Friend_List_Update, {
@@ -128,7 +150,7 @@ export class BroadcastChannelManager {
128
150
  this._updataNodeType()
129
151
  }, MessageTimeout)
130
152
  }
131
- /** 绑定事件 */
153
+ /** 绑定事件 | Bind event */
132
154
  private _bindBroadcastChannelEvent() {
133
155
  this._broadcastChannel &&
134
156
  (this._broadcastChannel.onmessage = (event) => {
@@ -139,7 +161,7 @@ export class BroadcastChannelManager {
139
161
  this.emit(type, event.data)
140
162
  })
141
163
 
142
- // 收到世界呼唤
164
+ // 收到初始广播 | Receiving the initial broadcast
143
165
  this.on(BcConnectEventTypeEnum.Broadcast, (data) => {
144
166
  const { id } = data
145
167
  if (!this._friendChannelIdSet.has(id)) {
@@ -149,13 +171,13 @@ export class BroadcastChannelManager {
149
171
  this.sendToTarget(BcConnectEventTypeEnum.Broadcast_Reply, id)
150
172
  })
151
173
 
152
- // 收到友方存在
174
+ // 收到初始广播回复 | The initial broadcast reply is received
153
175
  this.on(BcConnectEventTypeEnum.Broadcast_Reply, (data) => {
154
176
  const { id } = data
155
177
  this._addFriend(id)
156
178
  })
157
179
 
158
- // 收到其他申请为主节点
180
+ // 收到其他节点申请为主节点 | Others apply for the master node
159
181
  this.on(BcConnectEventTypeEnum.Req_Be_Main_Node, (data) => {
160
182
  const { id } = data
161
183
  if (id > this.id) {
@@ -163,7 +185,7 @@ export class BroadcastChannelManager {
163
185
  }
164
186
  })
165
187
 
166
- // 收到其他节点回复主节点心跳
188
+ // 收到主节点心跳回复 | Received the master node heartbeat reply
167
189
  this.on(BcConnectEventTypeEnum.Res_Main_Node_Hearbeat, (data) => {
168
190
  this._addFriend(data.id)
169
191
  })
@@ -171,7 +193,7 @@ export class BroadcastChannelManager {
171
193
  this._bindNodeEvent()
172
194
  }
173
195
 
174
- /** 监听节点类型切换事件 */
196
+ /** 监听节点类型切换事件 | */
175
197
  private _bindNodeEvent() {
176
198
  const onMainNodeHearbeat = (data: BcConnectSendMessageType) => {
177
199
  this._timeoutToBeMainNode()
@@ -183,9 +205,9 @@ export class BroadcastChannelManager {
183
205
  this.on(BcConnectEventTypeEnum.Node_Type_Change, (info) => {
184
206
  const { data } = info
185
207
  this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval)
186
- this._debug && console.log('BC:代理类型切换:', info.data)
208
+ this._debug && console.log('BC:NODE_TYPE_CHANGE:', info.data)
187
209
  if (data === BcConnectNodeTypeEnum.Main) {
188
- // 定时发送主节点心跳
210
+ // 定时发送主节点心跳 | The heartbeat of the master node is sent periodically
189
211
  this._mainNodeMsgInterval = setInterval(() => {
190
212
  this._catchOldFriend()
191
213
  this.send(BcConnectEventTypeEnum.Main_Node_Hearbeat)
@@ -193,17 +215,17 @@ export class BroadcastChannelManager {
193
215
  } else if (data === BcConnectNodeTypeEnum.Normal) {
194
216
  this._timeoutToBeMainNode()
195
217
  }
196
- // 收到主节点心跳, 重新更新友方列表
218
+ // 收到主节点心跳, 重新更新友方列表 | Update the friend list after receiving the heartbeat of the master node
197
219
  this.on(BcConnectEventTypeEnum.Main_Node_Hearbeat, onMainNodeHearbeat)
198
220
  })
199
221
  }
200
222
 
201
- /** 获取最新的友方列表 */
223
+ /** 获取最新的节点列表 | Get the latest node list */
202
224
  private _getNewFriendList() {
203
225
  return [...this._friendChannelIdSet].sort((a, b) => a - b)
204
226
  }
205
227
  /**
206
- * 更新当前节点类型
228
+ * 更新当前节点类型 | Update the current node type
207
229
  */
208
230
  private _updataNodeType() {
209
231
  this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval)
@@ -221,16 +243,17 @@ export class BroadcastChannelManager {
221
243
  }
222
244
 
223
245
  private _timeoutToBeMainNode() {
224
- this._mainNoceMsgTimeoutTimer && clearTimeout(this._mainNoceMsgTimeoutTimer)
246
+ this._mainNodeMsgTimeoutTimer && clearTimeout(this._mainNodeMsgTimeoutTimer)
225
247
  // 超时未收到心跳,认为主节点掉线,申请为主节点
226
- this._mainNoceMsgTimeoutTimer = setTimeout(() => {
248
+ // If no heartbeat is received after the timeout, the master node is considered to be offline and the master node is applied
249
+ this._mainNodeMsgTimeoutTimer = setTimeout(() => {
227
250
  this._req_beMainNode()
228
251
  }, MessageTimeout * 3)
229
252
  }
230
253
 
231
254
  /**
232
- * 保持记录的活跃的友方id列表
233
- * 清空正在记录的友方id列表
255
+ * 保存最新的节点列表到_oldFrendChannelIdList,清空_friendChannelIdSet
256
+ * Save the latest node list to _oldFrendChannelIdList and clear _friendChannelIdSet
234
257
  */
235
258
  private _catchOldFriend() {
236
259
  const newFriendList = this._getNewFriendList()
@@ -245,7 +268,7 @@ export class BroadcastChannelManager {
245
268
  }
246
269
 
247
270
  if (this._nodeType === BcConnectNodeTypeEnum.Main && Math.min(...this._oldFrendChannelIdList) < this.id) {
248
- // 有更小的id,不再为主节点
271
+ // 有更小的id,不再为主节点 | Has a smaller id and is no longer a master node
249
272
  this._setNodeType(BcConnectNodeTypeEnum.Normal)
250
273
  }
251
274
 
@@ -253,20 +276,22 @@ export class BroadcastChannelManager {
253
276
  }
254
277
 
255
278
  /**
256
- * 申请成为主节点
279
+ * 申请成为主节点 | Apply to be a master node
257
280
  */
258
281
  private _req_beMainNode() {
259
282
  this._debug && console.log('BC:req_beMainNode')
260
283
 
261
- // 向所有id友方节点发送申请
284
+ // 向所有节点申请成为主节点 | Apply to all nodes to become master nodes
262
285
  this.send(BcConnectEventTypeEnum.Req_Be_Main_Node)
263
286
 
264
287
  // 如果长时间未回复,认为自己可以当主节点
288
+ // If there is no reply for a long time, it considers itself to be the master
265
289
  const timer = setTimeout(() => {
266
290
  this._setNodeType(BcConnectNodeTypeEnum.Main)
267
291
  }, MessageTimeout)
268
292
 
269
293
  // 收到拒绝回复,清空timeout
294
+ // Clear the timeout when you receive a rejection reply
270
295
  const handleRes_beMainNode = () => {
271
296
  clearTimeout(timer)
272
297
  this.off(BcConnectEventTypeEnum.Res_Be_Main_Node, handleRes_beMainNode)
@@ -276,8 +301,8 @@ export class BroadcastChannelManager {
276
301
  }
277
302
 
278
303
  /**
279
- * 添加友方
280
- * @param id 节点id
304
+ * add node
305
+ * @param id id
281
306
  */
282
307
  public _addFriend(id: number) {
283
308
  if (!this._friendChannelIdSet.has(id)) {
@@ -286,9 +311,9 @@ export class BroadcastChannelManager {
286
311
  }
287
312
 
288
313
  /**
289
- * 广播消息
290
- * @param type 消息类型
291
- * @param data 数据
314
+ * 广播消息 | Send Broadcast message
315
+ * @param type 消息类型 | Message type
316
+ * @param data 数据 | data
292
317
  */
293
318
  public send(type: string, data?: any) {
294
319
  this._broadcastChannel?.postMessage({
@@ -299,10 +324,10 @@ export class BroadcastChannelManager {
299
324
  }
300
325
 
301
326
  /**
302
- * 给特定id的节点发送消息
303
- * @param type 消息类型
304
- * @param targetId 目标节点id
305
- * @param data 数据
327
+ * 给特定id的节点发送消息 | Send a message to a node with a specific id
328
+ * @param type 消息类型 | Message type
329
+ * @param targetId 目标节点id | Target Node id
330
+ * @param data 数据 | data
306
331
  */
307
332
  public sendToTarget(type: string, targetId: number, data?: any) {
308
333
  this._broadcastChannel?.postMessage({
@@ -314,9 +339,9 @@ export class BroadcastChannelManager {
314
339
  }
315
340
 
316
341
  /**
317
- * 注册事件
318
- * @param { string } event 事件类型
319
- * @param callback 回调
342
+ * 注册事件 | Registering events
343
+ * @param { string } event 事件类型 | Event type
344
+ * @param callback 回调 | callback
320
345
  * @returns void
321
346
  */
322
347
  public on(event: string, callback: (_: BcConnectSendMessageType) => void) {
@@ -333,9 +358,9 @@ export class BroadcastChannelManager {
333
358
  }
334
359
 
335
360
  /**
336
- * 注销事件
337
- * @param { string } event 事件类型
338
- * @param callback 事件回调
361
+ * 注销事件 | Remove events
362
+ * @param { string } event 事件类型 | Event type
363
+ * @param callback 事件回调 | callback
339
364
  * @returns
340
365
  */
341
366
  public off(event: string, callback?: (_: BcConnectSendMessageType) => void) {
@@ -354,9 +379,9 @@ export class BroadcastChannelManager {
354
379
  }
355
380
 
356
381
  /**
357
- * 触发事件
358
- * @param { string } event 事件类型
359
- * @param data 数据
382
+ * 触发事件 | Triggering events
383
+ * @param { string } event 事件类型 | Event type
384
+ * @param data 数据 | data
360
385
  */
361
386
  public emit(event: string, data: BcConnectSendMessageType) {
362
387
  const callbacks = this._eventMap.get(event) || []
@@ -367,7 +392,7 @@ export class BroadcastChannelManager {
367
392
  }
368
393
 
369
394
  /**
370
- * 销毁
395
+ * 销毁 | destroy
371
396
  */
372
397
  public destroy() {
373
398
  this._bcName = ''
@@ -381,8 +406,8 @@ export class BroadcastChannelManager {
381
406
 
382
407
  this._mainNodeMsgInterval && clearInterval(this._mainNodeMsgInterval)
383
408
  this._mainNodeMsgInterval = null
384
- this._mainNoceMsgTimeoutTimer && clearInterval(this._mainNoceMsgTimeoutTimer)
385
- this._mainNoceMsgTimeoutTimer = null
409
+ this._mainNodeMsgTimeoutTimer && clearInterval(this._mainNodeMsgTimeoutTimer)
410
+ this._mainNodeMsgTimeoutTimer = null
386
411
  this._debug && console.log('BC:destroy')
387
412
  }
388
413
  }