@devries-cameron20 / t3code Mirror · read-only / commits / ec585729e0
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 20:45 UTC

fix(mobile): prevent update checker from crashing iOS at launch (#22488)

Julius Marminge committed Oct 20, 2026 at 13:30 UTC ec585729e0a48c318f4cfb0c232b816c029d0541
2 files changed +77 -45
apps/mobile/src/features/updates/app-updates.test.ts
+37
@@ -1,7 +1,9 @@
1 +import { AppState } from "react-native";
2 import { describe, expect, it, vi } from "vite-plus/test";
3
4 import {
5 createAppUpdateDeferral,
6 + createAppUpdateForegroundRecheck,
7 createAppUpdateLaunchCheck,
8 FOREGROUND_APP_UPDATE_RECHECK_AFTER_MS,
9 registerHiddenUpdateTap,
@@ -19,6 +21,13 @@ vi.mock("expo-updates", () => ({
21 reloadAsync: vi.fn(),
22 }));
23
24 +vi.mock("react-native", () => ({
25 + AppState: {
26 + currentState: "active",
27 + addEventListener: vi.fn(() => ({ remove: vi.fn() })),
28 + },
29 +}));
30 +
31 function makeUpdateClient(overrides: Partial<AppUpdateClient> = {}): AppUpdateClient {
32 return {
33 isEnabled: true,
@@ -640,3 +649,31 @@ describe("registerHiddenUpdateTap", () => {
649 expect(count).toBe(0);
650 });
651 });
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
+40 -45
@@ -1,4 +1,6 @@
1 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";
4
5 import {
6 type AtomCommandResult,
@@ -418,22 +420,19 @@ async function defaultFlushPendingWrites(): Promise<void> {
420 async function defaultIsSafeToRestartInBackground(): Promise<boolean> {
421 const { isForegroundHandoffActive } = await import("../../lib/foreground-handoff");
422 if (isForegroundHandoffActive()) return false;
421 - const { AppState } = await import("react-native");
423 return AppState.currentState === "background";
424 }
425
426 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();
431 });
432 + if (includeCurrent && AppState.currentState === "background") {
433 + subscription.remove();
434 + apply();
435 + }
436 }
437
438 export const DEFERRED_INSTALL_PROMPT_AFTER_MS = 30 * 60 * 1000;
@@ -443,25 +442,23 @@ export const DEFERRED_INSTALL_PROMPT_AFTER_MS = 30 * 60 * 1000;
442 * the timer running.
443 */
444 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();
460 });
461 + if (AppState.currentState === "active") arm();
462 }
463
464 const defaultAppUpdateEnvironment: AppUpdateEnvironment = {
@@ -528,22 +525,20 @@ export function createAppUpdateForegroundRecheck(
525 return () => {
526 if (started || !isAppUpdateCheckAvailable(client)) return;
527 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 });
542 });
543 };
544 }