Skip to content
Myr

fix: key inserted array indices by name in the array-methods plugin

Before adoption: this commit came in with the history the project adopted, so no landing records it.

`handleInsertedValues` recorded inserted indices in `assigned_` as numbers, but `assigned_` is keyed by property name everywhere else, because the proxy traps only ever see strings. Patch generation looks indices up with `assigned_?.get(i.toString())`, so a numeric key was never found. The added indices loop in `generateArrayPatches` covers everything past `base_.length` regardless of `assigned_`, which hid the mismatch for a plain push. It surfaced when an insert reuses an index that already existed in the base, e.g. dropping the oldest entry and appending a new one: const [next, patches] = produceWithPatches(["a", "b", "c"], draft => { draft.pop() draft.push("z") }) // next -> ["a", "b", "z"] // patches -> [] (expected: replace /2 with "z") The result was correct but no patch described it, so undo/redo and state sync built on the patch stream silently dropped the change. Stringifying the index also keeps the key consistent for the finalization lookup in `handleCrossReference`, which reads `assigned_` with the same key this function passes it.

authored by jaybhade · committed by jaybhade · parent a3be9df · Browse files · 2 files+23 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+23 added−1 removed

  • tests:+17 added−0 removed
  • other:+6 added−1 removed

2 files+23 added−1 removed

modified:__tests__/base.js+17 added
421 unmodified lines
422 const newLength = draft.items.push(3, 4)423 expect(newLength).toBe(4)424 })425 })426427 test("push onto an index freed by pop() is patched", () => {428 const base = {items: ["a", "b", "c"]}429 const [result, patches, inverse] = produceWithPatches(430 base,431 draft => {432 draft.items.pop()433 draft.items.push("z")434 }435 )436 expect(result.items).toEqual(["a", "b", "z"])437 expect(patches).toEqual([438 {op: "replace", path: ["items", 2], value: "z"}439 ])440 expect(applyPatches(base, patches)).toEqual(result)441 expect(applyPatches(result, inverse)).toEqual(base)442 })443 })444445 describe("unshift()", () => {446 test("unshift single item", () => {
3783 unmodified lines
modified:src/plugins/arrayMethods.ts+6 added-1 removed
200 unmodified lines
201 *202 * Without this, values containing draft proxies (like `{...state[0]}`)203 * pushed via the array methods plugin would have their nested drafts204 * revoked during finalization without being replaced by final values.205 *206 * The index is stringified because the proxy traps only ever see property207 * names, so `assigned_` is keyed by string everywhere else. A numeric key208 * would be invisible to the readers that look indices up by name, such as209 * patch generation.210 */211 function handleInsertedValues(212 state: ProxyArrayState,213 startIndex: number,214 values: any[]215 ) {216 for (let i = 0; i < values.length; i++) {212 const index = startIndex + i217 const index = "" + (startIndex + i)218 state.assigned_!.set(index, true)219 handleCrossReference(state, index, values[i])220 }221 }
225 unmodified lines