Skip to content

Commit f64ca20

Browse files
refactor(mobile): settings screens read settled results instead of promise chains
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9ccf6d9 commit f64ca20

4 files changed

Lines changed: 39 additions & 42 deletions

File tree

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Dialog from "../../components/ui/Dialog";
22
import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
33
import Constants from "expo-constants";
4+
import { settlePromise } from "@t3tools/client-runtime/state/runtime";
45
import { useEffect, useRef, useState } from "react";
56
import { Pressable, View } from "react-native";
67
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -56,26 +57,28 @@ function AppSettingsSection() {
5657
return () => clearTimeout(timer);
5758
}, [updateState]);
5859

59-
function checkForUpdate() {
60+
async function checkForUpdate() {
6061
// `disabled={busy}` only takes effect on the next render, so two taps in the
6162
// same frame would both get through. The ref closes that window.
6263
if (updateInFlight.current) return;
6364
updateInFlight.current = true;
64-
void runAppUpdateCheck({
65-
applyMode: "immediate",
66-
onFailure: (message) => Dialog.alert("Update failed", message),
67-
onStateChange: setUpdateState,
68-
}).finally(() => {
69-
updateInFlight.current = false;
70-
});
65+
// Failures arrive through onFailure; the settled result only matters for unlocking.
66+
await settlePromise(() =>
67+
runAppUpdateCheck({
68+
applyMode: "immediate",
69+
onFailure: (message) => Dialog.alert("Update failed", message),
70+
onStateChange: setUpdateState,
71+
}),
72+
);
73+
updateInFlight.current = false;
7174
}
7275

7376
function handleVersionPress() {
7477
if (!updateCheckAvailable || updateInFlight.current) return;
7578
const tap = registerHiddenUpdateTap(hiddenUpdateTapCount.current);
7679
hiddenUpdateTapCount.current = tap.nextCount;
7780
if (tap.shouldCheck) {
78-
checkForUpdate();
81+
void checkForUpdate();
7982
}
8083
}
8184

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

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
type EnvironmentId,
88
type ServerProvider,
99
} from "@t3tools/contracts";
10-
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
10+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
1111
import { AsyncResult } from "effect/reactivity";
1212
import { useEffect, useRef, useState } from "react";
1313
import { View } from "react-native";
@@ -96,16 +96,15 @@ function EnvironmentDetail({ environmentId }: { readonly environmentId: Environm
9696
setPending(label);
9797
setError(null);
9898
setNotice(null);
99-
await action()
100-
.catch((cause: unknown) => {
101-
setError(
102-
cause instanceof Error ? cause.message : "The action could not be completed. Try again.",
103-
);
104-
})
105-
.finally(() => {
106-
pendingRef.current = false;
107-
setPending(null);
108-
});
99+
const result = await settlePromise(action);
100+
pendingRef.current = false;
101+
setPending(null);
102+
if (AsyncResult.isFailure(result)) {
103+
const cause = squashAtomCommandFailure(result);
104+
setError(
105+
cause instanceof Error ? cause.message : "The action could not be completed. Try again.",
106+
);
107+
}
109108
}
110109

111110
function requestServerUpdate() {
@@ -269,14 +268,14 @@ function EnvironmentDetail({ environmentId }: { readonly environmentId: Environm
269268
checkController.current = controller;
270269
const timeout = setTimeout(() => controller.abort(), 20_000);
271270
const fromVersion = config.environment.serverVersion;
272-
const targetVersion = await findEnvironmentUpdate(
273-
fromVersion,
274-
controller.signal,
275-
).finally(() => {
276-
clearTimeout(timeout);
277-
checkController.current = null;
278-
});
279-
setRelease({ fromVersion, targetVersion });
271+
const result = await settlePromise(() =>
272+
findEnvironmentUpdate(fromVersion, controller.signal),
273+
);
274+
clearTimeout(timeout);
275+
checkController.current = null;
276+
// `run` owns the error message, so a failed lookup rethrows to it.
277+
if (AsyncResult.isFailure(result)) throw squashAtomCommandFailure(result);
278+
setRelease({ fromVersion, targetVersion: result.value });
280279
});
281280
}}
282281
/>

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,14 @@ function ConfiguredSettingsNotificationsRouteScreen() {
370370
setLiveActivityStatus("disabled");
371371
}
372372

373-
const handleLiveActivitiesChange = (enabled: boolean) => {
373+
// Both writers settle their own failures, so awaiting them never throws.
374+
const handleLiveActivitiesChange = async (enabled: boolean) => {
374375
if (liveActivityWriteInFlight.current) return;
375376
if (!enabled) {
376377
liveActivityWriteInFlight.current = true;
377378
setLiveActivityStatus("linking");
378-
void disableLiveActivities().finally(() => {
379-
liveActivityWriteInFlight.current = false;
380-
});
379+
await disableLiveActivities();
380+
liveActivityWriteInFlight.current = false;
381381
return;
382382
}
383383

@@ -387,9 +387,8 @@ function ConfiguredSettingsNotificationsRouteScreen() {
387387
}
388388

389389
liveActivityWriteInFlight.current = true;
390-
void linkEnvironments().finally(() => {
391-
liveActivityWriteInFlight.current = false;
392-
});
390+
await linkEnvironments();
391+
liveActivityWriteInFlight.current = false;
393392
};
394393

395394
return (
@@ -441,13 +440,13 @@ function ConfiguredSettingsNotificationsRouteScreen() {
441440
(liveActivityStatus === "enabled" || liveActivityStatus === "linking") &&
442441
deviceRegistered
443442
}
444-
onValueChange={handleLiveActivitiesChange}
443+
onValueChange={(enabled) => void handleLiveActivitiesChange(enabled)}
445444
/>
446445
{liveActivityStatus === "signed-out" && canClearLiveActivitiesPreference ? (
447446
<SettingsRow
448447
icon="bolt.circle"
449448
label="Turn off Live Activity preference"
450-
onPress={() => handleLiveActivitiesChange(false)}
449+
onPress={() => void handleLiveActivitiesChange(false)}
451450
/>
452451
) : null}
453452
{supportsAndroidLiveUpdateSettings() ? (

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,9 @@ function ProviderAccount({
127127
pendingRef.current = true;
128128
setPending(true);
129129
setError(null);
130-
const result = await command().catch(() => null);
130+
const result = await command();
131131
pendingRef.current = false;
132132
setPending(false);
133-
if (result === null) {
134-
setError("Could not update provider sign-in.");
135-
return false;
136-
}
137133
if (result._tag === "Success") return true;
138134
if (!isAtomCommandInterrupted(result)) {
139135
const failure = squashAtomCommandFailure(result);

0 commit comments

Comments
 (0)