Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions apps/mobile/src/features/updates/app-updates.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { AppState } from "react-native";
import { describe, expect, it, vi } from "vite-plus/test";

import {
createAppUpdateDeferral,
createAppUpdateForegroundRecheck,
createAppUpdateLaunchCheck,
FOREGROUND_APP_UPDATE_RECHECK_AFTER_MS,
registerHiddenUpdateTap,
Expand All @@ -19,6 +21,13 @@ vi.mock("expo-updates", () => ({
reloadAsync: vi.fn(),
}));

vi.mock("react-native", () => ({
AppState: {
currentState: "active",
addEventListener: vi.fn(() => ({ remove: vi.fn() })),
},
}));

function makeUpdateClient(overrides: Partial<AppUpdateClient> = {}): AppUpdateClient {
return {
isEnabled: true,
Expand Down Expand Up @@ -640,3 +649,31 @@ describe("registerHiddenUpdateTap", () => {
expect(count).toBe(0);
});
});

describe("createAppUpdateForegroundRecheck", () => {
it("registers once and checks after returning from a long background stay", () => {
vi.stubGlobal("__DEV__", false);
const now = vi.spyOn(Date, "now");
const listener = vi.mocked(AppState.addEventListener);
listener.mockClear();
const client = makeUpdateClient();
const start = createAppUpdateForegroundRecheck(client, createAppUpdateDeferral());

try {
start();
start();
expect(listener).toHaveBeenCalledOnce();
const onChange = listener.mock.calls[0]![1];
now.mockReturnValue(1000);
onChange("background");
now.mockReturnValue(1000 + FOREGROUND_APP_UPDATE_RECHECK_AFTER_MS);
onChange("active");
expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
onChange("active");
expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
} finally {
now.mockRestore();
vi.unstubAllGlobals();
}
});
});
85 changes: 40 additions & 45 deletions apps/mobile/src/features/updates/app-updates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as Updates from "expo-updates";
// Metro namespace imports evaluate unrelated React Native getters, including unavailable native modules.
import { AppState } from "react-native";

import {
type AtomCommandResult,
Expand Down Expand Up @@ -418,22 +420,19 @@ async function defaultFlushPendingWrites(): Promise<void> {
async function defaultIsSafeToRestartInBackground(): Promise<boolean> {
const { isForegroundHandoffActive } = await import("../../lib/foreground-handoff");
if (isForegroundHandoffActive()) return false;
const { AppState } = await import("react-native");
return AppState.currentState === "background";
}

function defaultOnNextBackground(apply: () => void, includeCurrent: boolean): void {
void import("react-native").then(({ AppState }) => {
const subscription = AppState.addEventListener("change", (state) => {
if (state !== "background") return;
subscription.remove();
apply();
});
if (includeCurrent && AppState.currentState === "background") {
subscription.remove();
apply();
}
const subscription = AppState.addEventListener("change", (state) => {
if (state !== "background") return;
subscription.remove();
apply();
});
if (includeCurrent && AppState.currentState === "background") {
subscription.remove();
apply();
}
}

export const DEFERRED_INSTALL_PROMPT_AFTER_MS = 30 * 60 * 1000;
Expand All @@ -443,25 +442,23 @@ export const DEFERRED_INSTALL_PROMPT_AFTER_MS = 30 * 60 * 1000;
* the timer running.
*/
function defaultOnForegroundStay(apply: () => void): void {
void import("react-native").then(({ AppState }) => {
let timer: ReturnType<typeof setTimeout> | undefined;
const arm = () => {
timer ??= setTimeout(() => {
subscription.remove();
apply();
}, DEFERRED_INSTALL_PROMPT_AFTER_MS);
};
const disarm = () => {
if (timer === undefined) return;
clearTimeout(timer);
timer = undefined;
};
const subscription = AppState.addEventListener("change", (state) => {
if (state === "active") arm();
else if (state === "background") disarm();
});
if (AppState.currentState === "active") arm();
let timer: ReturnType<typeof setTimeout> | undefined;
const arm = () => {
timer ??= setTimeout(() => {
subscription.remove();
apply();
}, DEFERRED_INSTALL_PROMPT_AFTER_MS);
};
const disarm = () => {
if (timer === undefined) return;
clearTimeout(timer);
timer = undefined;
};
const subscription = AppState.addEventListener("change", (state) => {
if (state === "active") arm();
else if (state === "background") disarm();
});
if (AppState.currentState === "active") arm();
}

const defaultAppUpdateEnvironment: AppUpdateEnvironment = {
Expand Down Expand Up @@ -528,22 +525,20 @@ export function createAppUpdateForegroundRecheck(
return () => {
if (started || !isAppUpdateCheckAvailable(client)) return;
started = true;
void import("react-native").then(({ AppState }) => {
let backgroundedAtMs: number | null = null;
AppState.addEventListener("change", (state) => {
if (state === "background") {
backgroundedAtMs = Date.now();
return;
}
if (state !== "active") return;
const shouldCheck = shouldRecheckAppUpdateOnForeground(
backgroundedAtMs,
Date.now(),
deferral.pendingInstall,
);
backgroundedAtMs = null;
if (shouldCheck) void runAppUpdateCheck({ client, deferral });
});
let backgroundedAtMs: number | null = null;
AppState.addEventListener("change", (state) => {
if (state === "background") {
backgroundedAtMs = Date.now();
return;
}
if (state !== "active") return;
const shouldCheck = shouldRecheckAppUpdateOnForeground(
backgroundedAtMs,
Date.now(),
deferral.pendingInstall,
);
backgroundedAtMs = null;
if (shouldCheck) void runAppUpdateCheck({ client, deferral });
});
};
}
Expand Down
Loading