|
3 | 3 | action, |
4 | 4 | createMemo, |
5 | 5 | createOptimistic, |
| 6 | + createOptimisticStore, |
6 | 7 | createRenderEffect, |
7 | 8 | createRoot, |
8 | 9 | createSignal, |
@@ -42,3 +43,201 @@ for (const via of ["direct", "memo"] as const) |
42 | 43 | await tick(); |
43 | 44 | expect(done).toBe(true); |
44 | 45 | }); |
| 46 | + |
| 47 | +function setup() { |
| 48 | + let setSource!: (v: number) => void; |
| 49 | + let setX!: (v: number) => void; |
| 50 | + let api!: { |
| 51 | + count: () => number; |
| 52 | + doubled: () => number; |
| 53 | + sum: () => number; |
| 54 | + x: () => number; |
| 55 | + guess: (v: number) => void; |
| 56 | + }; |
| 57 | + createRoot(() => { |
| 58 | + const [source, ss] = createSignal(0); |
| 59 | + const [x, sx] = createSignal(0); |
| 60 | + setSource = ss; |
| 61 | + setX = sx; |
| 62 | + const [count, guess] = createOptimistic(() => source()); |
| 63 | + const doubled = createMemo(() => count() * 2); |
| 64 | + const sum = createMemo(() => count() + x()); |
| 65 | + createRenderEffect(count, () => {}); |
| 66 | + createRenderEffect(doubled, () => {}); |
| 67 | + createRenderEffect(sum, () => {}); |
| 68 | + api = { count, doubled, sum, x, guess }; |
| 69 | + }); |
| 70 | + flush(); |
| 71 | + return { setSource, setX, ...api }; |
| 72 | +} |
| 73 | + |
| 74 | +const watch = (p: Promise<unknown>) => { |
| 75 | + const s = { done: false }; |
| 76 | + p.then(() => (s.done = true)); |
| 77 | + return s; |
| 78 | +}; |
| 79 | + |
| 80 | +// Review of #3978, 1: a truth staged beneath an earlier guess does not |
| 81 | +// confirm the next guess — confirmed means the staged truth equals the guess. |
| 82 | +for (const via of ["direct", "memo"] as const) { |
| 83 | + it(`until() ${via}: a re-guess the source has not confirmed keeps waiting (two saves)`, async () => { |
| 84 | + const m = setup(); |
| 85 | + const save = action(function* (n: number) { |
| 86 | + m.guess(n); |
| 87 | + yield Promise.resolve(); |
| 88 | + yield until(() => (via === "memo" ? m.doubled() === n * 2 : m.count() === n)); |
| 89 | + }); |
| 90 | + const first = watch(save(1)); |
| 91 | + await tick(); |
| 92 | + m.setSource(1); |
| 93 | + flush(); |
| 94 | + await tick(); |
| 95 | + expect(first.done).toBe(true); |
| 96 | + |
| 97 | + const second = watch(save(2)); |
| 98 | + await tick(); |
| 99 | + expect(second.done).toBe(false); |
| 100 | + m.setSource(2); |
| 101 | + flush(); |
| 102 | + await tick(); |
| 103 | + expect(second.done).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it(`until() ${via}: one action guessing twice waits for the second confirmation`, async () => { |
| 107 | + const m = setup(); |
| 108 | + const save = action(function* () { |
| 109 | + m.guess(1); |
| 110 | + yield Promise.resolve(); |
| 111 | + yield until(() => m.count() === 1); |
| 112 | + m.guess(2); |
| 113 | + yield Promise.resolve(); |
| 114 | + yield until(() => (via === "memo" ? m.doubled() === 4 : m.count() === 2)); |
| 115 | + }); |
| 116 | + const s = watch(save()); |
| 117 | + await tick(); |
| 118 | + m.setSource(1); |
| 119 | + flush(); |
| 120 | + await tick(); |
| 121 | + expect(s.done).toBe(false); |
| 122 | + m.setSource(2); |
| 123 | + flush(); |
| 124 | + await tick(); |
| 125 | + expect(s.done).toBe(true); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +// Review of #3978, 2: a derivation that also reads a held plain write is not |
| 130 | +// the truth — its lane value (guess + the old `x`) never commits. |
| 131 | +it("until() does not resolve on a derivation mixing a confirmed guess with a held write", async () => { |
| 132 | + const m = setup(); |
| 133 | + const seen: string[] = []; |
| 134 | + const run = action(function* () { |
| 135 | + m.setX(10); |
| 136 | + m.guess(1); |
| 137 | + yield Promise.resolve(); |
| 138 | + yield until(() => m.x() === 10); |
| 139 | + seen.push("x"); |
| 140 | + yield until(() => m.sum() === 1); |
| 141 | + seen.push("sum=1"); |
| 142 | + }); |
| 143 | + run(); |
| 144 | + await tick(); |
| 145 | + m.setSource(1); |
| 146 | + flush(); |
| 147 | + await tick(); |
| 148 | + expect(seen).toEqual(["x"]); |
| 149 | +}); |
| 150 | + |
| 151 | +// Review of #3978, 3: confirmation is per dependency — an unconfirmed guess |
| 152 | +// elsewhere in the action does not block a derivation that does not read it. |
| 153 | +it("until() on a derivation of a confirmed guess ignores another, unconfirmed guess", async () => { |
| 154 | + let setA!: (v: number) => void; |
| 155 | + let save!: () => Promise<unknown>; |
| 156 | + createRoot(() => { |
| 157 | + const [a, sa] = createSignal(0); |
| 158 | + const [b] = createSignal(0); |
| 159 | + setA = sa; |
| 160 | + const [optA, guessA] = createOptimistic(() => a()); |
| 161 | + const [optB, guessB] = createOptimistic(() => b()); |
| 162 | + const doubledA = createMemo(() => optA() * 2); |
| 163 | + createRenderEffect(doubledA, () => {}); |
| 164 | + createRenderEffect(optB, () => {}); |
| 165 | + save = action(function* () { |
| 166 | + guessA(1); |
| 167 | + guessB(1); |
| 168 | + yield Promise.resolve(); |
| 169 | + yield until(() => doubledA() === 2); |
| 170 | + }); |
| 171 | + }); |
| 172 | + flush(); |
| 173 | + const s = watch(save()); |
| 174 | + await tick(); |
| 175 | + expect(s.done).toBe(false); |
| 176 | + setA(1); |
| 177 | + flush(); |
| 178 | + await tick(); |
| 179 | + expect(s.done).toBe(true); |
| 180 | +}); |
| 181 | + |
| 182 | +// The store form of review 1: a field edited twice in one action waits for |
| 183 | +// the second confirmation. |
| 184 | +it("until() on an optimistic store field edited twice waits for the second confirmation", async () => { |
| 185 | + let setSource!: (v: string) => void; |
| 186 | + let save!: () => Promise<unknown>; |
| 187 | + createRoot(() => { |
| 188 | + const [source, ss] = createSignal(""); |
| 189 | + setSource = ss; |
| 190 | + const [store, setStore] = createOptimisticStore(() => ({ text: source() }), { text: "" }); |
| 191 | + const upper = createMemo(() => store.text.toUpperCase()); |
| 192 | + createRenderEffect(upper, () => {}); |
| 193 | + save = action(function* () { |
| 194 | + setStore(s => void (s.text = "a")); |
| 195 | + yield Promise.resolve(); |
| 196 | + yield until(() => store.text === "a"); |
| 197 | + setStore(s => void (s.text = "b")); |
| 198 | + yield Promise.resolve(); |
| 199 | + yield until(() => upper() === "B"); |
| 200 | + }); |
| 201 | + }); |
| 202 | + flush(); |
| 203 | + const s = watch(save()); |
| 204 | + await tick(); |
| 205 | + setSource("a"); |
| 206 | + flush(); |
| 207 | + await tick(); |
| 208 | + expect(s.done).toBe(false); |
| 209 | + setSource("b"); |
| 210 | + flush(); |
| 211 | + await tick(); |
| 212 | + expect(s.done).toBe(true); |
| 213 | +}); |
| 214 | + |
| 215 | +// A store row the server sends back as a fresh object confirms by content |
| 216 | +// (the row echoes the guess, #3898), so a derivation over it is the truth too. |
| 217 | +it("until() on a derivation of an optimistic store row confirmed by a fresh, equal row", async () => { |
| 218 | + type Row = { id: string; text: string }; |
| 219 | + let setSource!: (v: Row[]) => void; |
| 220 | + let save!: () => Promise<unknown>; |
| 221 | + createRoot(() => { |
| 222 | + const [source, ss] = createSignal<Row[]>([]); |
| 223 | + setSource = ss; |
| 224 | + const [store, setStore] = createOptimisticStore(() => ({ items: source() }), { |
| 225 | + items: [] as Row[] |
| 226 | + }); |
| 227 | + const texts = createMemo(() => store.items.map(i => i.text).join(",")); |
| 228 | + createRenderEffect(texts, () => {}); |
| 229 | + save = action(function* () { |
| 230 | + setStore(s => void s.items.push({ id: "1", text: "a" })); |
| 231 | + yield Promise.resolve(); |
| 232 | + yield until(() => texts() === "a"); |
| 233 | + }); |
| 234 | + }); |
| 235 | + flush(); |
| 236 | + const s = watch(save()); |
| 237 | + await tick(); |
| 238 | + expect(s.done).toBe(false); |
| 239 | + setSource([{ id: "1", text: "a" }]); |
| 240 | + flush(); |
| 241 | + await tick(); |
| 242 | + expect(s.done).toBe(true); |
| 243 | +}); |
0 commit comments