Skip to content

Commit

Permalink
Applying reviewer feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
VickyStash committed Dec 11, 2024
1 parent 4f0f128 commit e2ed846
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/libs/NetworkConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/libs/actions/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
Expand Down

0 comments on commit e2ed846

Please sign in to comment.