diff --git a/src/libs/NetworkConnection.ts b/src/libs/NetworkConnection.ts index c5d22e19dc28..d5e0b356d7ac 100644 --- a/src/libs/NetworkConnection.ts +++ b/src/libs/NetworkConnection.ts @@ -7,6 +7,7 @@ import type {ValueOf} from 'type-fest'; import CONFIG from '@src/CONFIG'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; +import type Network from '@src/types/onyx/Network'; import type {ConnectionChanges} from '@src/types/onyx/Network'; import * as NetworkActions from './actions/Network'; import AppStateMonitor from './AppStateMonitor'; @@ -76,24 +77,7 @@ Onyx.connect({ return; } - // Starts random network status change when shouldSimulatePoorConnection is turned into true - // or after app restart if shouldSimulatePoorConnection is true already - if (!isPoorConnectionSimulated && !!network.shouldSimulatePoorConnection) { - clearTimeout(network.poorConnectionTimeoutID); - setRandomNetworkStatus(true); - } - - // Fetch the NetInfo state to set the correct offline status when shouldSimulatePoorConnection is turned into false - if (isPoorConnectionSimulated && !network.shouldSimulatePoorConnection) { - NetInfo.fetch().then((state) => { - const isInternetUnreachable = !state.isInternetReachable; - const stringifiedState = JSON.stringify(state); - setOfflineStatus(isInternetUnreachable || !isServerUp, 'NetInfo checked if the internet is reachable'); - Log.info( - `[NetworkStatus] The poor connection simulation mode was turned off. Getting the device network status from NetInfo. Network state: ${stringifiedState}. Setting the offline status to: ${isInternetUnreachable}.`, - ); - }); - } + simulatePoorConnection(network); isPoorConnectionSimulated = !!network.shouldSimulatePoorConnection; connectionChanges = network.connectionChanges; @@ -132,6 +116,28 @@ Onyx.connect({ }, }); +/** Controls poor connection simulation */ +function simulatePoorConnection(network: Network) { + // Starts random network status change when shouldSimulatePoorConnection is turned into true + // or after app restart if shouldSimulatePoorConnection is true already + if (!isPoorConnectionSimulated && !!network.shouldSimulatePoorConnection) { + clearTimeout(network.poorConnectionTimeoutID); + setRandomNetworkStatus(true); + } + + // Fetch the NetInfo state to set the correct offline status when shouldSimulatePoorConnection is turned into false + if (isPoorConnectionSimulated && !network.shouldSimulatePoorConnection) { + NetInfo.fetch().then((state) => { + const isInternetUnreachable = !state.isInternetReachable; + const stringifiedState = JSON.stringify(state); + setOfflineStatus(isInternetUnreachable || !isServerUp, 'NetInfo checked if the internet is reachable'); + Log.info( + `[NetworkStatus] The poor connection simulation mode was turned off. Getting the device network status from NetInfo. Network state: ${stringifiedState}. Setting the offline status to: ${isInternetUnreachable}.`, + ); + }); + } +} + /** Sets online/offline connection randomly every 2-5 seconds */ function setRandomNetworkStatus(initialCall = false) { // The check to ensure no new timeouts are scheduled after poor connection simulation is stopped diff --git a/src/libs/actions/Network.ts b/src/libs/actions/Network.ts index 5bfcd411975f..f2228a008dad 100644 --- a/src/libs/actions/Network.ts +++ b/src/libs/actions/Network.ts @@ -41,6 +41,7 @@ function setShouldSimulatePoorConnection(shouldSimulatePoorConnection: boolean, if (!shouldSimulatePoorConnection) { clearTimeout(poorConnectionTimeoutID); Onyx.merge(ONYXKEYS.NETWORK, {shouldSimulatePoorConnection, poorConnectionTimeoutID: undefined}); + return; } Onyx.merge(ONYXKEYS.NETWORK, {shouldSimulatePoorConnection}); }