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 23, 01:45 UTC
perf(mobile): streaming re-renders less on the open thread screen (#20963)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 19, 2026 at 10:23 UTC
6bfcfa8e4dfd74fa22b408938dec60bf92322072
3 files changed
+83
-51
apps/mobile/src/features/threads/ThreadDetailScreen.tsx
+74
-49
@@ -2,7 +2,11 @@ import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime
2
import { AsyncResult } from "effect/reactivity";
3
import * as Dialog from "../../components/ui/Dialog";
4
import { useAtomValue } from "@effect/atom-react";
5
-import { useChildThreadInputs, useThreadReportedModelSelection } from "../../state/entities";
5
+import {
6
+ useChildThreadInputs,
7
+ useThreadReportedModelSelection,
8
+ useThreadShell,
9
+} from "../../state/entities";
10
import { UsageLimitRecoveryCard } from "./UsageLimitRecoveryCard";
11
import { useNavigation } from "@react-navigation/native";
12
import type { WorktreeSetupCardProps } from "./worktree-setup-card";
@@ -319,6 +323,70 @@ const USER_INPUT_TOGGLE_TIMING = {
323
easing: Easing.out(Easing.cubic),
324
};
325
326
+// The visit watermark follows the shell's updatedAt, which ticks while a run
327
+// streams. Keeping it in a leaf lets the screen read the quiet shell.
328
+function ThreadVisitWatermark(props: {
329
+ readonly environmentId: EnvironmentId;
330
+ readonly selectedThread: EnvironmentThreadShell;
331
+ readonly showContent: boolean;
332
+}) {
333
+ const liveThread = useThreadShell({
334
+ environmentId: props.environmentId,
335
+ threadId: props.selectedThread.id,
336
+ });
337
+ const thread = liveThread ?? props.selectedThread;
338
+ const showContent = props.showContent;
339
+ const selectedThreadKey = scopedThreadKey(props.environmentId, thread.id);
340
+ const visitThread = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
341
+ const lastDispatchedVisitRef = useRef<string | null>(null);
342
+ const lastVisitDispatchRef = useRef({ threadKey: selectedThreadKey, at: 0 });
343
+ const selectedThreadId = thread.id;
344
+ const selectedThreadUpdatedAt = thread.updatedAt;
345
+ const selectedThreadLastVisitedAt = thread.lastVisitedAt;
346
+ const selectedThreadCompletedAt = thread.latestRun?.completedAt;
347
+ useEffect(() => {
348
+ if (!showContent || selectedThreadLastVisitedAt === undefined) return;
349
+ const threadUpdatedAtMs = Date.parse(selectedThreadUpdatedAt);
350
+ if (Number.isNaN(threadUpdatedAtMs)) return;
351
+ const lastVisitedAtMs = selectedThreadLastVisitedAt
352
+ ? Date.parse(selectedThreadLastVisitedAt)
353
+ : NaN;
354
+ if (!Number.isNaN(lastVisitedAtMs) && lastVisitedAtMs >= threadUpdatedAtMs) return;
355
+ const dispatchKey = `${selectedThreadKey}:${selectedThreadUpdatedAt}`;
356
+ if (lastDispatchedVisitRef.current === dispatchKey) return;
357
+ const dispatch = () => {
358
+ lastDispatchedVisitRef.current = dispatchKey;
359
+ lastVisitDispatchRef.current = { threadKey: selectedThreadKey, at: Date.now() };
360
+ void visitThread({
361
+ environmentId: props.environmentId,
362
+ input: { threadId: selectedThreadId, visitedAt: selectedThreadUpdatedAt },
363
+ });
364
+ };
365
+ const completedAtMs = selectedThreadCompletedAt ? Date.parse(selectedThreadCompletedAt) : NaN;
366
+ const hasUnseenCompletion =
367
+ !Number.isNaN(completedAtMs) &&
368
+ (Number.isNaN(lastVisitedAtMs) || completedAtMs > lastVisitedAtMs);
369
+ const previous = lastVisitDispatchRef.current;
370
+ const elapsed = Date.now() - previous.at;
371
+ if (previous.threadKey !== selectedThreadKey || hasUnseenCompletion || elapsed >= 10_000) {
372
+ dispatch();
373
+ return;
374
+ }
375
+ const timer = setTimeout(dispatch, 10_000 - elapsed);
376
+ return () => clearTimeout(timer);
377
+ }, [
378
+ props.environmentId,
379
+ selectedThreadId,
380
+ selectedThreadKey,
381
+ selectedThreadLastVisitedAt,
382
+ selectedThreadCompletedAt,
383
+ selectedThreadUpdatedAt,
384
+ showContent,
385
+ visitThread,
386
+ ]);
387
+ return null;
388
+}
389
+
390
export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetailScreenProps) {
391
const usesNativeWorkspaceColumns = useNativeWorkspaceColumnsSupported();
392
const navigation = useNavigation();
@@ -785,54 +853,6 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
853
setComposerFocused(false);
854
}, [selectedThreadKey, showContent]);
855
788
- const visitThread = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
789
- const lastDispatchedVisitRef = useRef<string | null>(null);
790
- const lastVisitDispatchRef = useRef({ threadKey: selectedThreadKey, at: 0 });
791
- const selectedThreadId = props.selectedThread.id;
792
- const selectedThreadUpdatedAt = props.selectedThread.updatedAt;
793
- const selectedThreadLastVisitedAt = props.selectedThread.lastVisitedAt;
794
- const selectedThreadCompletedAt = props.selectedThread.latestRun?.completedAt;
795
- useEffect(() => {
796
- if (!showContent || selectedThreadLastVisitedAt === undefined) return;
797
- const threadUpdatedAtMs = Date.parse(selectedThreadUpdatedAt);
798
- if (Number.isNaN(threadUpdatedAtMs)) return;
799
- const lastVisitedAtMs = selectedThreadLastVisitedAt
800
- ? Date.parse(selectedThreadLastVisitedAt)
801
- : NaN;
802
- if (!Number.isNaN(lastVisitedAtMs) && lastVisitedAtMs >= threadUpdatedAtMs) return;
803
- const dispatchKey = `${selectedThreadKey}:${selectedThreadUpdatedAt}`;
804
- if (lastDispatchedVisitRef.current === dispatchKey) return;
805
- const dispatch = () => {
806
- lastDispatchedVisitRef.current = dispatchKey;
807
- lastVisitDispatchRef.current = { threadKey: selectedThreadKey, at: Date.now() };
808
- void visitThread({
809
- environmentId: props.environmentId,
810
- input: { threadId: selectedThreadId, visitedAt: selectedThreadUpdatedAt },
811
- });
812
- };
813
- const completedAtMs = selectedThreadCompletedAt ? Date.parse(selectedThreadCompletedAt) : NaN;
814
- const hasUnseenCompletion =
815
- !Number.isNaN(completedAtMs) &&
816
- (Number.isNaN(lastVisitedAtMs) || completedAtMs > lastVisitedAtMs);
817
- const previous = lastVisitDispatchRef.current;
818
- const elapsed = Date.now() - previous.at;
819
- if (previous.threadKey !== selectedThreadKey || hasUnseenCompletion || elapsed >= 10_000) {
820
- dispatch();
821
- return;
822
- }
823
- const timer = setTimeout(dispatch, 10_000 - elapsed);
824
- return () => clearTimeout(timer);
825
- }, [
826
- props.environmentId,
827
- selectedThreadId,
828
- selectedThreadKey,
829
- selectedThreadLastVisitedAt,
830
- selectedThreadCompletedAt,
831
- selectedThreadUpdatedAt,
832
- showContent,
833
- visitThread,
834
- ]);
835
-
856
useEffect(() => {
857
setAnchorMessageId(null);
858
setSubmittedMessageId(null);
@@ -1003,6 +1023,11 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
1023
return (
1024
<View className="flex-1">
1025
{!usesNativeWorkspaceColumns ? <NativeLayoutObserver onChange={setScreenMetrics} /> : null}
1026
+ <ThreadVisitWatermark
1027
+ environmentId={props.environmentId}
1028
+ selectedThread={props.selectedThread}
1029
+ showContent={showContent}
1030
+ />
1031
{showContent ? (
1032
<View
1033
style={{ flex: 1 }}
apps/mobile/src/state/entities.ts
+7
@@ -99,6 +99,13 @@ export function useThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadSh
99
);
100
}
101
102
+/** Streaming timestamps, item counts, and the latest message can be stale during a run. */
103
+export function useQuietThreadShell(ref: ScopedThreadRef | null): EnvironmentThreadShell | null {
104
+ return useAtomValue(
105
+ ref === null ? EMPTY_THREAD_SHELL_ATOM : environmentThreadShells.quietThreadShellAtom(ref),
106
+ );
107
+}
108
+
109
export function useChildThreadInputs(ref: ScopedThreadRef) {
110
return useAtomValue(environmentThreadShells.childThreadInputsAtom(ref));
111
}
apps/mobile/src/state/use-thread-selection.ts
+2
-2
@@ -20,7 +20,7 @@ import {
20
import * as Option from "effect/Option";
21
22
import { scopedThreadKey } from "../lib/scopedEntities";
23
-import { useProject, useThreadShell } from "../state/entities";
23
+import { useProject, useQuietThreadShell } from "../state/entities";
24
import { useEnvironmentThread } from "../state/threads";
25
import {
26
resolvePendingThreadCreation,
@@ -139,7 +139,7 @@ function useResolvedThreadSelection(params: ThreadSelectionRouteParams | undefin
139
threadId: ThreadId.make(routeThreadId),
140
}
141
: lastRouteThreadRef;
142
- const selectedThreadShell = useThreadShell(selectedThreadRef);
142
+ const selectedThreadShell = useQuietThreadShell(selectedThreadRef);
143
const selectedThreadKey =
144
selectedThreadRef === null
145
? null