|
| 1 | +// #3802: a render effect born held under an action, re-run by a write to a |
| 2 | +// committed source, ran its callback with no committed value. |
| 3 | + |
| 4 | +import { afterEach, expect, it, vi } from "vitest"; |
| 5 | +import { |
| 6 | + action, |
| 7 | + createMemo, |
| 8 | + createOptimistic, |
| 9 | + createRenderEffect, |
| 10 | + createRoot, |
| 11 | + createSignal, |
| 12 | + flush |
| 13 | +} from "../src/index.js"; |
| 14 | + |
| 15 | +afterEach(() => vi.restoreAllMocks()); |
| 16 | + |
| 17 | +async function settle() { |
| 18 | + for (let i = 0; i < 8; i++) await Promise.resolve(); |
| 19 | + flush(); |
| 20 | +} |
| 21 | + |
| 22 | +it("a re-staging pass of a born-held effect does not run it before the commit", async () => { |
| 23 | + const error = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 24 | + const [value, setValue] = createSignal(false); |
| 25 | + const [mounted, setMounted] = createSignal(false); |
| 26 | + const [title, setTitle] = createSignal(""); |
| 27 | + let resolve!: () => void; |
| 28 | + const gate = new Promise<void>(r => (resolve = r)); |
| 29 | + const runs: [boolean, string][] = []; |
| 30 | + |
| 31 | + const save = action(function* () { |
| 32 | + setValue(true); |
| 33 | + yield gate; |
| 34 | + }); |
| 35 | + |
| 36 | + createRoot(() => { |
| 37 | + const child = createMemo(() => { |
| 38 | + if (!mounted()) return; |
| 39 | + createMemo(() => value()); |
| 40 | + createRenderEffect( |
| 41 | + () => ({ v: value(), t: title() }), |
| 42 | + state => { |
| 43 | + runs.push([state.v, state.t]); |
| 44 | + } |
| 45 | + ); |
| 46 | + }); |
| 47 | + createRenderEffect(child, () => {}); |
| 48 | + }); |
| 49 | + flush(); |
| 50 | + |
| 51 | + const pending = save(); |
| 52 | + await settle(); |
| 53 | + setMounted(true); |
| 54 | + await settle(); |
| 55 | + setTitle("Updated"); |
| 56 | + await settle(); |
| 57 | + |
| 58 | + expect(error).not.toHaveBeenCalled(); |
| 59 | + expect(runs).toEqual([]); |
| 60 | + |
| 61 | + resolve(); |
| 62 | + await pending; |
| 63 | + await settle(); |
| 64 | + |
| 65 | + expect(error).not.toHaveBeenCalled(); |
| 66 | + expect(runs).toEqual([[true, "Updated"]]); |
| 67 | +}); |
| 68 | + |
| 69 | +// The same effect re-derived by an optimistic write's lane rather than a |
| 70 | +// mainline write: the lane's run had nothing committed to apply either. |
| 71 | +it("a lane pass of a born-held effect does not run it with no committed value", async () => { |
| 72 | + const error = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 73 | + const [value, setValue] = createSignal(false); |
| 74 | + const [mounted, setMounted] = createSignal(false); |
| 75 | + const [guess, setGuess] = createOptimistic(0); |
| 76 | + let resolveSave!: () => void; |
| 77 | + let resolveVote!: () => void; |
| 78 | + const runs: [boolean, number][] = []; |
| 79 | + |
| 80 | + const save = action(function* () { |
| 81 | + setValue(true); |
| 82 | + yield new Promise<void>(r => (resolveSave = r)); |
| 83 | + }); |
| 84 | + const vote = action(function* () { |
| 85 | + setGuess(5); |
| 86 | + yield new Promise<void>(r => (resolveVote = r)); |
| 87 | + }); |
| 88 | + |
| 89 | + createRoot(() => { |
| 90 | + const child = createMemo(() => { |
| 91 | + if (!mounted()) return; |
| 92 | + createMemo(() => value()); |
| 93 | + createRenderEffect( |
| 94 | + () => ({ v: value(), g: guess() }), |
| 95 | + state => { |
| 96 | + runs.push([state.v, state.g]); |
| 97 | + } |
| 98 | + ); |
| 99 | + }); |
| 100 | + createRenderEffect(child, () => {}); |
| 101 | + }); |
| 102 | + flush(); |
| 103 | + |
| 104 | + const saving = save(); |
| 105 | + await settle(); |
| 106 | + setMounted(true); |
| 107 | + await settle(); |
| 108 | + const voting = vote(); |
| 109 | + await settle(); |
| 110 | + |
| 111 | + expect(error).not.toHaveBeenCalled(); |
| 112 | + |
| 113 | + resolveVote(); |
| 114 | + await voting; |
| 115 | + await settle(); |
| 116 | + resolveSave(); |
| 117 | + await saving; |
| 118 | + await settle(); |
| 119 | + |
| 120 | + expect(error).not.toHaveBeenCalled(); |
| 121 | + expect(runs.at(-1)).toEqual([true, 0]); |
| 122 | +}); |
0 commit comments