@@ -8,7 +8,6 @@ import CONST from '@src/CONST';
8
8
import ONYXKEYS from '@src/ONYXKEYS' ;
9
9
import * as NetworkActions from './actions/Network' ;
10
10
import AppStateMonitor from './AppStateMonitor' ;
11
- import checkInternetReachability from './checkInternetReachability' ;
12
11
import Log from './Log' ;
13
12
14
13
let isOffline = false ;
@@ -43,23 +42,12 @@ function setOfflineStatus(isCurrentlyOffline: boolean, reason = ''): void {
43
42
// When reconnecting, ie, going from offline to online, all the reconnection callbacks
44
43
// are triggered (this is usually Actions that need to re-download data from the server)
45
44
if ( isOffline && ! isCurrentlyOffline ) {
46
- NetworkActions . setIsBackendReachable ( true , 'moved from offline to online' ) ;
47
45
triggerReconnectionCallbacks ( 'offline status changed' ) ;
48
46
}
49
47
50
48
isOffline = isCurrentlyOffline ;
51
49
}
52
50
53
- function setNetWorkStatus ( isInternetReachable : boolean | null ) : void {
54
- let networkStatus ;
55
- if ( ! isBoolean ( isInternetReachable ) ) {
56
- networkStatus = CONST . NETWORK . NETWORK_STATUS . UNKNOWN ;
57
- } else {
58
- networkStatus = isInternetReachable ? CONST . NETWORK . NETWORK_STATUS . ONLINE : CONST . NETWORK . NETWORK_STATUS . OFFLINE ;
59
- }
60
- NetworkActions . setNetWorkStatus ( networkStatus ) ;
61
- }
62
-
63
51
// Update the offline status in response to changes in shouldForceOffline
64
52
let shouldForceOffline = false ;
65
53
Onyx . connect ( {
@@ -103,71 +91,39 @@ Onyx.connect({
103
91
} ) ;
104
92
105
93
/**
106
- * Set interval to periodically (re)check backend status.
107
- * Because backend unreachability might imply lost internet connection, we need to check internet reachability.
108
- * @returns clearInterval cleanup
94
+ * Set up the event listener for NetInfo to tell whether the user has
95
+ * internet connectivity or not. This is more reliable than the Pusher
96
+ * `disconnected` event which takes about 10-15 seconds to emit.
97
+ * @returns unsubscribe method
109
98
*/
110
- function subscribeToBackendAndInternetReachability ( ) : ( ) => void {
111
- const intervalID = setInterval ( ( ) => {
112
- // Offline status also implies backend unreachability
113
- if ( isOffline ) {
114
- // Periodically recheck the network connection
115
- // More info: https://github.com/Expensify/App/issues/42988
116
- recheckNetworkConnection ( ) ;
117
- Log . info ( `[NetworkStatus] Rechecking the network connection with "isOffline" set to "true" to double-check internet reachability.` ) ;
118
- return ;
119
- }
120
- // Using the API url ensures reachability is tested over internet
121
- fetch ( `${ CONFIG . EXPENSIFY . DEFAULT_API_ROOT } api/Ping?accountID=${ accountID || 'unknown' } ` , {
122
- method : 'GET' ,
123
- cache : 'no-cache' ,
124
- } )
125
- . then ( ( response ) => {
99
+ function subscribeToNetInfo ( ) : ( ) => void {
100
+ // Note: We are disabling the configuration for NetInfo when using the local web API since requests can get stuck in a 'Pending' state and are not reliable indicators for "offline".
101
+ // If you need to test the "recheck" feature then switch to the production API proxy server.
102
+ if ( ! CONFIG . IS_USING_LOCAL_WEB ) {
103
+ // Calling NetInfo.configure (re)checks current state. We use it to force a recheck whenever we (re)subscribe
104
+ NetInfo . configure ( {
105
+ // By default, NetInfo uses `/` for `reachabilityUrl`
106
+ // When App is served locally (or from Electron) this address is always reachable - even offline
107
+ // Using the API url ensures reachability is tested over internet
108
+ reachabilityUrl : `${ CONFIG . EXPENSIFY . DEFAULT_API_ROOT } api/Ping?accountID=${ accountID || 'unknown' } ` ,
109
+ reachabilityMethod : 'GET' ,
110
+ reachabilityTest : ( response ) => {
126
111
if ( ! response . ok ) {
127
112
return Promise . resolve ( false ) ;
128
113
}
129
114
return response
130
115
. json ( )
131
116
. then ( ( json ) => Promise . resolve ( json . jsonCode === 200 ) )
132
117
. catch ( ( ) => Promise . resolve ( false ) ) ;
133
- } )
134
- . then ( ( isBackendReachable : boolean ) => {
135
- if ( isBackendReachable ) {
136
- NetworkActions . setIsBackendReachable ( true , 'successfully completed API request' ) ;
137
- return ;
138
- }
139
- NetworkActions . setIsBackendReachable ( false , 'request succeeded, but internet reachability test failed' ) ;
140
- checkInternetReachability ( ) . then ( ( isInternetReachable : boolean ) => {
141
- setOfflineStatus ( ! isInternetReachable , 'checkInternetReachability was called after api/ping returned a non-200 jsonCode' ) ;
142
- setNetWorkStatus ( isInternetReachable ) ;
143
- } ) ;
144
- } )
145
- . catch ( ( ) => {
146
- NetworkActions . setIsBackendReachable ( false , 'request failed and internet reachability test failed' ) ;
147
- checkInternetReachability ( ) . then ( ( isInternetReachable : boolean ) => {
148
- setOfflineStatus ( ! isInternetReachable , 'checkInternetReachability was called after api/ping request failed' ) ;
149
- setNetWorkStatus ( isInternetReachable ) ;
150
- } ) ;
151
- } ) ;
152
- } , CONST . NETWORK . BACKEND_CHECK_INTERVAL_MS ) ;
153
-
154
- return ( ) => {
155
- clearInterval ( intervalID ) ;
156
- } ;
157
- }
118
+ } ,
158
119
159
- /**
160
- * Monitor internet connectivity and perform periodic backend reachability checks
161
- * @returns unsubscribe method
162
- */
163
- function subscribeToNetworkStatus ( ) : ( ) => void {
164
- // Note: We are disabling the reachability check when using the local web API since requests can get stuck in a 'Pending' state and are not reliable indicators for reachability.
165
- // If you need to test the "recheck" feature then switch to the production API proxy server.
166
- const unsubscribeFromBackendReachability = ! CONFIG . IS_USING_LOCAL_WEB ? subscribeToBackendAndInternetReachability ( ) : undefined ;
120
+ // If a check is taking longer than this time we're considered offline
121
+ reachabilityRequestTimeout : CONST . NETWORK . MAX_PENDING_TIME_MS ,
122
+ } ) ;
123
+ }
167
124
168
- // Set up the event listener for NetInfo to tell whether the user has
169
- // internet connectivity or not. This is more reliable than the Pusher
170
- // `disconnected` event which takes about 10-15 seconds to emit.
125
+ // Subscribe to the state change event via NetInfo so we can update
126
+ // whether a user has internet connectivity or not.
171
127
const unsubscribeNetInfo = NetInfo . addEventListener ( ( state ) => {
172
128
Log . info ( '[NetworkConnection] NetInfo state change' , false , { ...state } ) ;
173
129
if ( shouldForceOffline ) {
@@ -176,11 +132,27 @@ function subscribeToNetworkStatus(): () => void {
176
132
}
177
133
setOfflineStatus ( state . isInternetReachable === false , 'NetInfo received a state change event' ) ;
178
134
Log . info ( `[NetworkStatus] NetInfo.addEventListener event coming, setting "offlineStatus" to ${ ! ! state . isInternetReachable } with network state: ${ JSON . stringify ( state ) } ` ) ;
179
- setNetWorkStatus ( state . isInternetReachable ) ;
135
+ let networkStatus ;
136
+ if ( ! isBoolean ( state . isInternetReachable ) ) {
137
+ networkStatus = CONST . NETWORK . NETWORK_STATUS . UNKNOWN ;
138
+ } else {
139
+ networkStatus = state . isInternetReachable ? CONST . NETWORK . NETWORK_STATUS . ONLINE : CONST . NETWORK . NETWORK_STATUS . OFFLINE ;
140
+ }
141
+ NetworkActions . setNetWorkStatus ( networkStatus ) ;
180
142
} ) ;
181
143
144
+ // Periodically recheck the network connection
145
+ // More info: https://github.com/Expensify/App/issues/42988
146
+ const recheckIntervalID = setInterval ( ( ) => {
147
+ if ( ! isOffline ) {
148
+ return ;
149
+ }
150
+ recheckNetworkConnection ( ) ;
151
+ Log . info ( `[NetworkStatus] Rechecking the network connection with "isOffline" set to "true" to double-check internet reachability.` ) ;
152
+ } , CONST . NETWORK . RECHECK_INTERVAL_MS ) ;
153
+
182
154
return ( ) => {
183
- unsubscribeFromBackendReachability ?. ( ) ;
155
+ clearInterval ( recheckIntervalID ) ;
184
156
unsubscribeNetInfo ( ) ;
185
157
} ;
186
158
}
@@ -231,6 +203,6 @@ export default {
231
203
onReconnect,
232
204
triggerReconnectionCallbacks,
233
205
recheckNetworkConnection,
234
- subscribeToNetworkStatus ,
206
+ subscribeToNetInfo ,
235
207
} ;
236
208
export type { NetworkStatus } ;
0 commit comments