Open History to see the last commit
1import * as Option from "effect/Option";
2import { Atom, type AtomRegistry } from "effect/reactivity";
3
4// Outside the nodes: a node is dropped once unmounted, and the next read must
5// still compare against the revision the query was last read under.
6const readRevisions = new WeakMap<AtomRegistry.AtomRegistry, WeakMap<object, string | null>>();
7
8// The registry keys serializable atoms' nodes by their key, not the atom.
9const queryKey = (query: Atom.Atom<unknown>) =>
10 Atom.isSerializable(query) ? query[Atom.SerializableTypeId].key : query;
11
12const makeRevisionedFamily = (query: Atom.Atom<unknown>) =>
13 Atom.family((revision: string | null) =>
14 Atom.readable(
15 (get) => {
16 // Only a node's first build compares: a node still mounted under the old
17 // revision rebuilds when the query refetches, and must not refetch it again.
18 if (Option.isNone(get.self())) {
19 let revisions = readRevisions.get(get.registry);
20 if (revisions === undefined) {
21 revisions = new WeakMap();
22 readRevisions.set(get.registry, revisions);
23 }
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 // Deferred: this builds while React renders, and a refresh would synchronously
30 // re-render the query's other readers, which React rejects.
31 if (stale) {
32 const registry = get.registry;
33 queueMicrotask(() => {
34 // No node, as after the registry is disposed: nothing cached to refetch.
35 if (registry.getNodes().has(queryKey(query))) registry.refresh(query);
36 });
37 }
38 revisions.set(query, revision);
39 }
40 return get(query);
41 },
42 // Refreshing the reader refetches the query it reads.
43 (refresh) => refresh(query),
44 ),
45 );
46
47// Held as long as the query lives, so one (query, revision) pair keeps one atom; an
48// `Atom.family` would hold the inner family only weakly and could swap the atom under a reader.
49const revisionedFamilies = new WeakMap<
50 Atom.Atom<unknown>,
51 ReturnType<typeof makeRevisionedFamily>
52>();
53const revisionedFamily = (query: Atom.Atom<unknown>) => {
54 let family = revisionedFamilies.get(query);
55 if (family === undefined) {
56 family = makeRevisionedFamily(query);
57 revisionedFamilies.set(query, family);
58 }
59 return family;
60};
61
62/**
63 * Reads `query` under a workspace revision (latest mutation id); a different
64 * revision than the last read refetches it. The first read records the revision,
65 * and refetches only a query that a reader without a revision already cached.
66 * `undefined` reads without taking part, for readers that must not refetch now.
67 */
68export function revisionedQuery<A>(
69 query: Atom.Atom<A>,
70 revision: string | null | undefined,
71): Atom.Atom<A> {
72 if (revision === undefined) return query;
73 return revisionedFamily(query)(revision) as Atom.Atom<A>;
74}