Skip to content
17 changes: 11 additions & 6 deletions ts/features/connectivity/saga/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { updateMixpanelSuperProperties } from "../../../mixpanelConfig/superProp
import { GlobalState } from "../../../store/reducers/types";
import { ConnectivityClient, createConnectivityClient } from "../api/client";
import { apiUrlPrefix } from "../../../config";
import { appCurrentStateSelector } from "../../../store/reducers/appState";

const CONNECTIVITY_STATUS_LOAD_INTERVAL = (30 * 1000) as Millisecond;
const CONNECTIVITY_STATUS_FAILURE_INTERVAL = (10 * 1000) as Millisecond;
const CONNECTIVITY_STATUS_BACKGROUND_INTERVAL = 1000 as Millisecond;

function* checkBackendConnectionStatus(
client: ConnectivityClient
Expand All @@ -36,6 +38,13 @@ export function* connectionStatusSaga(
> {
while (true) {
try {
const appState = yield* select(appCurrentStateSelector);

if (appState !== "active") {
// if the app is not active we wait for the next check
yield* call(startTimer, CONNECTIVITY_STATUS_BACKGROUND_INTERVAL);
continue;
}
const libraryResponse = yield* call(fetchNetInfoState());

if (E.isRight(libraryResponse)) {
Expand All @@ -58,15 +67,11 @@ export function* connectionStatusSaga(
yield* call(startTimer, CONNECTIVITY_STATUS_LOAD_INTERVAL);
continue;
}
yield* call(startTimer, CONNECTIVITY_STATUS_FAILURE_INTERVAL);
continue;
}
yield* call(startTimer, CONNECTIVITY_STATUS_FAILURE_INTERVAL);
continue;
} catch (e) {
yield* call(startTimer, CONNECTIVITY_STATUS_FAILURE_INTERVAL);
continue;
// we ignore errors and treat them as a connection failure
}
yield* call(startTimer, CONNECTIVITY_STATUS_FAILURE_INTERVAL);
}
}

Expand Down
Loading