Skip to content

Commit

Permalink
Log connection changes
Browse files Browse the repository at this point in the history
  • Loading branch information
VickyStash committed Dec 9, 2024
1 parent 43330f8 commit 5e52653
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/libs/actions/Network.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {differenceInHours} from 'date-fns/differenceInHours';
import Onyx from 'react-native-onyx';
import Log from '@libs/Log';
import type {NetworkStatus} from '@libs/NetworkConnection';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ConnectionChanges} from '@src/types/onyx/Network';

let isPoorConnectionSimulated: boolean | undefined;
let connectionChanges: ConnectionChanges | undefined;
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (value) => {
Expand All @@ -20,16 +23,43 @@ Onyx.connect({
}

isPoorConnectionSimulated = !!value.shouldSimulatePoorConnection;
connectionChanges = value.connectionChanges;
},
});

function trackConnectionChanges() {
if (!connectionChanges?.startTime) {
Onyx.merge(ONYXKEYS.NETWORK, {connectionChanges: {startTime: new Date().getTime(), amount: 1}});
return;
}

const diffInHours = differenceInHours(new Date(), connectionChanges.startTime);
const newAmount = (connectionChanges.amount ?? 0) + 1;

if (diffInHours < 1) {
Onyx.merge(ONYXKEYS.NETWORK, {connectionChanges: {amount: newAmount}});
return;
}

Log.info(
`[NetworkConnection] Connection has changed ${newAmount} time(s) for the last ${diffInHours} hour(s). Poor connection simulation is turned ${
isPoorConnectionSimulated ? 'on' : 'off'
}`,
);

Onyx.merge(ONYXKEYS.NETWORK, {connectionChanges: {startTime: new Date().getTime(), amount: 0}});
}

function setIsOffline(isOffline: boolean, reason = '') {
if (reason) {
let textToLog = '[Network] Client is';
textToLog += isOffline ? ' entering offline mode' : ' back online';
textToLog += ` because: ${reason}`;
Log.info(textToLog);
}

trackConnectionChanges();

Onyx.merge(ONYXKEYS.NETWORK, {isOffline});
}

Expand Down
13 changes: 13 additions & 0 deletions src/types/onyx/Network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import type {NetworkStatus} from '@libs/NetworkConnection';

/** The value where connection changes are tracked */
type ConnectionChanges = {
/** Amount of connection changes */
amount?: number;

/** Start time in milliseconds */
startTime?: number;
};

/** Model of network state */
type Network = {
/** Is the network currently offline or not */
Expand All @@ -14,6 +23,9 @@ type Network = {
/** Poor connection timeout id */
poorConnectionTimeoutID?: NodeJS.Timeout;

/** The value where connection changes are tracked */
connectionChanges?: ConnectionChanges;

/** Whether we should fail all network requests */
shouldFailAllRequests?: boolean;

Expand All @@ -25,3 +37,4 @@ type Network = {
};

export default Network;
export type {ConnectionChanges};

0 comments on commit 5e52653

Please sign in to comment.