Skip to content
Myr

Merge pull request #1283 from Jaybhade/fix/array-methods-assigned-index-key

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

authored by Mark Erikson · committed by GitHub · parents a3be9df, e3df956 · 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.

Trailers

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

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