Skip to content

Commit ec58572

Browse files
fix(mobile): prevent update checker from crashing iOS at launch (#22488)
1 parent 0c85b0d commit ec58572

2 files changed

Lines changed: 77 additions & 45 deletions

File tree

‎apps/mobile/src/features/updates/app-updates.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { AppState } from "react-native";
12
import { describe, expect, it, vi } from "vite-plus/test";
23

34
import {
45
createAppUpdateDeferral,
6+
createAppUpdateForegroundRecheck,
57
createAppUpdateLaunchCheck,
68
FOREGROUND_APP_UPDATE_RECHECK_AFTER_MS,
79
registerHiddenUpdateTap,
@@ -19,6 +21,13 @@ vi.mock("expo-updates", () => ({
1921
reloadAsync: vi.fn(),
2022
}));
2123

24+
vi.mock("react-native", () => ({
25+
AppState: {
26+
currentState: "active",
27+
addEventListener: vi.fn(() => ({ remove: vi.fn() })),
28+
},
29+
}));
30+
2231
function makeUpdateClient(overrides: Partial<AppUpdateClient> = {}): AppUpdateClient {
2332
return {
2433
isEnabled: true,
@@ -640,3 +649,31 @@ describe("registerHiddenUpdateTap", () => {
640649
expect(count).toBe(0);
641650
});
642651
});
652+
653+
describe("createAppUpdateForegroundRecheck", () => {
654+
it("registers once and checks after returning from a long background stay", () => {
655+
vi.stubGlobal("__DEV__", false);
656+
const now = vi.spyOn(Date, "now");
657+
const listener = vi.mocked(AppState.addEventListener);
658+
listener.mockClear();
659+
const client = makeUpdateClient();
660+
const start = createAppUpdateForegroundRecheck(client, createAppUpdateDeferral());
661+
662+
try {
663+
start();
664+
start();
665+
expect(listener).toHaveBeenCalledOnce();
666+
const onChange = listener.mock.calls[0]![1];
667+
now.mockReturnValue(1000);
668+
onChange("background");
669+
now.mockReturnValue(1000 + FOREGROUND_APP_UPDATE_RECHECK_AFTER_MS);
670+
onChange("active");
671+
expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
672+
onChange("active");
673+
expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
674+
} finally {
675+
now.mockRestore();
676+
vi.unstubAllGlobals();
677+
}
678+
});
679+
});

‎apps/mobile/src/features/updates/app-updates.ts‎

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as Updates from "expo-updates";
2+
// Metro namespace imports evaluate unrelated React Native getters, including unavailable native modules.
3+
import { AppState } from "react-native";
24

35
import {
46
type AtomCommandResult,
@@ -418,22 +420,19 @@ async function defaultFlushPendingWrites(): Promise<void> {
418420
async function defaultIsSafeToRestartInBackground(): Promise<boolean> {
419421
const { isForegroundHandoffActive } = await import("../../lib/foreground-handoff");
420422
if (isForegroundHandoffActive()) return false;
421-
const { AppState } = await import("react-native");
422423
return AppState.currentState === "background";
423424
}
424425

425426
function defaultOnNextBackground(apply: () => void, includeCurrent: boolean): void {
426-
void import("react-native").then(({ AppState }) => {
427-
const subscription = AppState.addEventListener("change", (state) => {
428-
if (state !== "background") return;
429-
subscription.remove();
430-
apply();
431-
});
432-
if (includeCurrent && AppState.currentState === "background") {
433-
subscription.remove();
434-
apply();
435-
}
427+
const subscription = AppState.addEventListener("change", (state) => {
428+
if (state !== "background") return;
429+
subscription.remove();
430+
apply();
436431
});
432+
if (includeCurrent && AppState.currentState === "background") {
433+
subscription.remove();
434+
apply();
435+
}
437436
}
438437

439438
export const DEFERRED_INSTALL_PROMPT_AFTER_MS = 30 * 60 * 1000;
@@ -443,25 +442,23 @@ export const DEFERRED_INSTALL_PROMPT_AFTER_MS = 30 * 60 * 1000;
443442
* the timer running.
444443
*/
445444
function defaultOnForegroundStay(apply: () => void): void {
446-
void import("react-native").then(({ AppState }) => {
447-
let timer: ReturnType<typeof setTimeout> | undefined;
448-
const arm = () => {
449-
timer ??= setTimeout(() => {
450-
subscription.remove();
451-
apply();
452-
}, DEFERRED_INSTALL_PROMPT_AFTER_MS);
453-
};
454-
const disarm = () => {
455-
if (timer === undefined) return;
456-
clearTimeout(timer);
457-
timer = undefined;
458-
};
459-
const subscription = AppState.addEventListener("change", (state) => {
460-
if (state === "active") arm();
461-
else if (state === "background") disarm();
462-
});
463-
if (AppState.currentState === "active") arm();
445+
let timer: ReturnType<typeof setTimeout> | undefined;
446+
const arm = () => {
447+
timer ??= setTimeout(() => {
448+
subscription.remove();
449+
apply();
450+
}, DEFERRED_INSTALL_PROMPT_AFTER_MS);
451+
};
452+
const disarm = () => {
453+
if (timer === undefined) return;
454+
clearTimeout(timer);
455+
timer = undefined;
456+
};
457+
const subscription = AppState.addEventListener("change", (state) => {
458+
if (state === "active") arm();
459+
else if (state === "background") disarm();
464460
});
461+
if (AppState.currentState === "active") arm();
465462
}
466463

467464
const defaultAppUpdateEnvironment: AppUpdateEnvironment = {
@@ -528,22 +525,20 @@ export function createAppUpdateForegroundRecheck(
528525
return () => {
529526
if (started || !isAppUpdateCheckAvailable(client)) return;
530527
started = true;
531-
void import("react-native").then(({ AppState }) => {
532-
let backgroundedAtMs: number | null = null;
533-
AppState.addEventListener("change", (state) => {
534-
if (state === "background") {
535-
backgroundedAtMs = Date.now();
536-
return;
537-
}
538-
if (state !== "active") return;
539-
const shouldCheck = shouldRecheckAppUpdateOnForeground(
540-
backgroundedAtMs,
541-
Date.now(),
542-
deferral.pendingInstall,
543-
);
544-
backgroundedAtMs = null;
545-
if (shouldCheck) void runAppUpdateCheck({ client, deferral });
546-
});
528+
let backgroundedAtMs: number | null = null;
529+
AppState.addEventListener("change", (state) => {
530+
if (state === "background") {
531+
backgroundedAtMs = Date.now();
532+
return;
533+
}
534+
if (state !== "active") return;
535+
const shouldCheck = shouldRecheckAppUpdateOnForeground(
536+
backgroundedAtMs,
537+
Date.now(),
538+
deferral.pendingInstall,
539+
);
540+
backgroundedAtMs = null;
541+
if (shouldCheck) void runAppUpdateCheck({ client, deferral });
547542
});
548543
};
549544
}

0 commit comments

Comments
 (0)