|
| 1 | +/** |
| 2 | + * #3543: a deferred (zombie) child that recomputes while an unrelated action |
| 3 | + * is parked loses REACTIVE_ZOMBIE — recompute()/updateIfNecessary() rewrite |
| 4 | + * `_flags` wholesale. At the owner's commit `disposeChildren(child, true)` |
| 5 | + * then takes the ordinary parent-chain splice and, with no `_prevSibling`, |
| 6 | + * writes `parent._firstChild = next`, detaching the REPLACEMENT child. The |
| 7 | + * replacement stays subscribed but no owner cleanup can reach it again. |
| 8 | + * |
| 9 | + * Shape is the compiler's `<Show when={n() > 0 && n() < 2}>`: a condition |
| 10 | + * memo whose getter creates a nested memo on every run. |
| 11 | + */ |
| 12 | +import { describe, expect, it } from "vitest"; |
| 13 | +import { |
| 14 | + action, |
| 15 | + createMemo, |
| 16 | + createRenderEffect, |
| 17 | + createRoot, |
| 18 | + createSignal, |
| 19 | + flush |
| 20 | +} from "../src/index.js"; |
| 21 | + |
| 22 | +function setup() { |
| 23 | + const [n, setN] = createSignal(0); |
| 24 | + const [a, setA] = createSignal(0); |
| 25 | + let release!: () => void; |
| 26 | + const gate = new Promise<void>(r => (release = r)); |
| 27 | + const run = action(async function* () { |
| 28 | + setA(v => v + 1); |
| 29 | + yield gate; |
| 30 | + }); |
| 31 | + let disposeRoot!: () => void; |
| 32 | + // runs of the nested memo: every live copy subscribed to `n` runs per write |
| 33 | + let runs = 0; |
| 34 | + createRoot(d => { |
| 35 | + disposeRoot = d; |
| 36 | + const cond = createMemo(() => { |
| 37 | + const c = createMemo(() => (runs++, n() > 0)); |
| 38 | + return c() && n() < 2; |
| 39 | + }); |
| 40 | + createRenderEffect( |
| 41 | + () => a(), |
| 42 | + () => {} |
| 43 | + ); |
| 44 | + createRenderEffect( |
| 45 | + () => cond(), |
| 46 | + () => {} |
| 47 | + ); |
| 48 | + }); |
| 49 | + flush(); |
| 50 | + const toggle = (times: number) => { |
| 51 | + for (let i = 0; i < times; i++) { |
| 52 | + setN(i % 2 ? 0 : 1); |
| 53 | + flush(); |
| 54 | + } |
| 55 | + }; |
| 56 | + // nested-memo runs caused by one more write |
| 57 | + const runsPerWrite = () => { |
| 58 | + const before = runs; |
| 59 | + setN(v => (v ? 0 : 1)); |
| 60 | + flush(); |
| 61 | + return runs - before; |
| 62 | + }; |
| 63 | + return { toggle, runsPerWrite, run, release, disposeRoot }; |
| 64 | +} |
| 65 | + |
| 66 | +describe("#3543 zombie child recompute keeps its zombie flag", () => { |
| 67 | + it("control: no pending action keeps subscribers bounded", () => { |
| 68 | + const { toggle, runsPerWrite } = setup(); |
| 69 | + toggle(40); |
| 70 | + expect(runsPerWrite()).toBeLessThanOrEqual(2); |
| 71 | + }); |
| 72 | + |
| 73 | + it("pending unrelated action keeps subscribers bounded, and after it finishes", async () => { |
| 74 | + const { toggle, runsPerWrite, run, release, disposeRoot } = setup(); |
| 75 | + const done = run(); |
| 76 | + flush(); |
| 77 | + toggle(40); |
| 78 | + expect(runsPerWrite()).toBeLessThanOrEqual(2); |
| 79 | + release(); |
| 80 | + await done; |
| 81 | + flush(); |
| 82 | + expect(runsPerWrite()).toBeLessThanOrEqual(2); |
| 83 | + disposeRoot(); |
| 84 | + expect(runsPerWrite()).toBe(0); |
| 85 | + }); |
| 86 | +}); |
0 commit comments