|
| 1 | +// #4349: an action's transaction lands beneath its own `refresh()`: nothing |
| 2 | +// on screen observed the re-ask's flight yet, so the landing commits the node |
| 3 | +// in flight (CONFIG_INPUTS_PUBLISHED, #3305). A render effect reading |
| 4 | +// `isPending(likes)` and `likes()` then probes the flight before anything |
| 5 | +// joined (a seam watcher, `watchVerdict`) and observes it with the plain |
| 6 | +// read: it goes pending and opens a frame of its own, which parks on it. The |
| 7 | +// lane seam re-derived the watcher "on the committed world" — and the re-run |
| 8 | +// re-entered the same transaction and parked it again, every round, until |
| 9 | +// the dev guard: "Potential Infinite Loop Detected … last staged node: |
| 10 | +// effect". A reader the parked transaction holds that read none of its |
| 11 | +// stagings has nothing to re-derive: it runs at the landing. |
| 12 | +// |
| 13 | +// Frames (F-9; optimistic-lanes R12): a bare refresh is a quiet re-ask — |
| 14 | +// `v41` stays until `v42` lands; a declared reload (`affects`) shows |
| 15 | +// `pending`, then `v42`. |
| 16 | +import { describe, expect, it } from "vitest"; |
| 17 | +import { |
| 18 | + action, |
| 19 | + affects, |
| 20 | + createMemo, |
| 21 | + createRenderEffect, |
| 22 | + createRoot, |
| 23 | + createSignal, |
| 24 | + flush, |
| 25 | + isPending, |
| 26 | + refresh |
| 27 | +} from "../src/index.js"; |
| 28 | + |
| 29 | +const tick = async () => { |
| 30 | + for (let i = 0; i < 8; i++) await Promise.resolve(); |
| 31 | + flush(); |
| 32 | +}; |
| 33 | + |
| 34 | +type Shape = { |
| 35 | + affects?: boolean; |
| 36 | + /** "refresh-yield": refresh, then a pending step; "yield-refresh": the |
| 37 | + * Like button's save, then the refresh; "refresh": refresh only. */ |
| 38 | + order: "refresh" | "refresh-yield" | "yield-refresh"; |
| 39 | + /** The JSX shape: the ternary's condition is its own memo. */ |
| 40 | + conditionMemo?: boolean; |
| 41 | + outside?: boolean; |
| 42 | +}; |
| 43 | + |
| 44 | +async function likeButton(shape: Shape) { |
| 45 | + const errors: unknown[] = []; |
| 46 | + const onError = (e: unknown) => errors.push(e); |
| 47 | + process.on("unhandledRejection", onError); |
| 48 | + process.on("uncaughtException", onError); |
| 49 | + let server = 41; |
| 50 | + const fetches: (() => void)[] = []; |
| 51 | + let saved: (() => void) | undefined; |
| 52 | + const frames: string[] = []; |
| 53 | + let likes!: () => number; |
| 54 | + const dispose = createRoot(dispose => { |
| 55 | + likes = createMemo(async () => { |
| 56 | + const v = server; |
| 57 | + await new Promise<void>(r => fetches.push(r)); |
| 58 | + return v; |
| 59 | + }); |
| 60 | + const pending = shape.conditionMemo |
| 61 | + ? createMemo(() => isPending(likes)) |
| 62 | + : () => isPending(likes); |
| 63 | + createRenderEffect( |
| 64 | + () => (pending() ? "pending" : `v${likes()}`), |
| 65 | + v => { |
| 66 | + if (frames.at(-1) !== v) frames.push(v); |
| 67 | + } |
| 68 | + ); |
| 69 | + return dispose; |
| 70 | + }); |
| 71 | + const steps: string[] = []; |
| 72 | + const step = async () => { |
| 73 | + await tick(); |
| 74 | + steps.push(frames.at(-1)!); |
| 75 | + }; |
| 76 | + try { |
| 77 | + flush(); |
| 78 | + fetches.shift()!(); |
| 79 | + await step(); |
| 80 | + const reask = () => { |
| 81 | + server++; |
| 82 | + refresh(likes); |
| 83 | + }; |
| 84 | + if (shape.outside) { |
| 85 | + if (shape.affects) affects(likes); |
| 86 | + reask(); |
| 87 | + } else |
| 88 | + action(function* () { |
| 89 | + if (shape.affects) affects(likes); |
| 90 | + if (shape.order === "yield-refresh") { |
| 91 | + yield new Promise<void>(r => (saved = r)); |
| 92 | + reask(); |
| 93 | + } else { |
| 94 | + reask(); |
| 95 | + if (shape.order === "refresh-yield") yield new Promise<void>(r => (saved = r)); |
| 96 | + } |
| 97 | + })(); |
| 98 | + await step(); |
| 99 | + if (saved) { |
| 100 | + saved(); |
| 101 | + await step(); |
| 102 | + } |
| 103 | + expect(fetches.length).toBe(1); |
| 104 | + fetches.shift()!(); |
| 105 | + await step(); |
| 106 | + return { frames, steps, errors }; |
| 107 | + } finally { |
| 108 | + dispose(); |
| 109 | + process.off("unhandledRejection", onError); |
| 110 | + process.off("uncaughtException", onError); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +describe("refresh(memo) inside an action with an isPending reader settles (#4349)", () => { |
| 115 | + for (const conditionMemo of [false, true]) { |
| 116 | + const via = conditionMemo ? " (condition memo)" : ""; |
| 117 | + it(`the issue's shape: a bare refresh is quiet${via}`, async () => { |
| 118 | + const r = await likeButton({ order: "refresh", conditionMemo }); |
| 119 | + expect(r.errors).toEqual([]); |
| 120 | + expect(r.steps).toEqual(["v41", "v41", "v42"]); |
| 121 | + expect(r.frames).toEqual(["v41", "v42"]); |
| 122 | + }); |
| 123 | + |
| 124 | + it(`a bare refresh, then a yield${via}`, async () => { |
| 125 | + const r = await likeButton({ order: "refresh-yield", conditionMemo }); |
| 126 | + expect(r.errors).toEqual([]); |
| 127 | + expect(r.steps).toEqual(["v41", "v41", "v41", "v42"]); |
| 128 | + expect(r.frames).toEqual(["v41", "v42"]); |
| 129 | + }); |
| 130 | + |
| 131 | + it(`the Like button: affects, yield, refresh — pending, then v42${via}`, async () => { |
| 132 | + const r = await likeButton({ affects: true, order: "yield-refresh", conditionMemo }); |
| 133 | + expect(r.errors).toEqual([]); |
| 134 | + expect(r.steps).toEqual(["v41", "pending", "pending", "v42"]); |
| 135 | + expect(r.frames).toEqual(["v41", "pending", "v42"]); |
| 136 | + }); |
| 137 | + |
| 138 | + it(`affects, refresh, yield — pending, then v42${via}`, async () => { |
| 139 | + const r = await likeButton({ affects: true, order: "refresh-yield", conditionMemo }); |
| 140 | + expect(r.errors).toEqual([]); |
| 141 | + expect(r.steps).toEqual(["v41", "pending", "pending", "v42"]); |
| 142 | + expect(r.frames).toEqual(["v41", "pending", "v42"]); |
| 143 | + }); |
| 144 | + |
| 145 | + it(`affects and refresh with no step settles${via}`, async () => { |
| 146 | + // The mark is the transaction's, released at its landing in the same |
| 147 | + // flush (affects.ts); whether that flush's `pending` frame shows is the |
| 148 | + // mark's lifetime, not this loop. |
| 149 | + const r = await likeButton({ affects: true, order: "refresh", conditionMemo }); |
| 150 | + expect(r.errors).toEqual([]); |
| 151 | + expect(r.steps.at(-1)).toBe("v42"); |
| 152 | + expect(r.frames.at(-1)).toBe("v42"); |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + it("the same refresh outside an action (control)", async () => { |
| 157 | + const r = await likeButton({ order: "refresh", outside: true }); |
| 158 | + expect(r.errors).toEqual([]); |
| 159 | + expect(r.frames).toEqual(["v41", "v42"]); |
| 160 | + }); |
| 161 | + |
| 162 | + it("`[isPending(x), x()]` after an action's write holds like `x()` alone (D-23), once", async () => { |
| 163 | + // The same loop without a refresh: the action's write lands beneath its |
| 164 | + // memo's new question, and the render effect that probes and reads it |
| 165 | + // parked the frame it opened every round. |
| 166 | + const fetches: (() => void)[] = []; |
| 167 | + const frames: string[] = []; |
| 168 | + let set!: (v: number) => void; |
| 169 | + const dispose = createRoot(dispose => { |
| 170 | + const [s, setS] = createSignal(41); |
| 171 | + set = setS; |
| 172 | + const x = createMemo(async () => { |
| 173 | + const v = s(); |
| 174 | + await new Promise<void>(r => fetches.push(r)); |
| 175 | + return v; |
| 176 | + }); |
| 177 | + createRenderEffect( |
| 178 | + () => `${isPending(x)}:${x()}`, |
| 179 | + v => { |
| 180 | + frames.push(v); |
| 181 | + } |
| 182 | + ); |
| 183 | + return dispose; |
| 184 | + }); |
| 185 | + try { |
| 186 | + flush(); |
| 187 | + fetches.shift()!(); |
| 188 | + await tick(); |
| 189 | + expect(frames).toEqual(["false:41"]); |
| 190 | + action(function* () { |
| 191 | + set(42); |
| 192 | + })(); |
| 193 | + expect(() => flush()).not.toThrow(); |
| 194 | + await tick(); |
| 195 | + expect(frames).toEqual(["false:41"]); |
| 196 | + fetches.shift()!(); |
| 197 | + await tick(); |
| 198 | + expect(frames).toEqual(["false:41", "false:42"]); |
| 199 | + } finally { |
| 200 | + dispose(); |
| 201 | + } |
| 202 | + }); |
| 203 | +}); |
0 commit comments