@@ -4,20 +4,17 @@ import EventHandler from "./event-handler.js?v=$version";
4
4
5
5
export async function init ( id , options ) {
6
6
const { invoke, method, interval = 3000 , url, connectionId } = options ;
7
- const localStorageKey = 'bb_hub_el_id' ;
8
- if ( localStorage . getItem ( localStorageKey ) === null ) {
9
- localStorage . setItem ( localStorageKey , id ) ;
7
+ const elKey = 'bb_hub_el_id' ;
8
+ if ( localStorage . getItem ( elKey ) === null ) {
9
+ localStorage . setItem ( elKey , id ) ;
10
10
}
11
11
12
- const localStorageConnectionIdKey = 'bb_hub_connection_id' ;
13
- let clientId = localStorage . getItem ( localStorageConnectionIdKey ) ;
12
+ const connectionIdKey = 'bb_hub_connection_id' ;
13
+ let clientId = localStorage . getItem ( connectionIdKey ) ;
14
14
if ( clientId === null ) {
15
- localStorage . setItem ( localStorageConnectionIdKey , connectionId ) ;
15
+ localStorage . setItem ( connectionIdKey , connectionId ) ;
16
16
clientId = connectionId ;
17
17
}
18
- window . addEventListener ( 'unload' , ( ) => {
19
- dispose ( id ) ;
20
- } ) ;
21
18
22
19
const hubs = [ ] ;
23
20
const chanel = new BroadcastChannel ( 'bb_hubs_chanel' ) ;
@@ -27,44 +24,42 @@ export async function init(id, options) {
27
24
hubs . push ( id ) ;
28
25
}
29
26
else if ( type === 'dispose' ) {
30
- const index = hubs . indexOf ( v => v === id ) ;
27
+ const index = hubs . indexOf ( id ) ;
31
28
if ( index > - 1 ) {
32
29
hubs . splice ( index , 1 ) ;
33
30
}
34
- if ( clientId === connectionId ) {
35
- localStorage . removeItem ( localStorageConnectionIdKey ) ;
36
- }
37
- if ( localStorage . getItem ( localStorageKey ) === id ) {
38
- localStorage . removeItem ( localStorageKey ) ;
31
+ if ( localStorage . getItem ( elKey ) === id ) {
32
+ localStorage . removeItem ( elKey ) ;
39
33
}
40
34
}
41
35
} ) ;
42
36
43
37
const info = await getClientInfo ( url ) ;
44
38
info . id = clientId ;
45
- const handler = setInterval ( async ( ) => {
39
+
40
+ const callback = async ( ) => {
46
41
chanel . postMessage ( { id, type : 'ping' } ) ;
47
- let hubId = localStorage . getItem ( localStorageKey ) ;
48
42
49
- if ( hubId === null ) {
50
- localStorage . setItem ( localStorageKey , id ) ;
43
+ let hubId = localStorage . getItem ( elKey ) ;
44
+ if ( hubId === null || hubs . length === 0 ) {
45
+ localStorage . setItem ( elKey , id ) ;
51
46
hubId = id ;
52
47
}
53
48
if ( hubId === id ) {
54
49
await invoke . invokeMethodAsync ( method , info ) ;
55
50
}
56
- else if ( hubs . length > 0 ) {
57
- const h = hubs . find ( v => v === hubId ) ;
58
- if ( h === void 0 ) {
59
- localStorage . removeItem ( localStorageKey ) ;
60
- }
61
- }
62
- else {
63
- localStorage . removeItem ( localStorageKey ) ;
64
- }
51
+ }
52
+ await callback ( ) ;
53
+
54
+ const handler = setInterval ( async ( ) => {
55
+ await callback ( ) ;
65
56
} , interval ) ;
66
57
67
- const hub = { handler, chanel, connectionId } ;
58
+ window . addEventListener ( 'unload' , ( ) => {
59
+ dispose ( id ) ;
60
+ } ) ;
61
+
62
+ const hub = { handler, chanel } ;
68
63
Data . set ( id , hub ) ;
69
64
}
70
65
0 commit comments