Skip to content

Commit 95e2238

Browse files
github-actions[bot]juliusmarminge
authored andcommitted
fix(mobile): throttle streaming thread visit updates
1 parent 233063a commit 95e2238

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -674,14 +674,16 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
674674

675675
const visitThread = useAtomCommand(threadEnvironment.visit, { reportFailure: false });
676676
const lastDispatchedVisitRef = useRef<string | null>(null);
677+
const lastVisitDispatchRef = useRef({ threadKey: selectedThreadKey, at: 0 });
677678
const selectedThreadId = props.selectedThread.id;
678679
const selectedThreadUpdatedAt = props.selectedThread.updatedAt;
679680
const selectedThreadLastVisitedAt = props.selectedThread.lastVisitedAt;
681+
const selectedThreadCompletedAt = props.selectedThread.latestRun?.completedAt;
680682
useEffect(() => {
681683
// Records the server-side visited watermark while the thread is on
682684
// screen (mirror of web ChatView), so the "Done" marker clears on every
683685
// device. Field absent → the server predates visited tracking.
684-
if (selectedThreadLastVisitedAt === undefined) return;
686+
if (!showContent || selectedThreadLastVisitedAt === undefined) return;
685687
const threadUpdatedAtMs = Date.parse(selectedThreadUpdatedAt);
686688
if (Number.isNaN(threadUpdatedAtMs)) return;
687689
const lastVisitedAtMs = selectedThreadLastVisitedAt
@@ -691,17 +693,36 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
691693
// Dedupe per watermark — the effect re-runs before the command echo lands.
692694
const dispatchKey = `${selectedThreadKey}:${selectedThreadUpdatedAt}`;
693695
if (lastDispatchedVisitRef.current === dispatchKey) return;
694-
lastDispatchedVisitRef.current = dispatchKey;
695-
void visitThread({
696-
environmentId: props.environmentId,
697-
input: { threadId: selectedThreadId, visitedAt: selectedThreadUpdatedAt },
698-
});
696+
const dispatch = () => {
697+
lastDispatchedVisitRef.current = dispatchKey;
698+
lastVisitDispatchRef.current = { threadKey: selectedThreadKey, at: Date.now() };
699+
void visitThread({
700+
environmentId: props.environmentId,
701+
input: { threadId: selectedThreadId, visitedAt: selectedThreadUpdatedAt },
702+
});
703+
};
704+
// Completion clears unread state immediately; streaming watermarks use the
705+
// same ten-second trailing throttle as web, keeping the newest update.
706+
const completedAtMs = selectedThreadCompletedAt ? Date.parse(selectedThreadCompletedAt) : NaN;
707+
const hasUnseenCompletion =
708+
!Number.isNaN(completedAtMs) &&
709+
(Number.isNaN(lastVisitedAtMs) || completedAtMs > lastVisitedAtMs);
710+
const previous = lastVisitDispatchRef.current;
711+
const elapsed = Date.now() - previous.at;
712+
if (previous.threadKey !== selectedThreadKey || hasUnseenCompletion || elapsed >= 10_000) {
713+
dispatch();
714+
return;
715+
}
716+
const timer = setTimeout(dispatch, 10_000 - elapsed);
717+
return () => clearTimeout(timer);
699718
}, [
700719
props.environmentId,
701720
selectedThreadId,
702721
selectedThreadKey,
703722
selectedThreadLastVisitedAt,
723+
selectedThreadCompletedAt,
704724
selectedThreadUpdatedAt,
725+
showContent,
705726
visitThread,
706727
]);
707728

0 commit comments

Comments
 (0)