Skip to content

Commit 5c000e2

Browse files
fix(web): breadcrumb menus refetch a listing cached before the latest workspace change
The first revisioned read only recorded the mutation id, so a listing that useDirectoryEntries had cached before the latest workspace change stayed stale in the breadcrumb menu until the 30s stale time. The first read now refetches when the query already has a node in the registry and a mutation id exists. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c77de0a commit 5c000e2

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎apps/web/src/state/revisionedQuery.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ describe("revisionedQuery", () => {
6262
expect(fetches()).toBe(3);
6363
});
6464

65+
it("refetches a query another reader already cached on its first revisioned read", () => {
66+
const { mount, fetches } = setup();
67+
68+
mount(undefined)();
69+
expect(fetches()).toBe(1);
70+
71+
mount("mutation-1")();
72+
expect(fetches()).toBe(2);
73+
74+
mount("mutation-1")();
75+
expect(fetches()).toBe(2);
76+
});
77+
78+
it("does not refetch a cached query on a first read before any workspace change", () => {
79+
const { mount, fetches } = setup();
80+
81+
mount(undefined)();
82+
mount(null)();
83+
expect(fetches()).toBe(1);
84+
});
85+
6586
it("leaves the query alone when read without a revision", () => {
6687
const { mount, query, fetches } = setup();
6788

‎apps/web/src/state/revisionedQuery.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { Atom, type AtomRegistry } from "effect/reactivity";
55
// still compare against the revision the query was last read under.
66
const readRevisions = new WeakMap<AtomRegistry.AtomRegistry, WeakMap<object, string | null>>();
77

8+
// The registry keys serializable atoms' nodes by their key, not the atom.
9+
const queryKey = (query: Atom.Atom<unknown>) =>
10+
Atom.isSerializable(query) ? query[Atom.SerializableTypeId].key : query;
11+
812
const makeRevisionedFamily = (query: Atom.Atom<unknown>) =>
913
Atom.family((revision: string | null) =>
1014
Atom.readable(
@@ -17,7 +21,12 @@ const makeRevisionedFamily = (query: Atom.Atom<unknown>) =>
1721
revisions = new WeakMap();
1822
readRevisions.set(get.registry, revisions);
1923
}
20-
if (revisions.has(query) && revisions.get(query) !== revision) get.refresh(query);
24+
const stale = revisions.has(query)
25+
? revisions.get(query) !== revision
26+
: // A reader without a revision may have cached it before the latest
27+
// workspace change; a query nobody has read yet fetches fresh anyway.
28+
revision !== null && get.registry.getNodes().has(queryKey(query));
29+
if (stale) get.refresh(query);
2130
revisions.set(query, revision);
2231
}
2332
return get(query);
@@ -44,9 +53,9 @@ const revisionedFamily = (query: Atom.Atom<unknown>) => {
4453

4554
/**
4655
* Reads `query` under a workspace revision (latest mutation id); a different
47-
* revision than the last read refetches it. The first read only records the
48-
* revision. `undefined` reads without taking part, for readers that must not
49-
* refetch now.
56+
* revision than the last read refetches it. The first read records the revision,
57+
* and refetches only a query that a reader without a revision already cached.
58+
* `undefined` reads without taking part, for readers that must not refetch now.
5059
*/
5160
export function revisionedQuery<A>(
5261
query: Atom.Atom<A>,

0 commit comments

Comments
 (0)