Skip to content

Commit

Permalink
breaking: Emit remove instead of length replace patches when removing…
Browse files Browse the repository at this point in the history
… items from an array, #964
  • Loading branch information
mweststrate committed Mar 23, 2023
2 parents ec72486 + cb5712a commit d5aba94
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
32 changes: 22 additions & 10 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ describe("arrays - splice middle", () => {
},
[
{op: "replace", path: ["x", 1], value: 3},
{op: "replace", path: ["x", "length"], value: 2}
{op: "remove", path: ["x", 2]}
]
)
})
Expand All @@ -583,7 +583,8 @@ describe("arrays - multiple splice", () => {
{op: "replace", path: [1], value: 3},
{op: "replace", path: [2], value: 3},
{op: "replace", path: [4], value: 0},
{op: "replace", path: ["length"], value: 5}
{op: "remove", path: [6]},
{op: "remove", path: [5]}
]
)
})
Expand All @@ -598,7 +599,7 @@ describe("arrays - modify and shrink", () => {
},
[
{op: "replace", path: ["x", 0], value: 4},
{op: "replace", path: ["x", "length"], value: 2}
{op: "remove", path: ["x", 2]}
],
[
{op: "replace", path: ["x", 0], value: 1},
Expand Down Expand Up @@ -643,7 +644,10 @@ describe("arrays - truncate", () => {
d => {
d.x.length -= 2
},
[{op: "replace", path: ["x", "length"], value: 1}],
[
{op: "remove", path: ["x", 2]},
{op: "remove", path: ["x", 1]}
],
[
{op: "add", path: ["x", 1], value: 2},
{op: "add", path: ["x", 2], value: 3}
Expand All @@ -658,7 +662,10 @@ describe("arrays - pop twice", () => {
d.x.pop()
d.x.pop()
},
[{op: "replace", path: ["x", "length"], value: 1}]
[
{op: "remove", path: ["x", 2]},
{op: "remove", path: ["x", 1]}
]
)
})

Expand All @@ -673,7 +680,10 @@ describe("arrays - push multiple", () => {
{op: "add", path: ["x", 3], value: 4},
{op: "add", path: ["x", 4], value: 5}
],
[{op: "replace", path: ["x", "length"], value: 3}]
[
{op: "remove", path: ["x", 4]},
{op: "remove", path: ["x", 3]}
]
)
})

Expand All @@ -693,7 +703,8 @@ describe("arrays - splice (expand)", () => {
[
{op: "replace", path: ["x", 1], value: 2},
{op: "replace", path: ["x", 2], value: 3},
{op: "replace", path: ["x", "length"], value: 3}
{op: "remove", path: ["x", 4]},
{op: "remove", path: ["x", 3]}
]
)
})
Expand All @@ -708,7 +719,8 @@ describe("arrays - splice (shrink)", () => {
[
{op: "replace", path: ["x", 1], value: 6},
{op: "replace", path: ["x", 2], value: 5},
{op: "replace", path: ["x", "length"], value: 3}
{op: "remove", path: ["x", 4]},
{op: "remove", path: ["x", 3]}
],
[
{op: "replace", path: ["x", 1], value: 2},
Expand Down Expand Up @@ -826,7 +838,7 @@ describe("arrays - splice should should result in remove op.", () => {
d => {
d.splice(1, 1)
},
[{op: "replace", path: ["length"], value: 1}],
[{op: "remove", path: [1]}],
[{op: "add", path: [1], value: 2}]
)
})
Expand All @@ -838,7 +850,7 @@ describe("arrays - NESTED splice should should result in remove op.", () => {
d => {
d.a.b.c.splice(1, 1)
},
[{op: "replace", path: ["a", "b", "c", "length"], value: 1}],
[{op: "remove", path: ["a", "b", "c", 1]}],
[{op: "add", path: ["a", "b", "c", 1], value: 2}]
)
})
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ export function enablePatches() {
value: clonePatchValueIfNeeded(copy_[i])
})
}
if (base_.length < copy_.length) {
for (let i = copy_.length - 1; base_.length <= i; --i) {
const path = basePath.concat([i])
inversePatches.push({
op: REPLACE,
path: basePath.concat(["length"]),
value: base_.length
op: REMOVE,
path
})
}
}
Expand Down

0 comments on commit d5aba94

Please sign in to comment.