Skip to content

Commit 8c54273

Browse files
fix(clients): reopening a workspace file shows its current contents
Reopening a file (or any environment query) within the 5 minute idle TTL reused the stale-while-revalidate node, so its stale check never ran and the old contents survived every reopen. The idle TTL now holds only the data and the refresh signal it was read under; the swr node and the polling wrapper are released with their last reader, so a reopen rebuilds swr and revalidates a stale answer. Polling stops when nobody reads. On Android the source view has no pull-to-refresh, so the file menu's Refresh action now also covers files whose contents come from the file query (source, Markdown and table previews). Fixes #29117 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c86c3fa commit 8c54273

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

‎apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,14 +869,17 @@ export function ThreadFileScreen(props: ThreadFileRouteScreenProps) {
869869
onPress: () => tryOpenExternalUrl(assetPreviewUri, "file-preview"),
870870
} as const)
871871
: null,
872-
resolvedActiveMode === "preview" &&
873-
(isBrowserFile || isImageFile || isVideoFile || isAudioFile)
872+
needsFileContents ||
873+
(resolvedActiveMode === "preview" &&
874+
(isBrowserFile || isImageFile || isVideoFile || isAudioFile))
874875
? ({
875876
id: "refresh",
876877
title: "Refresh",
877878
icon: "arrow.clockwise",
878879
inline: false,
879880
onPress: async () => {
881+
// Android's native source view has no pull-to-refresh, so this is its only refresh.
882+
if (needsFileContents) return fileQuery.refresh();
880883
if (isVideoFile || isAudioFile) await assetPreview.refresh();
881884
setPreviewRevision((current) => current + 1);
882885
},

‎packages/client-runtime/src/state/runtime.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ describe("environment query lifecycle", () => {
949949
expect(yield* read).toBe(2);
950950

951951
unmount();
952+
yield* Effect.yieldNow;
952953
registry.set(trigger, 2);
953954
registry.set(trigger, 3);
954955
expect(executions).toBe(2);

‎packages/client-runtime/src/state/runtime.ts‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,8 @@ function runInEnvironment<A, E, R>(
516516
const refreshOnSignalWhileRead =
517517
<V>(signal: Atom.Atom<unknown>, isFinal: (value: V) => boolean) =>
518518
<A extends V>(self: Atom.Atom<A>): Atom.Atom<A> => {
519-
// The signal value each registry's data was read under. Held outside the node because the
520-
// registry can sweep this node while `self` keeps the data, and the rebuilt node must still
521-
// see a change it missed.
519+
// The signal value each registry's data was read under. Held outside the node because a
520+
// rebuilt node must still see a change it missed.
522521
const readUnder = new WeakMap<AtomRegistry.AtomRegistry, unknown>();
523522
return Atom.transform(
524523
self,
@@ -665,7 +664,9 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
665664
const connectionAtom = environmentConnectionAtoms(runtime);
666665
const family = Atom.family((key: string) => {
667666
const target = parseEnvironmentRpcKey<Input>(key);
668-
const idleTtlMs = options.idleTtlMs ?? 5 * 60_000;
667+
const isFinal = (result: AsyncResult.AsyncResult<A, unknown>) =>
668+
options.isFinal !== undefined && Option.exists(AsyncResult.value(result), options.isFinal);
669+
const refreshTrigger = options.refreshTrigger?.(target);
669670
const queryAtom = runtime
670671
.atom<
671672
A,
@@ -779,24 +780,22 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
779780
});
780781
})
781782
.pipe(
782-
Atom.swr({
783-
staleTime: options.staleTimeMs ?? 30_000,
784-
revalidateOnMount: true,
785-
}),
786-
Atom.setIdleTTL(idleTtlMs),
783+
(self) =>
784+
refreshTrigger === undefined
785+
? self
786+
: self.pipe(refreshOnSignalWhileRead(refreshTrigger, isFinal)),
787+
// The idle TTL keeps the data (and the signal it was read under), but nothing above it:
788+
// `swr` checks staleness only when its node is built, and polling serves only readers.
789+
Atom.setIdleTTL(options.idleTtlMs ?? 5 * 60_000),
787790
);
788-
const isFinal = (result: AsyncResult.AsyncResult<A, unknown>) =>
789-
options.isFinal !== undefined && Option.exists(AsyncResult.value(result), options.isFinal);
790-
const intervalQuery =
791+
return (
791792
options.refreshIntervalMs === undefined
792793
? queryAtom
793-
: queryAtom.pipe(refreshEveryUntilFinal(options.refreshIntervalMs, isFinal));
794-
const refreshTrigger = options.refreshTrigger?.(target);
795-
return (
796-
refreshTrigger === undefined
797-
? intervalQuery
798-
: intervalQuery.pipe(refreshOnSignalWhileRead(refreshTrigger, isFinal))
799-
).pipe(Atom.setIdleTTL(idleTtlMs), Atom.withLabel(`${options.label}:${key}`));
794+
: queryAtom.pipe(refreshEveryUntilFinal(options.refreshIntervalMs, isFinal))
795+
).pipe(
796+
Atom.swr({ staleTime: options.staleTimeMs ?? 30_000, revalidateOnMount: true }),
797+
Atom.withLabel(`${options.label}:${key}`),
798+
);
800799
});
801800
return (target) => family(environmentRpcKey(target));
802801
}

0 commit comments

Comments
 (0)