5
5
*/
6
6
import { Str } from 'expensify-common' ;
7
7
import Onyx from 'react-native-onyx' ;
8
+ import { isOpeningRouteInDesktop , resetIsOpeningRouteInDesktop } from '@libs/Browser/index.website' ;
9
+ import Log from '@libs/Log' ;
8
10
import * as ActiveClients from '@userActions/ActiveClients' ;
9
11
import ONYXKEYS from '@src/ONYXKEYS' ;
10
12
import type { Init , IsClientTheLeader , IsReady } from './types' ;
11
- import Log from '@libs/Log' ;
12
- import { isDevelopment } from '@libs/Environment/Environment' ;
13
13
14
14
const clientID = Str . guid ( ) ;
15
15
const maxClients = 20 ;
@@ -18,8 +18,8 @@ let resolveSavedSelfPromise: () => void;
18
18
const savedSelfPromise = new Promise < void > ( ( resolve ) => {
19
19
resolveSavedSelfPromise = resolve ;
20
20
} ) ;
21
- // The first time before unload is triggered by `openRouteInDesktopApp` function so we don't want to clear the clientID in this case.
22
- let shouldPreventBeforeUnload = ! isDevelopment ( ) ;
21
+ let beforeunloadListenerAdded = false ;
22
+
23
23
/**
24
24
* Determines when the client is ready. We need to wait both till we saved our ID in onyx AND the init method was called
25
25
*/
@@ -34,7 +34,7 @@ Onyx.connect({
34
34
35
35
activeClients = val ;
36
36
37
- console . log ( " [Active Clients] the client value is changed to" , activeClients ) ;
37
+ console . log ( ' [Active Clients] the client value is changed to' , activeClients ) ;
38
38
// Remove from the beginning of the list any clients that are past the limit, to avoid having thousands of them
39
39
let removed = false ;
40
40
while ( activeClients . length >= maxClients ) {
@@ -70,15 +70,10 @@ const isClientTheLeader: IsClientTheLeader = () => {
70
70
71
71
Log . info ( `[Active Clients] checking the leader lastActiveClient=${ lastActiveClient } clientID=${ clientID } ` ) ;
72
72
73
-
74
73
return lastActiveClient === clientID ;
75
74
} ;
76
75
77
76
const cleanUpClientId = ( ) => {
78
- if ( shouldPreventBeforeUnload ) {
79
- shouldPreventBeforeUnload = false ;
80
- return ;
81
- }
82
77
isPromotingNewLeader = isClientTheLeader ( ) ;
83
78
activeClients = activeClients . filter ( ( id ) => id !== clientID ) ;
84
79
Log . info ( `[Active Clients] removed a client here clientID=${ clientID } ` ) ;
@@ -89,18 +84,36 @@ const cleanUpClientId = () => {
89
84
} ) ;
90
85
} ;
91
86
87
+ const removeBeforeUnloadListener = ( ) => {
88
+ if ( ! beforeunloadListenerAdded ) {
89
+ return ;
90
+ }
91
+ beforeunloadListenerAdded = false ;
92
+ window . removeEventListener ( 'beforeunload' , cleanUpClientId ) ;
93
+ } ;
94
+
92
95
/**
93
96
* Add our client ID to the list of active IDs.
94
97
* We want to ensure we have no duplicates and that the activeClient gets added at the end of the array (see isClientTheLeader)
95
98
*/
96
99
const init : Init = ( ) => {
100
+ removeBeforeUnloadListener ( ) ;
97
101
activeClients = activeClients . filter ( ( id ) => id !== clientID ) ;
98
102
activeClients . push ( clientID ) ;
99
103
Log . info ( `[Active Clients] added a client here clientID=${ clientID } ` ) ;
100
104
console . log ( JSON . parse ( JSON . stringify ( activeClients ) ) ) ;
101
105
ActiveClients . setActiveClients ( activeClients ) . then ( resolveSavedSelfPromise ) ;
102
106
103
- window . addEventListener ( 'beforeunload' , cleanUpClientId ) ;
107
+ beforeunloadListenerAdded = true ;
108
+ window . addEventListener ( 'beforeunload' , ( ) => {
109
+ // When we open route in desktop, beforeunload is fired unexpectedly here.
110
+ // So we should return early in this case to prevent cleaning the clientID
111
+ if ( isOpeningRouteInDesktop ( ) ) {
112
+ resetIsOpeningRouteInDesktop ( ) ;
113
+ return ;
114
+ }
115
+ cleanUpClientId ( ) ;
116
+ } ) ;
104
117
} ;
105
118
106
119
export { init , isClientTheLeader , isReady } ;
0 commit comments