Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[breaking change] Do not replace array.length as patch when removing items #964

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,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 @@ -574,7 +574,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 @@ -589,7 +590,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 @@ -634,7 +635,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 @@ -649,7 +653,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 @@ -664,7 +671,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 @@ -684,7 +694,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 @@ -699,7 +710,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 @@ -817,7 +829,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 @@ -829,7 +841,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 @@ -104,11 +104,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