|
| 1 | +// #3621: a memo that disposed its own root during its recompute came back |
| 2 | +// alive. `disposeChildren` set REACTIVE_DISPOSED on it reentrantly, and |
| 3 | +// `recompute`'s `finally` then rebuilt `_flags` from the ZOMBIE bit alone, |
| 4 | +// dropping it. `isDisposed()` read false, `refresh()` re-ran the memo, and the |
| 5 | +// reads that followed the `dispose()` call inside the pass had re-linked the |
| 6 | +// dead node to its sources — it kept re-running in a torn-down tree. |
| 7 | +// |
| 8 | +// Sibling of #3601 / #3606 (cleanups disposing the root mid-run). The ruling |
| 9 | +// here is the disposal path's: a node that dies during its own pass is dead |
| 10 | +// at the end of it, and the pass is void — no value commits or propagates |
| 11 | +// (#3024: a dead node freezes at its last committed value), no dependencies |
| 12 | +// survive it. |
| 13 | + |
| 14 | +import { |
| 15 | + $REFRESH, |
| 16 | + createEffect, |
| 17 | + createMemo, |
| 18 | + createOwner, |
| 19 | + createRoot, |
| 20 | + createSignal, |
| 21 | + flush, |
| 22 | + isDisposed, |
| 23 | + refresh, |
| 24 | + runWithOwner |
| 25 | +} from "../src/index.js"; |
| 26 | + |
| 27 | +const node = (accessor: () => unknown) => (accessor as any)[$REFRESH]; |
| 28 | + |
| 29 | +afterEach(() => flush()); |
| 30 | + |
| 31 | +describe("#3621 a memo disposing its own root mid-recompute stays disposed", () => { |
| 32 | + it("issue repro: isDisposed is true and refresh() does not re-run it", () => { |
| 33 | + const [n, setN] = createSignal(0); |
| 34 | + let memo!: () => number; |
| 35 | + let runs = 0; |
| 36 | + |
| 37 | + createRoot(dispose => { |
| 38 | + memo = createMemo(() => { |
| 39 | + runs++; |
| 40 | + if (n() === 1) dispose(); |
| 41 | + return n(); |
| 42 | + }); |
| 43 | + createEffect(memo, () => {}); |
| 44 | + }); |
| 45 | + |
| 46 | + flush(); |
| 47 | + expect(runs).toBe(1); |
| 48 | + setN(1); |
| 49 | + flush(); |
| 50 | + expect(runs).toBe(2); |
| 51 | + |
| 52 | + expect(isDisposed(node(memo))).toBe(true); |
| 53 | + refresh(memo); |
| 54 | + flush(); |
| 55 | + expect(runs).toBe(2); |
| 56 | + }); |
| 57 | + |
| 58 | + it("the reads after dispose() inside the pass leave no subscription behind", () => { |
| 59 | + const [n, setN] = createSignal(0); |
| 60 | + let memo!: () => number; |
| 61 | + let runs = 0; |
| 62 | + |
| 63 | + createRoot(dispose => { |
| 64 | + memo = createMemo(() => { |
| 65 | + runs++; |
| 66 | + if (n() === 1) dispose(); |
| 67 | + // Read AFTER the dispose: this link was created on a dead node. |
| 68 | + return n(); |
| 69 | + }); |
| 70 | + createEffect(memo, () => {}); |
| 71 | + }); |
| 72 | + |
| 73 | + flush(); |
| 74 | + setN(1); |
| 75 | + flush(); |
| 76 | + expect(runs).toBe(2); |
| 77 | + |
| 78 | + // No subscriber on the source, no dependency on the memo. |
| 79 | + expect(node(n)._subs).toBeNull(); |
| 80 | + expect(node(memo)._deps).toBeNull(); |
| 81 | + |
| 82 | + // And a write to the source does not reach it. |
| 83 | + setN(2); |
| 84 | + flush(); |
| 85 | + expect(runs).toBe(2); |
| 86 | + }); |
| 87 | + |
| 88 | + it("the value produced by the dying pass is not committed: the memo freezes at its last committed value (#3024)", () => { |
| 89 | + const [n, setN] = createSignal(0); |
| 90 | + let memo!: () => number; |
| 91 | + |
| 92 | + createRoot(dispose => { |
| 93 | + memo = createMemo(() => { |
| 94 | + if (n() === 1) dispose(); |
| 95 | + return n(); |
| 96 | + }); |
| 97 | + createEffect(memo, () => {}); |
| 98 | + }); |
| 99 | + |
| 100 | + flush(); |
| 101 | + expect(memo()).toBe(0); |
| 102 | + setN(1); |
| 103 | + flush(); |
| 104 | + expect(memo()).toBe(0); |
| 105 | + }); |
| 106 | + |
| 107 | + it("an effect downstream of the memo (outside the disposed root) does not fire from the mid-compute value", () => { |
| 108 | + const [n, setN] = createSignal(0); |
| 109 | + let memo!: () => number; |
| 110 | + const seen: number[] = []; |
| 111 | + |
| 112 | + createRoot(dispose => { |
| 113 | + memo = createMemo(() => { |
| 114 | + if (n() === 1) dispose(); |
| 115 | + return n(); |
| 116 | + }); |
| 117 | + }); |
| 118 | + createRoot(() => { |
| 119 | + createEffect(memo, v => { |
| 120 | + seen.push(v); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + flush(); |
| 125 | + expect(seen).toEqual([0]); |
| 126 | + setN(1); |
| 127 | + flush(); |
| 128 | + expect(seen).toEqual([0]); |
| 129 | + setN(2); |
| 130 | + flush(); |
| 131 | + expect(seen).toEqual([0]); |
| 132 | + }); |
| 133 | + |
| 134 | + it("effect twin: an effect whose compute phase disposes its root does not run its effect phase", () => { |
| 135 | + const [n, setN] = createSignal(0); |
| 136 | + const seen: number[] = []; |
| 137 | + let computes = 0; |
| 138 | + |
| 139 | + createRoot(dispose => { |
| 140 | + createEffect( |
| 141 | + () => { |
| 142 | + computes++; |
| 143 | + if (n() === 1) dispose(); |
| 144 | + return n(); |
| 145 | + }, |
| 146 | + v => { |
| 147 | + seen.push(v); |
| 148 | + } |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + flush(); |
| 153 | + expect(seen).toEqual([0]); |
| 154 | + expect(computes).toBe(1); |
| 155 | + setN(1); |
| 156 | + flush(); |
| 157 | + expect(computes).toBe(2); |
| 158 | + expect(seen).toEqual([0]); |
| 159 | + // Dead: no subscription survived the pass. |
| 160 | + expect(node(n)._subs).toBeNull(); |
| 161 | + setN(2); |
| 162 | + flush(); |
| 163 | + expect(computes).toBe(2); |
| 164 | + expect(seen).toEqual([0]); |
| 165 | + }); |
| 166 | + |
| 167 | + it("a memo disposing a parent owner (not its root) mid-compute stays disposed; the root survives", () => { |
| 168 | + const [n, setN] = createSignal(0); |
| 169 | + let memo!: () => number; |
| 170 | + let sibling!: () => number; |
| 171 | + let runs = 0; |
| 172 | + let siblingRuns = 0; |
| 173 | + |
| 174 | + createRoot(() => { |
| 175 | + const owner = createOwner(); |
| 176 | + runWithOwner(owner, () => { |
| 177 | + memo = createMemo(() => { |
| 178 | + runs++; |
| 179 | + if (n() === 1) owner.dispose(); |
| 180 | + return n(); |
| 181 | + }); |
| 182 | + createEffect(memo, () => {}); |
| 183 | + }); |
| 184 | + // Owned by the root directly: must keep running after the inner owner dies. |
| 185 | + sibling = createMemo(() => { |
| 186 | + siblingRuns++; |
| 187 | + return n() * 10; |
| 188 | + }); |
| 189 | + createEffect(sibling, () => {}); |
| 190 | + }); |
| 191 | + |
| 192 | + flush(); |
| 193 | + expect(runs).toBe(1); |
| 194 | + expect(siblingRuns).toBe(1); |
| 195 | + setN(1); |
| 196 | + flush(); |
| 197 | + expect(runs).toBe(2); |
| 198 | + expect(siblingRuns).toBe(2); |
| 199 | + |
| 200 | + expect(isDisposed(node(memo))).toBe(true); |
| 201 | + expect(isDisposed(node(sibling))).toBe(false); |
| 202 | + expect(node(memo)._deps).toBeNull(); |
| 203 | + refresh(memo); |
| 204 | + flush(); |
| 205 | + expect(runs).toBe(2); |
| 206 | + |
| 207 | + setN(2); |
| 208 | + flush(); |
| 209 | + expect(runs).toBe(2); |
| 210 | + expect(siblingRuns).toBe(3); |
| 211 | + expect(sibling()).toBe(20); |
| 212 | + }); |
| 213 | + |
| 214 | + it("control: disposing after the flush (non-reentrant) is unchanged", () => { |
| 215 | + const [n, setN] = createSignal(0); |
| 216 | + let memo!: () => number; |
| 217 | + let runs = 0; |
| 218 | + let dispose!: () => void; |
| 219 | + |
| 220 | + createRoot(d => { |
| 221 | + dispose = d; |
| 222 | + memo = createMemo(() => { |
| 223 | + runs++; |
| 224 | + return n(); |
| 225 | + }); |
| 226 | + createEffect(memo, () => {}); |
| 227 | + }); |
| 228 | + |
| 229 | + flush(); |
| 230 | + setN(1); |
| 231 | + flush(); |
| 232 | + expect(runs).toBe(2); |
| 233 | + expect(memo()).toBe(1); |
| 234 | + |
| 235 | + dispose(); |
| 236 | + expect(isDisposed(node(memo))).toBe(true); |
| 237 | + expect(node(n)._subs).toBeNull(); |
| 238 | + refresh(memo); |
| 239 | + flush(); |
| 240 | + expect(runs).toBe(2); |
| 241 | + setN(2); |
| 242 | + flush(); |
| 243 | + expect(runs).toBe(2); |
| 244 | + // Frozen at the last committed value (#3024). |
| 245 | + expect(memo()).toBe(1); |
| 246 | + }); |
| 247 | +}); |
0 commit comments