Skip to content

Commit fee2c4f

Browse files
authored
fix: skip nested patches for children of shifted or reordered arrays (#1316)
With enableArrayMethods(), unshift/shift/splice/reverse/sort run natively on copy_ and mark all indices as reassigned. A child drafted afterwards gets its key from the index it now sits at, so getPath accepts it and emits a nested patch such as ["arr", 3, "a"]. That patch is generated before the parent array's own patches. When the index is past the base length (e.g. unshift and then writing to the last element) it points at a path that does not exist yet, and applyPatches throws "path doesn't resolve". The array already emits a replace/add patch for every index that changed, carrying the finalized child, so nested patches add nothing there. Return no path for children of an array whose indices were reassigned. This also matches the patches produced without the plugin, where such children are not patched individually either.
1 parent 484f5cf commit fee2c4f

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎__tests__/base.js‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,91 @@ function runBaseTest(
716716
})
717717
})
718718

719+
describe("patches for elements moved by shifting or reordering", () => {
720+
const createBase = () => ({
721+
arr: [
722+
{id: 1, a: 1, meta: {n: 1}},
723+
{id: 2, a: 2, meta: {n: 2}},
724+
{id: 3, a: 3, meta: {n: 3}}
725+
]
726+
})
727+
const added = {id: 9, a: 9, meta: {n: 9}}
728+
729+
// Only the first four failed before: they write to an element that was
730+
// shifted to an index at or past the base length. The rest are guards.
731+
test.each([
732+
[
733+
"unshift, write to an element moved past the base length",
734+
d => {
735+
d.arr.unshift({...added})
736+
d.arr[3].a = 99
737+
}
738+
],
739+
[
740+
"unshift, write to a nested object of an element moved past the base length",
741+
d => {
742+
d.arr.unshift({...added})
743+
d.arr[3].meta.n = 99
744+
}
745+
],
746+
[
747+
"unshift of two items, write to the last element",
748+
d => {
749+
d.arr.unshift({...added}, {...added, id: 10})
750+
d.arr[4].a = 99
751+
}
752+
],
753+
[
754+
"splice insert, write to an element moved past the base length",
755+
d => {
756+
d.arr.splice(1, 0, {...added})
757+
d.arr[3].a = 99
758+
}
759+
],
760+
[
761+
"unshift, write to an element that stays within the base length",
762+
d => {
763+
d.arr.unshift({...added})
764+
d.arr[1].a = 99
765+
}
766+
],
767+
[
768+
"shift, write to a shifted element",
769+
d => {
770+
d.arr.shift()
771+
d.arr[1].a = 99
772+
}
773+
],
774+
[
775+
"reverse, write to a relocated element",
776+
d => {
777+
d.arr.reverse()
778+
d.arr[0].a = 99
779+
}
780+
],
781+
[
782+
"push, write to the pushed item",
783+
d => {
784+
d.arr.push({...added})
785+
d.arr[3].a = 99
786+
}
787+
],
788+
[
789+
"write, then unshift",
790+
d => {
791+
d.arr[2].a = 99
792+
d.arr.unshift({...added})
793+
}
794+
]
795+
])("%s", (_title, recipe) => {
796+
const base = createBase()
797+
const [result, patches, inverse] = produceWithPatches(base, recipe)
798+
expect(base).toEqual(createBase())
799+
expect(applyPatches(base, patches)).toEqual(result)
800+
expect(applyPatches(result, inverse)).toEqual(base)
801+
})
802+
})
803+
719804
describe("return values", () => {
720805
test("pop returns removed item", () => {
721806
const base = {items: [{id: 1}, {id: 2}]}

‎src/plugins/patches.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ export function enablePatches() {
9090
return null // Key deleted
9191
}
9292

93+
// Once an array's elements have been shifted or reordered, a child's index
94+
// no longer matches its position in the base. The array generates a patch
95+
// for every index that changed (carrying the finalized child), so a nested
96+
// patch at the child's new index is redundant and, for an index past the
97+
// base length, points at a path that does not exist yet when it is applied.
98+
if (
99+
state.parent_!.type_ === ArchType.Array &&
100+
(state.parent_ as ProxyArrayState).allIndicesReassigned_
101+
) {
102+
return null
103+
}
104+
93105
// Step 5: Add key to path
94106
path.push(key)
95107
}

0 commit comments

Comments
 (0)