|
| 1 | +import { afterEach, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + action, |
| 4 | + createEffect, |
| 5 | + createMemo, |
| 6 | + createRenderEffect, |
| 7 | + createRoot, |
| 8 | + createSignal, |
| 9 | + createTrackedEffect, |
| 10 | + flush, |
| 11 | + onSettled |
| 12 | +} from "../src/index.js"; |
| 13 | + |
| 14 | +afterEach(() => flush()); |
| 15 | + |
| 16 | +async function settle() { |
| 17 | + for (let i = 0; i < 6; i++) { |
| 18 | + flush(); |
| 19 | + await Promise.resolve(); |
| 20 | + } |
| 21 | + flush(); |
| 22 | +} |
| 23 | + |
| 24 | +// #4606: the run's own read pulls the memo, whose walk the recomputing run |
| 25 | +// refuses; the tracked effect re-runs once the memo's value commits (F-1). |
| 26 | +it("re-runs for a same-pass write it reads through a memo (#4606)", async () => { |
| 27 | + const log: number[] = []; |
| 28 | + createRoot(() => { |
| 29 | + const [s, setS] = createSignal(0); |
| 30 | + const m = createMemo(() => s() * 10); |
| 31 | + createEffect( |
| 32 | + () => 1, |
| 33 | + () => { |
| 34 | + setS(1); |
| 35 | + } |
| 36 | + ); |
| 37 | + createTrackedEffect(() => { |
| 38 | + log.push(m()); |
| 39 | + }); |
| 40 | + }); |
| 41 | + await settle(); |
| 42 | + expect(log).toEqual([0, 10]); |
| 43 | +}); |
| 44 | + |
| 45 | +it("re-runs for an onSettled write it reads through a memo (#4606)", async () => { |
| 46 | + const log: number[] = []; |
| 47 | + createRoot(() => { |
| 48 | + const [s, setS] = createSignal(100); |
| 49 | + const m = createMemo(() => s() + 1); |
| 50 | + onSettled(() => { |
| 51 | + setS(101); |
| 52 | + }); |
| 53 | + createTrackedEffect(() => { |
| 54 | + log.push(m()); |
| 55 | + }); |
| 56 | + }); |
| 57 | + await settle(); |
| 58 | + expect(log).toEqual([101, 102]); |
| 59 | +}); |
| 60 | + |
| 61 | +it("does not re-run for a memo whose same-pass writes net to the committed value", async () => { |
| 62 | + const log: number[] = []; |
| 63 | + createRoot(() => { |
| 64 | + const [s, setS] = createSignal(0); |
| 65 | + const m = createMemo(() => s() * 10); |
| 66 | + createEffect( |
| 67 | + () => 1, |
| 68 | + () => { |
| 69 | + setS(1); |
| 70 | + setS(0); |
| 71 | + } |
| 72 | + ); |
| 73 | + createTrackedEffect(() => { |
| 74 | + log.push(m()); |
| 75 | + }); |
| 76 | + }); |
| 77 | + await settle(); |
| 78 | + expect(log).toEqual([0]); |
| 79 | +}); |
| 80 | + |
| 81 | +// #4607: a run during an action's hold, for another dependency, is served |
| 82 | +// the committed value of the held one (D-29) and consumes the run the |
| 83 | +// landing replays; it re-runs at the landing as A15's stale reader. |
| 84 | +for (const mode of ["tracked", "effect"] as const) |
| 85 | + for (const touchU of [false, true]) |
| 86 | + it(`${mode}: last run sees an action's landing (mid-hold run: ${touchU}) (#4607)`, async () => { |
| 87 | + const log: string[] = []; |
| 88 | + let release!: (v?: unknown) => void; |
| 89 | + let s!: () => number, setS!: (v: number) => void, setU!: (v: number) => void; |
| 90 | + createRoot(() => { |
| 91 | + let u: () => number; |
| 92 | + [s, setS] = createSignal(0); |
| 93 | + [u, setU] = createSignal(0); |
| 94 | + if (mode === "tracked") |
| 95 | + createTrackedEffect(() => { |
| 96 | + log.push(`s${s()}u${u()}`); |
| 97 | + }); |
| 98 | + else |
| 99 | + createEffect( |
| 100 | + () => `s${s()}u${u()}`, |
| 101 | + v => { |
| 102 | + log.push(v); |
| 103 | + } |
| 104 | + ); |
| 105 | + }); |
| 106 | + await settle(); |
| 107 | + const p = new Promise(r => (release = r)); |
| 108 | + const done = action(function* () { |
| 109 | + setS(1); |
| 110 | + yield p; |
| 111 | + })(); |
| 112 | + await settle(); |
| 113 | + if (touchU) { |
| 114 | + setU(1); |
| 115 | + await settle(); |
| 116 | + // D-29: the held write is never visible to it. |
| 117 | + expect(log[log.length - 1]).toBe(mode === "tracked" ? "s0u1" : "s0u0"); |
| 118 | + } |
| 119 | + release(); |
| 120 | + await done; |
| 121 | + await settle(); |
| 122 | + expect(s()).toBe(1); |
| 123 | + expect(log).toEqual( |
| 124 | + touchU |
| 125 | + ? mode === "tracked" |
| 126 | + ? ["s0u0", "s0u1", "s1u1"] |
| 127 | + : ["s0u0", "s1u1"] |
| 128 | + : ["s0u0", "s1u0"] |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | +// D-29: re-run at the landing, but it holds nothing — a flight it reads, |
| 133 | +// once no render effect reads it, does not keep the hold open. |
| 134 | +it("a tracked effect that read a held value does not hold a flight (#4607)", async () => { |
| 135 | + const shown: string[] = []; |
| 136 | + const seen: number[] = []; |
| 137 | + let s!: () => number, setS!: (v: number) => void; |
| 138 | + let setU!: (v: number) => void, setShow!: (v: boolean) => void; |
| 139 | + createRoot(() => { |
| 140 | + let u: () => number, show: () => boolean; |
| 141 | + [s, setS] = createSignal(0); |
| 142 | + [u, setU] = createSignal(0); |
| 143 | + [show, setShow] = createSignal(true); |
| 144 | + const F = createMemo(async () => (s() === 0 ? 0 : new Promise<number>(() => {}))); |
| 145 | + createRenderEffect( |
| 146 | + () => (show() ? `s${s()} F${F()}` : `s${s()} hidden`), |
| 147 | + v => { |
| 148 | + shown.push(v); |
| 149 | + } |
| 150 | + ); |
| 151 | + createTrackedEffect(() => { |
| 152 | + u(); |
| 153 | + seen.push(s()); |
| 154 | + F(); |
| 155 | + }); |
| 156 | + }); |
| 157 | + await settle(); |
| 158 | + setS(1); |
| 159 | + await settle(); |
| 160 | + setU(1); |
| 161 | + await settle(); |
| 162 | + expect(seen).toEqual([0, 0]); |
| 163 | + setShow(false); |
| 164 | + await settle(); |
| 165 | + expect(shown).toEqual(["s0 F0", "s1 hidden"]); |
| 166 | + expect(seen).toEqual([0, 0, 1]); |
| 167 | +}); |
0 commit comments