TypeScript
fix(mobile): prevent update checker from crashing iOS at launch (#22488)
Julius Marmingeec585729e0a4
diff --git a/apps/mobile/src/features/updates/app-updates.test.ts b/apps/mobile/src/features/updates/app-updates.test.tsindex e35f173a472..d980164b32d 100644--- a/apps/mobile/src/features/updates/app-updates.test.ts+++ b/apps/mobile/src/features/updates/app-updates.test.ts@@ -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,@@ -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,@@ -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();+ }+ });+});diff --git a/apps/mobile/src/features/updates/app-updates.ts b/apps/mobile/src/features/updates/app-updates.tsindex 2cfe3aa7aeb..6390256108e 100644--- a/apps/mobile/src/features/updates/app-updates.ts+++ b/apps/mobile/src/features/updates/app-updates.ts@@ -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,@@ -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;@@ -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 = {@@ -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 });});};}