|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + action, |
| 4 | + createMemo, |
| 5 | + createProjection, |
| 6 | + createRenderEffect, |
| 7 | + createRoot, |
| 8 | + createSignal, |
| 9 | + deep, |
| 10 | + flush, |
| 11 | + snapshot |
| 12 | +} from "../src/index.js"; |
| 13 | +import { Queue, globalQueue } from "../src/core/scheduler.js"; |
| 14 | + |
| 15 | +it("keeps writes and effects held when a boundary check re-enters an action", async () => { |
| 16 | + const gate = Promise.withResolvers<void>(); |
| 17 | + const rendered: number[] = []; |
| 18 | + const [value, setValue] = createSignal(0); |
| 19 | + const [, setTick] = createSignal(0); |
| 20 | + const dispose = createRoot(dispose => { |
| 21 | + createRenderEffect(value, v => { |
| 22 | + rendered.push(v); |
| 23 | + }); |
| 24 | + return dispose; |
| 25 | + }); |
| 26 | + flush(); |
| 27 | + |
| 28 | + const start = action(function* () { |
| 29 | + setValue(1); |
| 30 | + yield gate.promise; |
| 31 | + }); |
| 32 | + // Park the action with value=1 staged but uncommitted. |
| 33 | + const done = start(); |
| 34 | + flush(); |
| 35 | + |
| 36 | + let checked = false; |
| 37 | + const boundary = Object.assign(new Queue(), { |
| 38 | + _checkSources() { |
| 39 | + if (checked) return; |
| 40 | + checked = true; |
| 41 | + // Writing a signal owned by the parked action re-enters its transaction. |
| 42 | + setValue(2); |
| 43 | + } |
| 44 | + }); |
| 45 | + globalQueue.addChild(boundary); |
| 46 | + try { |
| 47 | + // Unrelated work starts an ambient flush that checks boundary sources. |
| 48 | + setTick(1); |
| 49 | + flush(); |
| 50 | + expect.soft(value()).toBe(0); |
| 51 | + expect.soft(rendered).toEqual([0]); |
| 52 | + } finally { |
| 53 | + globalQueue.removeChild(boundary); |
| 54 | + gate.resolve(); |
| 55 | + await done; |
| 56 | + flush(); |
| 57 | + dispose(); |
| 58 | + flush(); |
| 59 | + } |
| 60 | + expect(rendered).toEqual([0, 2]); |
| 61 | +}); |
| 62 | + |
| 63 | +it("releases a deep projection reader after store-commit re-entry during a refetch", async () => { |
| 64 | + const settingsResponse = Promise.withResolvers<{ pins: string[] }>(); |
| 65 | + const configResponse = Promise.withResolvers<{ ready: boolean }>(); |
| 66 | + const [refreshRequested, setRefreshRequested] = createSignal(false); |
| 67 | + const [, setTick] = createSignal(0); |
| 68 | + const rendered: string[][] = []; |
| 69 | + |
| 70 | + const dispose = createRoot(dispose => { |
| 71 | + const settings = createProjection<{ pins: string[] }>( |
| 72 | + () => (refreshRequested() ? settingsResponse.promise : { pins: [] }), |
| 73 | + { pins: [] } |
| 74 | + ); |
| 75 | + const config = createProjection( |
| 76 | + () => (refreshRequested() ? configResponse.promise : { ready: false }), |
| 77 | + { ready: false } |
| 78 | + ); |
| 79 | + const pins = createMemo(() => snapshot(deep(settings.pins))); |
| 80 | + createRenderEffect(pins, value => { |
| 81 | + rendered.push([...value]); |
| 82 | + }); |
| 83 | + createRenderEffect( |
| 84 | + () => config.ready, |
| 85 | + () => {} |
| 86 | + ); |
| 87 | + return dispose; |
| 88 | + }); |
| 89 | + flush(); |
| 90 | + |
| 91 | + try { |
| 92 | + // Both refetches join one transition. |
| 93 | + setRefreshRequested(true); |
| 94 | + flush(); |
| 95 | + |
| 96 | + // Settings settles first; the config response still holds the render. |
| 97 | + settingsResponse.resolve({ pins: ["new pin"] }); |
| 98 | + await Promise.resolve(); |
| 99 | + flush(); |
| 100 | + expect.soft(rendered).toEqual([[]]); |
| 101 | + |
| 102 | + // An unrelated flush commits the settings store backing. Its deep() |
| 103 | + // notification re-enters the held transaction from commitPendingNodes(). |
| 104 | + setTick(1); |
| 105 | + flush(); |
| 106 | + expect.soft(rendered).toEqual([[]]); |
| 107 | + |
| 108 | + // Completing the last refetch must release the parked render. |
| 109 | + configResponse.resolve({ ready: true }); |
| 110 | + await Promise.resolve(); |
| 111 | + flush(); |
| 112 | + expect(rendered).toEqual([[], ["new pin"]]); |
| 113 | + } finally { |
| 114 | + settingsResponse.resolve({ pins: ["new pin"] }); |
| 115 | + configResponse.resolve({ ready: true }); |
| 116 | + await Promise.resolve(); |
| 117 | + flush(); |
| 118 | + dispose(); |
| 119 | + flush(); |
| 120 | + } |
| 121 | +}); |
| 122 | + |
| 123 | +// Effect ownership: a run applies with the commit of the transaction that |
| 124 | +// computed its value. The flush that entered a transaction mid-finalize still |
| 125 | +// applies everything it computed mainline — otherwise the write that caused the |
| 126 | +// flush reads committed while its own render stays stale until the entered |
| 127 | +// transaction settles. |
| 128 | +it("applies mainline-computed effects in the flush that entered a transaction", async () => { |
| 129 | + const settingsResponse = Promise.withResolvers<{ pins: string[] }>(); |
| 130 | + const configResponse = Promise.withResolvers<{ ready: boolean }>(); |
| 131 | + const [refreshRequested, setRefreshRequested] = createSignal(false); |
| 132 | + const [tick, setTick] = createSignal(0); |
| 133 | + const rendered: string[][] = []; |
| 134 | + const ticks: number[] = []; |
| 135 | + |
| 136 | + const dispose = createRoot(dispose => { |
| 137 | + const settings = createProjection<{ pins: string[] }>( |
| 138 | + () => (refreshRequested() ? settingsResponse.promise : { pins: [] }), |
| 139 | + { pins: [] } |
| 140 | + ); |
| 141 | + const config = createProjection( |
| 142 | + () => (refreshRequested() ? configResponse.promise : { ready: false }), |
| 143 | + { ready: false } |
| 144 | + ); |
| 145 | + const pins = createMemo(() => snapshot(deep(settings.pins))); |
| 146 | + createRenderEffect(pins, value => { |
| 147 | + rendered.push([...value]); |
| 148 | + }); |
| 149 | + createRenderEffect( |
| 150 | + () => config.ready, |
| 151 | + () => {} |
| 152 | + ); |
| 153 | + createRenderEffect(tick, t => { |
| 154 | + ticks.push(t); |
| 155 | + }); |
| 156 | + return dispose; |
| 157 | + }); |
| 158 | + flush(); |
| 159 | + |
| 160 | + try { |
| 161 | + setRefreshRequested(true); |
| 162 | + flush(); |
| 163 | + settingsResponse.resolve({ pins: ["new pin"] }); |
| 164 | + await Promise.resolve(); |
| 165 | + flush(); |
| 166 | + expect.soft(rendered).toEqual([[]]); |
| 167 | + |
| 168 | + // The tick flush commits the settings backing; its deep() notification |
| 169 | + // enters the held transaction from commitPendingNodes(). |
| 170 | + setTick(1); |
| 171 | + flush(); |
| 172 | + expect.soft(tick()).toBe(1); |
| 173 | + // Computed mainline before finalize → applied by this flush (no read/DOM split). |
| 174 | + expect.soft(ticks).toEqual([0, 1]); |
| 175 | + // Computed under the entered transaction → parked with it. |
| 176 | + expect.soft(rendered).toEqual([[]]); |
| 177 | + |
| 178 | + configResponse.resolve({ ready: true }); |
| 179 | + await Promise.resolve(); |
| 180 | + flush(); |
| 181 | + expect(rendered).toEqual([[], ["new pin"]]); |
| 182 | + expect(ticks).toEqual([0, 1]); |
| 183 | + } finally { |
| 184 | + settingsResponse.resolve({ pins: ["new pin"] }); |
| 185 | + configResponse.resolve({ ready: true }); |
| 186 | + await Promise.resolve(); |
| 187 | + flush(); |
| 188 | + dispose(); |
| 189 | + flush(); |
| 190 | + } |
| 191 | +}); |
| 192 | + |
| 193 | +// A write staged during finalize BEFORE the entry is adopted by the entered |
| 194 | +// transaction (held). Finalize's heap runs after its hooks, so the dependent |
| 195 | +// effect recomputes after the entry, owner-stamped, and parks with it: state |
| 196 | +// and DOM stay consistent and release together. |
| 197 | +it("holds a pre-entry hook write and its effect together with the entered transaction", async () => { |
| 198 | + const gate = Promise.withResolvers<void>(); |
| 199 | + const [value, setValue] = createSignal(0); |
| 200 | + const [other, setOther] = createSignal(0); |
| 201 | + const [tick, setTick] = createSignal(0); |
| 202 | + const renderedValue: number[] = []; |
| 203 | + const renderedOther: number[] = []; |
| 204 | + const renderedTick: number[] = []; |
| 205 | + const dispose = createRoot(dispose => { |
| 206 | + createRenderEffect(value, v => void renderedValue.push(v)); |
| 207 | + createRenderEffect(other, v => void renderedOther.push(v)); |
| 208 | + createRenderEffect(tick, v => void renderedTick.push(v)); |
| 209 | + return dispose; |
| 210 | + }); |
| 211 | + flush(); |
| 212 | + |
| 213 | + const start = action(function* () { |
| 214 | + setValue(1); |
| 215 | + yield gate.promise; |
| 216 | + }); |
| 217 | + const done = start(); |
| 218 | + flush(); |
| 219 | + |
| 220 | + let checked = false; |
| 221 | + const boundary = Object.assign(new Queue(), { |
| 222 | + _checkSources() { |
| 223 | + if (checked) return; |
| 224 | + checked = true; |
| 225 | + setOther(1); // ambient, staged before the entry |
| 226 | + setValue(2); // enters the parked action |
| 227 | + } |
| 228 | + }); |
| 229 | + globalQueue.addChild(boundary); |
| 230 | + try { |
| 231 | + setTick(1); |
| 232 | + flush(); |
| 233 | + expect.soft(tick()).toBe(1); |
| 234 | + expect.soft(renderedTick).toEqual([0, 1]); |
| 235 | + expect.soft(value()).toBe(0); |
| 236 | + expect.soft(renderedValue).toEqual([0]); |
| 237 | + expect.soft(other()).toBe(0); |
| 238 | + expect.soft(renderedOther).toEqual([0]); |
| 239 | + } finally { |
| 240 | + globalQueue.removeChild(boundary); |
| 241 | + gate.resolve(); |
| 242 | + await done; |
| 243 | + flush(); |
| 244 | + } |
| 245 | + expect(value()).toBe(2); |
| 246 | + expect(other()).toBe(1); |
| 247 | + expect(renderedValue).toEqual([0, 2]); |
| 248 | + expect(renderedOther).toEqual([0, 1]); |
| 249 | + dispose(); |
| 250 | + flush(); |
| 251 | +}); |
0 commit comments