Open History to see the last commit
1import { Atom, AtomRegistry } from "effect/reactivity";
2import { describe, expect, it } from "vite-plus/test";
3
4import { revisionedQuery } from "./revisionedQuery";
5
6// A stand-in query that counts its fetches.
7function setup() {
8 let fetches = 0;
9 // Node removal runs on the registry's scheduler; releasing flushes it so the
10 // next mount of the same revision builds a fresh node, as a React remount does.
11 const scheduled: Array<() => void> = [];
12 const registry = AtomRegistry.make({
13 scheduleTask: (task) => {
14 scheduled.push(task);
15 return () => {};
16 },
17 });
18 const query = Atom.make(() => {
19 fetches += 1;
20 return fetches;
21 }).pipe(Atom.keepAlive);
22 const mount = async (revision: string | null | undefined) => {
23 const release = registry.mount(revisionedQuery(query, revision));
24 // A stale read refetches in a microtask, after the render that read it.
25 await Promise.resolve();
26 return () => {
27 release();
28 for (const task of scheduled.splice(0)) task();
29 };
30 };
31 return { registry, query, mount, fetches: () => fetches };
32}
33
34describe("revisionedQuery", () => {
35 it("refetches once per new revision, not on the first read or a remount", async () => {
36 const { mount, fetches } = setup();
37
38 (await mount("mutation-1"))();
39 expect(fetches()).toBe(1);
40
41 (await mount("mutation-1"))();
42 expect(fetches()).toBe(1);
43
44 (await mount("mutation-2"))();
45 expect(fetches()).toBe(2);
46 });
47
48 it("does not ping-pong while readers of the old and new revision overlap", async () => {
49 const { mount, fetches } = setup();
50
51 const releaseOld = await mount("mutation-1");
52 const releaseNew = await mount("mutation-2");
53 expect(fetches()).toBe(2);
54 releaseOld();
55 releaseNew();
56 });
57
58 it("refetches when an earlier revision is read again", async () => {
59 const { mount, fetches } = setup();
60
61 (await mount("mutation-1"))();
62 (await mount("mutation-2"))();
63 (await mount("mutation-1"))();
64 expect(fetches()).toBe(3);
65 });
66
67 it("refetches a query another reader already cached on its first revisioned read", async () => {
68 const { mount, fetches } = setup();
69
70 (await mount(undefined))();
71 expect(fetches()).toBe(1);
72
73 (await mount("mutation-1"))();
74 expect(fetches()).toBe(2);
75
76 (await mount("mutation-1"))();
77 expect(fetches()).toBe(2);
78 });
79
80 it("does not refetch a cached query on a first read before any workspace change", async () => {
81 const { mount, fetches } = setup();
82
83 (await mount(undefined))();
84 (await mount(null))();
85 expect(fetches()).toBe(1);
86 });
87
88 it("leaves the query alone when read without a revision", async () => {
89 const { mount, query, fetches } = setup();
90
91 (await mount("mutation-1"))();
92 expect(revisionedQuery(query, undefined)).toBe(query);
93 (await mount(undefined))();
94 (await mount("mutation-1"))();
95 expect(fetches()).toBe(1);
96 });
97
98 it("refetches the query when the reader is refreshed", async () => {
99 const { registry, query, mount, fetches } = setup();
100
101 const release = await mount("mutation-1");
102 registry.refresh(revisionedQuery(query, "mutation-1"));
103 expect(fetches()).toBe(2);
104 release();
105 });
106
107 it("drops a pending refetch when the registry is disposed before it runs", async () => {
108 const { registry, query, mount } = setup();
109
110 (await mount("mutation-1"))();
111 const release = registry.mount(revisionedQuery(query, "mutation-2"));
112 release();
113 registry.dispose();
114
115 // A refetch that ran now would throw "registry is disposed" as an uncaught error.
116 await Promise.resolve();
117 });
118});