Skip to content

Commit 2a43228

Browse files
fix(mobile): handle OTA rollbacks and duplicate update taps
- Prevent concurrent update checks before the disabled state re-renders - Download and restart when Expo reports a rollback directive
1 parent 0e88bb4 commit 2a43228

1 file changed

Lines changed: 49 additions & 32 deletions

File tree

‎apps/mobile/src/features/settings/SettingsRouteScreen.tsx‎

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { NativeStackScreenOptions } from "../../native/StackHeader";
88
import { SymbolView } from "../../components/AppSymbol";
99
import * as Effect from "effect/Effect";
1010
import { AsyncResult } from "effect/unstable/reactivity";
11-
import { useCallback, useEffect, useMemo, useState, useSyncExternalStore } from "react";
11+
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
1212
import {
1313
ActivityIndicator,
1414
Alert,
@@ -584,6 +584,7 @@ type UpdateCheckState = "idle" | "checking" | "downloading" | "restarting" | "cu
584584
function AppSettingsSection() {
585585
const icon = useThemeColor("--color-icon");
586586
const [updateState, setUpdateState] = useState<UpdateCheckState>("idle");
587+
const updateInFlight = useRef(false);
587588

588589
const version = Constants.expoConfig?.version ?? "0.0.0";
589590
// Fall back to "production" to match resolveAppVariant in app.config.ts, so a
@@ -614,37 +615,14 @@ function AppSettingsSection() {
614615
}, [updateState]);
615616

616617
const checkForUpdate = useCallback(async () => {
617-
setUpdateState("checking");
618-
const check = await settlePromise(() => Updates.checkForUpdateAsync());
619-
if (check._tag === "Failure") {
620-
reportUpdateFailure(check, "Could not check for updates.");
621-
setUpdateState("idle");
622-
return;
623-
}
624-
if (!check.value.isAvailable) {
625-
setUpdateState("current");
626-
return;
627-
}
628-
629-
setUpdateState("downloading");
630-
const fetched = await settlePromise(() => Updates.fetchUpdateAsync());
631-
if (fetched._tag === "Failure") {
632-
reportUpdateFailure(fetched, "Could not download the update.");
633-
setUpdateState("idle");
634-
return;
635-
}
636-
if (!fetched.value.isNew) {
637-
setUpdateState("current");
638-
return;
639-
}
640-
641-
setUpdateState("restarting");
642-
// reloadAsync never resolves on success — the JS context is torn down — so
643-
// reaching the failure branch below is the only way this returns.
644-
const reloaded = await settlePromise(() => Updates.reloadAsync());
645-
if (reloaded._tag === "Failure") {
646-
reportUpdateFailure(reloaded, "Downloaded, but could not restart the app.");
647-
setUpdateState("idle");
618+
// `disabled={busy}` only takes effect on the next render, so two taps in the
619+
// same frame would both get through. The ref closes that window.
620+
if (updateInFlight.current) return;
621+
updateInFlight.current = true;
622+
try {
623+
await runUpdateCheck(setUpdateState);
624+
} finally {
625+
updateInFlight.current = false;
648626
}
649627
}, []);
650628

@@ -713,6 +691,45 @@ function AppSettingsSection() {
713691
);
714692
}
715693

694+
async function runUpdateCheck(setUpdateState: (state: UpdateCheckState) => void): Promise<void> {
695+
setUpdateState("checking");
696+
const check = await settlePromise(() => Updates.checkForUpdateAsync());
697+
if (check._tag === "Failure") {
698+
reportUpdateFailure(check, "Could not check for updates.");
699+
setUpdateState("idle");
700+
return;
701+
}
702+
// A rollback directive (`eas update:rollback`) arrives as isAvailable: false
703+
// with isRollBackToEmbedded: true — there is nothing newer to install, but the
704+
// running OTA still has to be dropped for the embedded bundle.
705+
if (!check.value.isAvailable && !check.value.isRollBackToEmbedded) {
706+
setUpdateState("current");
707+
return;
708+
}
709+
710+
setUpdateState("downloading");
711+
const fetched = await settlePromise(() => Updates.fetchUpdateAsync());
712+
if (fetched._tag === "Failure") {
713+
reportUpdateFailure(fetched, "Could not download the update.");
714+
setUpdateState("idle");
715+
return;
716+
}
717+
// isNew is always false for a rollback, so it can't be the sole gate here either.
718+
if (!fetched.value.isNew && !fetched.value.isRollBackToEmbedded) {
719+
setUpdateState("current");
720+
return;
721+
}
722+
723+
setUpdateState("restarting");
724+
// reloadAsync never resolves on success — the JS context is torn down — so
725+
// reaching the failure branch below is the only way this returns.
726+
const reloaded = await settlePromise(() => Updates.reloadAsync());
727+
if (reloaded._tag === "Failure") {
728+
reportUpdateFailure(reloaded, "Downloaded, but could not restart the app.");
729+
setUpdateState("idle");
730+
}
731+
}
732+
716733
function reportUpdateFailure(result: AtomCommandResult<unknown, unknown>, fallback: string): void {
717734
reportAtomCommandResult(result, { label: "app update check" });
718735
if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;

0 commit comments

Comments
 (0)