|
| 1 | +/** |
| 2 | + * #3767 — a writable derived store whose derive returns one of its own draft |
| 3 | + * rows (`draft.filter(...)`) overflowed the stack once its source refreshed. |
| 4 | + * The row read through the draft is a draft WRAPPER (a proxy over a fake |
| 5 | + * target forwarding to the store proxy, projection.ts `wrapDraft`), not the |
| 6 | + * store's own proxy; reconcile took it for a foreign value and adopted it as |
| 7 | + * the row's backing, so the row's backing forwarded to the row itself. |
| 8 | + */ |
| 9 | +import { describe, expect, it } from "vitest"; |
| 10 | +import { |
| 11 | + createRenderEffect, |
| 12 | + createRoot, |
| 13 | + createSignal, |
| 14 | + createStore, |
| 15 | + flush |
| 16 | +} from "../../src/index.js"; |
| 17 | + |
| 18 | +type Row = { id: number; title: string; failed?: boolean }; |
| 19 | + |
| 20 | +describe("#3767 a derive retaining its own draft row", () => { |
| 21 | + it("keeps the row readable after the source refreshes", () => { |
| 22 | + const [source, setSource] = createSignal<Row[]>([{ id: 1, title: "server" }]); |
| 23 | + let rows!: Row[]; |
| 24 | + let setRows!: (fn: (draft: Row[]) => Row[] | void) => void; |
| 25 | + const dispose = createRoot(d => { |
| 26 | + [rows, setRows] = createStore<Row[]>( |
| 27 | + draft => [...source(), ...draft.filter(row => row.failed)], |
| 28 | + [] |
| 29 | + ); |
| 30 | + return d; |
| 31 | + }); |
| 32 | + flush(); |
| 33 | + expect(rows.map(r => r.id)).toEqual([1]); |
| 34 | + |
| 35 | + setRows(draft => { |
| 36 | + draft.push({ id: 2, title: "local", failed: true }); |
| 37 | + }); |
| 38 | + flush(); |
| 39 | + expect(rows.map(r => r.id)).toEqual([1, 2]); |
| 40 | + const local = rows[1]; |
| 41 | + |
| 42 | + setSource([{ id: 1, title: "server" }]); |
| 43 | + flush(); |
| 44 | + expect(rows.map(r => r.id)).toEqual([1, 2]); |
| 45 | + expect(Object.keys(rows[1])).toEqual(["id", "title", "failed"]); |
| 46 | + expect({ ...rows[1] }).toEqual({ id: 2, title: "local", failed: true }); |
| 47 | + expect(rows[1]).toBe(local); |
| 48 | + |
| 49 | + setSource([{ id: 1, title: "server 2" }]); |
| 50 | + flush(); |
| 51 | + expect(rows.map(r => r.title)).toEqual(["server 2", "local"]); |
| 52 | + expect(rows[1]).toBe(local); |
| 53 | + |
| 54 | + // The retained row is still the store's row: a write through the setter |
| 55 | + // lands, and the next derive keeps it. |
| 56 | + setRows(draft => { |
| 57 | + draft[1].title = "edited"; |
| 58 | + }); |
| 59 | + flush(); |
| 60 | + expect(rows[1].title).toBe("edited"); |
| 61 | + setSource([{ id: 1, title: "server 3" }]); |
| 62 | + flush(); |
| 63 | + expect(rows.map(r => r.title)).toEqual(["server 3", "edited"]); |
| 64 | + expect(rows[1]).toBe(local); |
| 65 | + dispose(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("re-renders a reader of the retained row", () => { |
| 69 | + const [source, setSource] = createSignal<Row[]>([{ id: 1, title: "server" }]); |
| 70 | + let rows!: Row[]; |
| 71 | + let setRows!: (fn: (draft: Row[]) => Row[] | void) => void; |
| 72 | + const seen: string[][] = []; |
| 73 | + const dispose = createRoot(d => { |
| 74 | + [rows, setRows] = createStore<Row[]>( |
| 75 | + draft => [...source(), ...draft.filter(row => row.failed)], |
| 76 | + [] |
| 77 | + ); |
| 78 | + createRenderEffect( |
| 79 | + () => rows.map(r => r.title), |
| 80 | + titles => { |
| 81 | + seen.push(titles); |
| 82 | + } |
| 83 | + ); |
| 84 | + return d; |
| 85 | + }); |
| 86 | + flush(); |
| 87 | + setRows(draft => { |
| 88 | + draft.push({ id: 2, title: "local", failed: true }); |
| 89 | + }); |
| 90 | + flush(); |
| 91 | + setSource([{ id: 1, title: "server" }]); |
| 92 | + flush(); |
| 93 | + setRows(draft => { |
| 94 | + draft[1].title = "edited"; |
| 95 | + }); |
| 96 | + flush(); |
| 97 | + expect(seen).toEqual([["server"], ["server", "local"], ["server", "edited"]]); |
| 98 | + dispose(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("retains a nested draft record of an object store", () => { |
| 102 | + type State = { server: string; local?: { note: string } }; |
| 103 | + const [source, setSource] = createSignal("a"); |
| 104 | + let state!: State; |
| 105 | + let setState!: (fn: (draft: State) => State | void) => void; |
| 106 | + const dispose = createRoot(d => { |
| 107 | + [state, setState] = createStore<State>(draft => ({ server: source(), local: draft.local }), { |
| 108 | + server: "" |
| 109 | + }); |
| 110 | + return d; |
| 111 | + }); |
| 112 | + flush(); |
| 113 | + setState(draft => { |
| 114 | + draft.local = { note: "kept" }; |
| 115 | + }); |
| 116 | + flush(); |
| 117 | + const local = state.local; |
| 118 | + setSource("b"); |
| 119 | + flush(); |
| 120 | + expect(state.server).toBe("b"); |
| 121 | + expect({ ...state.local }).toEqual({ note: "kept" }); |
| 122 | + expect(state.local).toBe(local); |
| 123 | + setSource("c"); |
| 124 | + flush(); |
| 125 | + expect({ ...state.local }).toEqual({ note: "kept" }); |
| 126 | + dispose(); |
| 127 | + }); |
| 128 | +}); |
0 commit comments