@devries-cameron20 / t3code-1 / commits / 5811d5c846
Fork 0

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

refactor(mobile): app-lifetime services install once, and the usage widget stops re-rendering on every presentation change (#25740)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 26, 2026 at 21:20 UTC 5811d5c846e8fae712da46a741870e620717a234
6 files changed +153 -57
apps/mobile/src/App.tsx
+10 -13
@@ -40,8 +40,9 @@ import {
40 type FoundationHotModule,
41 } from "./lib/foundation-fast-refresh";
42 import { useMobileNavigationTheme } from "./lib/useMobileNavigationTheme";
43 -import { useUiRuntimeMemoryWarningGc } from "./lib/useUiRuntimeMemoryWarningGc";
43 +import { installUiRuntimeMemoryWarningGc } from "./lib/useUiRuntimeMemoryWarningGc";
44 import { SubscriptionUsageCoordinator } from "./widgets/SubscriptionUsageCoordinator";
45 +import { installSubscriptionUsageRefresh } from "./widgets/useSubscriptionUsage";
46 import { GlobalVoiceInputControl } from "./features/voice-input/GlobalVoiceInputControl";
47 import { restoreSettingsLinkState } from "./features/settings/settings-navigation";
48 import {
@@ -99,6 +100,14 @@ function handleNavigationReady(): void {
100 declare const module: { readonly hot?: FoundationHotModule } | undefined;
101
102 const appServiceDisposers = [
103 + installUiRuntimeMemoryWarningGc(),
104 + subscribeComposerDraftModelReconciliation({
105 + registry: appAtomRegistry,
106 + threadShellAtom: environmentThreadShells.threadShellAtom,
107 + threadRefsAtom: environmentThreadShells.threadRefsAtom,
108 + shellStateAtom: environmentShell.stateAtom,
109 + }),
110 + installSubscriptionUsageRefresh(),
111 installPermissionUpdateNotice(),
112 installThreadOutboxDelivery(),
113 installComposerAttachmentUploads(),
@@ -118,18 +127,6 @@ function SplashScreenCoordinator() {
127 }
128
129 export default function App() {
121 - useUiRuntimeMemoryWarningGc();
122 - useEffect(
123 - () =>
124 - subscribeComposerDraftModelReconciliation({
125 - registry: appAtomRegistry,
126 - threadShellAtom: environmentThreadShells.threadShellAtom,
127 - threadRefsAtom: environmentThreadShells.threadRefsAtom,
128 - shellStateAtom: environmentShell.stateAtom,
129 - }),
130 - [],
131 - );
132 -
130 return (
131 <RegistryContext.Provider value={appAtomRegistry}>
132 <CloudAuthProvider>
apps/mobile/src/lib/appState.ts
+2 -2
@@ -3,12 +3,12 @@ import { AccessibilityInfo, AppState } from "react-native";
3
4 type Listener = () => void;
5
6 -function subscribeAppState(onChange: Listener): () => void {
6 +export function subscribeAppState(onChange: Listener): () => void {
7 const subscription = AppState.addEventListener("change", () => onChange());
8 return () => subscription.remove();
9 }
10
11 -function isAppActive(): boolean {
11 +export function isAppActive(): boolean {
12 return AppState.currentState === "active";
13 }
14
apps/mobile/src/lib/useUiRuntimeMemoryWarningGc.ts
+5 -8
@@ -1,4 +1,3 @@
1 -import { useEffect } from "react";
1 import { AppState } from "react-native";
2 import { scheduleOnUI } from "react-native-worklets";
3
@@ -9,11 +8,9 @@ function collectUiRuntimeGarbage() {
8 }
9
10 /** iOS memory warnings collect only RN's heap; collect worklets too to release shadow nodes and serializables. */
12 -export function useUiRuntimeMemoryWarningGc() {
13 - useEffect(() => {
14 - const subscription = AppState.addEventListener("memoryWarning", () => {
15 - scheduleOnUI(collectUiRuntimeGarbage);
16 - });
17 - return () => subscription.remove();
18 - }, []);
11 +export function installUiRuntimeMemoryWarningGc(): () => void {
12 + const subscription = AppState.addEventListener("memoryWarning", () => {
13 + scheduleOnUI(collectUiRuntimeGarbage);
14 + });
15 + return () => subscription.remove();
16 }
apps/mobile/src/widgets/SubscriptionUsageCoordinator.tsx
-2
@@ -7,7 +7,6 @@ import { useAppearancePreferences } from "../features/settings/appearance/Appear
7 import { environmentCatalog } from "../connection/catalog";
8 import { environmentPresentations } from "../state/presentation";
9 import { publishSubscriptionUsage } from "./publishSubscriptionUsage";
10 -import { useSubscriptionUsage } from "./useSubscriptionUsage";
10 import { buildSubscriptionUsageSnapshot } from "./subscriptionUsageSnapshot";
11 import { createWidgetTheme } from "./widgetTheme";
12
@@ -29,7 +28,6 @@ export function SubscriptionUsageCoordinator() {
28 } = useAppearancePreferences();
29 const catalog = useAtomValue(environmentCatalog.catalogValueAtom);
30 const snapshot = useAtomValue(snapshotAtom);
32 - useSubscriptionUsage(catalog.isReady);
31 const androidAppearance = Platform.OS === "android" ? themeAppearance : null;
32 useEffect(() => {
33 if (!catalog.isReady || !appearanceReady) return;
apps/mobile/src/widgets/useSubscriptionUsage.test.ts new
+97
@@ -0,0 +1,97 @@
1 +import { EnvironmentId } from "@t3tools/contracts";
2 +import { Atom, AtomRegistry } from "effect/reactivity";
3 +import { describe, expect, it, vi } from "vite-plus/test";
4 +
5 +const fixture = vi.hoisted(() => ({
6 + appState: "active",
7 + appStateListeners: new Set<() => void>(),
8 + refreshed: [] as string[],
9 +}));
10 +
11 +vi.mock("react-native", () => ({
12 + AppState: {
13 + get currentState() {
14 + return fixture.appState;
15 + },
16 + addEventListener: (_event: string, listener: () => void) => {
17 + fixture.appStateListeners.add(listener);
18 + return { remove: () => fixture.appStateListeners.delete(listener) };
19 + },
20 + },
21 + AccessibilityInfo: { addEventListener: vi.fn(), isReduceMotionEnabled: vi.fn() },
22 +}));
23 +
24 +vi.mock("../state/atom-registry", async () => {
25 + const { AtomRegistry } = await import("effect/reactivity");
26 + return { appAtomRegistry: AtomRegistry.make() };
27 +});
28 +
29 +vi.mock("../connection/catalog", async () => {
30 + const { Atom } = await import("effect/reactivity");
31 + return { environmentCatalog: { catalogValueAtom: Atom.make({ isReady: false }) } };
32 +});
33 +
34 +vi.mock("../state/presentation", async () => {
35 + const { Atom } = await import("effect/reactivity");
36 + return {
37 + environmentPresentations: { presentationsAtom: Atom.make(new Map<string, unknown>()) },
38 + };
39 +});
40 +
41 +vi.mock("../state/server", () => ({
42 + serverEnvironment: { refreshProviders: { label: "Refresh providers" } },
43 +}));
44 +
45 +vi.mock("@t3tools/client-runtime/state/runtime", () => ({
46 + runAtomCommand: (
47 + _registry: unknown,
48 + _command: unknown,
49 + input: { readonly environmentId: string },
50 + ) => {
51 + fixture.refreshed.push(input.environmentId);
52 + return Promise.resolve();
53 + },
54 +}));
55 +
56 +import { environmentCatalog } from "../connection/catalog";
57 +import { appAtomRegistry } from "../state/atom-registry";
58 +import { environmentPresentations } from "../state/presentation";
59 +import { installSubscriptionUsageRefresh } from "./useSubscriptionUsage";
60 +
61 +const registry = appAtomRegistry as AtomRegistry.AtomRegistry;
62 +const catalogAtom = environmentCatalog.catalogValueAtom as unknown as Atom.Writable<{
63 + isReady: boolean;
64 +}>;
65 +const presentationsAtom = environmentPresentations.presentationsAtom as unknown as Atom.Writable<
66 + Map<string, { connection: { phase: string } }>
67 +>;
68 +
69 +const connected = EnvironmentId.make("env-connected");
70 +const connecting = EnvironmentId.make("env-connecting");
71 +
72 +describe("installSubscriptionUsageRefresh", () => {
73 + it("refreshes connected environments once the catalog is ready and the app is active", () => {
74 + fixture.appState = "background";
75 + fixture.refreshed = [];
76 + registry.set(catalogAtom, { isReady: false });
77 + registry.set(
78 + presentationsAtom,
79 + new Map([
80 + [connected, { connection: { phase: "connected" } }],
81 + [connecting, { connection: { phase: "connecting" } }],
82 + ]),
83 + );
84 +
85 + const dispose = installSubscriptionUsageRefresh();
86 + registry.set(catalogAtom, { isReady: true });
87 + // Backgrounded: nothing refreshes yet.
88 + expect(fixture.refreshed).toEqual([]);
89 +
90 + fixture.appState = "active";
91 + for (const listener of fixture.appStateListeners) listener();
92 + expect(fixture.refreshed).toEqual([connected]);
93 +
94 + dispose();
95 + expect(fixture.appStateListeners.size).toBe(0);
96 + });
97 +});
apps/mobile/src/widgets/useSubscriptionUsage.ts
+39 -32
@@ -1,40 +1,47 @@
1 -import { useAtomValue } from "@effect/atom-react";
2 -import { useEffect, useMemo } from "react";
3 -import { AppState } from "react-native";
1 +import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
2 +import type { EnvironmentId } from "@t3tools/contracts";
3
4 +import { environmentCatalog } from "../connection/catalog";
5 +import { isAppActive, subscribeAppState } from "../lib/appState";
6 +import { appAtomRegistry } from "../state/atom-registry";
7 import { environmentPresentations } from "../state/presentation";
8 import { serverEnvironment } from "../state/server";
7 -import { useAtomCommand } from "../state/use-atom-command";
9 import { createWidgetRefresher, WIDGET_REFRESH_INTERVAL } from "./subscriptionUsageSnapshot";
10
10 -export function useSubscriptionUsage(enabled = true) {
11 - const presentations = useAtomValue(environmentPresentations.presentationsAtom);
12 - const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, {
13 - reportFailure: false,
14 - });
15 - const refresh = useMemo(
16 - () =>
17 - createWidgetRefresher(
18 - (environmentId: Parameters<typeof refreshProviders>[0]["environmentId"]) =>
19 - refreshProviders({ environmentId, input: {} }),
20 - ),
21 - [refreshProviders],
11 +/**
12 + * App-lifetime worker that keeps provider quotas fresh for the usage widget:
13 + * on resume, on every connection change, and on a fixed interval while active.
14 + * Each environment is refreshed at most once per interval.
15 + */
16 +export function installSubscriptionUsageRefresh(): () => void {
17 + const refresh = createWidgetRefresher((environmentId: EnvironmentId) =>
18 + runAtomCommand(
19 + appAtomRegistry,
20 + serverEnvironment.refreshProviders,
21 + { environmentId, input: {} },
22 + { label: serverEnvironment.refreshProviders.label, reportFailure: false },
23 + ),
24 );
25 + const update = () => {
26 + if (!isAppActive() || !appAtomRegistry.get(environmentCatalog.catalogValueAtom).isReady) return;
27 + const connected = [...appAtomRegistry.get(environmentPresentations.presentationsAtom)]
28 + .filter(([, presentation]) => presentation.connection.phase === "connected")
29 + .map(([id]) => id);
30 + void refresh(connected, Date.now());
31 + };
32
24 - useEffect(() => {
25 - const update = () => {
26 - if (!enabled || AppState.currentState !== "active") return;
27 - const connected = [...presentations]
28 - .filter(([, presentation]) => presentation.connection.phase === "connected")
29 - .map(([id]) => id);
30 - void refresh(connected, Date.now());
31 - };
32 - update();
33 - const subscription = AppState.addEventListener("change", update);
34 - const timer = setInterval(update, WIDGET_REFRESH_INTERVAL);
35 - return () => {
36 - subscription.remove();
37 - clearInterval(timer);
38 - };
39 - }, [enabled, presentations, refresh]);
33 + const unsubscribeCatalog = appAtomRegistry.subscribe(environmentCatalog.catalogValueAtom, update);
34 + const unsubscribePresentations = appAtomRegistry.subscribe(
35 + environmentPresentations.presentationsAtom,
36 + update,
37 + { immediate: true },
38 + );
39 + const unsubscribeAppState = subscribeAppState(update);
40 + const timer = setInterval(update, WIDGET_REFRESH_INTERVAL);
41 + return () => {
42 + unsubscribeCatalog();
43 + unsubscribePresentations();
44 + unsubscribeAppState();
45 + clearInterval(timer);
46 + };
47 }