Skip to content
Myr

Merge pull request #1210 from immerjs/bugfix/1209-array-plugin-nested-drafts

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 570c800, 90a7765 · Browse files · 2 files+92 added−0 removed
This commit

State

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

Trailers

  • fix: handle nested proxies after spreading and inserting into an array

Diff stats

2 files+92 added−0 removed

  • tests:+47 added−0 removed
  • other:+45 added−0 removed

2 files+92 added−0 removed

modified:__tests__/base.js+47 added
2776 unmodified lines
2777 })2778 expect(nextState).toEqual({foo: {bar: {a: true, c: true}}})2779 })27802781 // #1209 - spread draft object and push back via array methods2782 it("can spread a draft and push it back into the array", () => {2783 const base = [{nestedArray: []}]2784 const next = produce(base, s => {2785 s.push({...s[0]})2786 })2787 expect(next[0].nestedArray).toEqual([])2788 expect(next[1].nestedArray).toEqual([])2789 expect(next[1].nestedArray.length).toBe(0)2790 })27912792 it("can spread a draft with nested objects and push it back", () => {2793 const base = [{nested: {value: 42}}]2794 const next = produce(base, s => {2795 s.push({...s[0]})2796 })2797 expect(next[0].nested.value).toBe(42)2798 expect(next[1].nested.value).toBe(42)2799 })28002801 it("can push a draft value directly into its parent array", () => {2802 const base = [{nestedArray: []}]2803 const next = produce(base, s => {2804 s.push(s[0])2805 })2806 expect(next[0].nestedArray).toEqual([])2807 expect(next[1].nestedArray).toEqual([])2808 })28092810 it("can unshift a spread draft back into the array", () => {2811 const base = [{nestedArray: [1]}]2812 const next = produce(base, s => {2813 s.unshift({...s[0]})2814 })2815 expect(next[0].nestedArray).toEqual([1])2816 expect(next[1].nestedArray).toEqual([1])2817 })28182819 it("can splice a spread draft into the array", () => {2820 const base = [{nestedArray: ["a", "b"]}]2821 const next = produce(base, s => {2822 s.splice(0, 0, {...s[0]})2823 })2824 expect(next[0].nestedArray).toEqual(["a", "b"])2825 expect(next[1].nestedArray).toEqual(["a", "b"])2826 })28272828 it("supports assigning undefined to an existing property", () => {2829 const nextState = produce(baseState, s => {2830 s.aProp = undefined2831 })
1349 unmodified lines
modified:src/plugins/arrayMethods.ts+45 added
2 unmodified lines
3 latest,4 loadPlugin,5 markChanged,6 prepareCopy,7 handleCrossReference,8 ProxyArrayState9} from "../internal"1011/**
178 unmodified lines
190 return Math.min(index, length)191 }192193 /**194 * Calls handleCrossReference for each value being inserted into the array,195 * and marks the corresponding indices as assigned in `assigned_`.196 *197 * This ensures nested drafts inside inserted values (e.g. from spreading198 * a draft object) are properly finalized, matching the behavior of the199 * proxy set trap which calls handleCrossReference on every assignment.200 *201 * Without this, values containing draft proxies (like `{...state[0]}`)202 * pushed via the array methods plugin would have their nested drafts203 * revoked during finalization without being replaced by final values.204 */205 function handleInsertedValues(206 state: ProxyArrayState,207 startIndex: number,208 values: any[]209 ) {210 for (let i = 0; i < values.length; i++) {211 const index = startIndex + i212 state.assigned_!.set(index, true)213 handleCrossReference(state, index, values[i])214 }215 }216217 /**218 * Handles mutating operations that add/remove elements (push, pop, shift, unshift, splice).219 *220 * Operates directly on `state.copy_` without creating per-element proxies.221 * For shifting methods (shift, unshift), marks all indices as reassigned since
6 unmodified lines
228 method: string,229 args: any[]230 ) {231 return executeArrayMethod(state, () => {232 // For push/unshift, capture the length before the operation233 // so we can compute insertion indices for handleCrossReference234 const lengthBefore = state.copy_!.length235236 const result = (state.copy_! as any)[method](...args)237238 // Handle index reassignment for shifting methods239 if (SHIFTING_METHODS.has(method as MutatingArrayMethod)) {240 markAllIndicesReassigned(state)241 }242243 // Handle cross-references for newly inserted values.244 // push appends at the end, unshift inserts at the beginning.245 if (method === "push" && args.length > 0) {246 handleInsertedValues(state, lengthBefore, args)247 } else if (method === "unshift" && args.length > 0) {248 handleInsertedValues(state, 0, args)249 }250251 // Return appropriate value based on method252 return RESULT_RETURNING_METHODS.has(method as MutatingArrayMethod)253 ? result254 : state.draft_
66 unmodified lines
321 const res = executeArrayMethod(state, () =>322 state.copy_!.splice(...(args as [number, number, ...any[]]))323 )324 markAllIndicesReassigned(state)325 // Handle cross-references for inserted values (args from index 2+)326 if (args.length > 2) {327 const startIndex = normalizeSliceIndex(328 args[0] ?? 0,329 state.copy_!.length330 )331 handleInsertedValues(state, startIndex, args.slice(2))332 }333 return res334 }335 } else {336 // Handle non-mutating methods
104 unmodified lines