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

fix: patches when delete object property in array #526

Merged
merged 1 commit into from Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions __tests__/patch.js
Expand Up @@ -648,6 +648,21 @@ describe("arrays - splice (shrink)", () => {
)
})

describe.only("arrays - delete", () => {
runPatchTest(
{
x: [
{a: 1, b: 2},
{c: 3, d: 4}
]
},
d => {
delete d.x[1].c
},
[{op: "remove", path: ["x", 1, "c"]}]
)
})

describe("sets - add - 1", () => {
runPatchTest(
new Set([1]),
Expand Down
15 changes: 11 additions & 4 deletions src/es5.ts
Expand Up @@ -241,15 +241,22 @@ function markChangesRecursively(object: any) {
markChangedES5(state)
}
})
} else if (type === ProxyType.ES5Array && hasArrayChanges(state)) {
markChangedES5(state)
assigned.length = true
} else if (type === ProxyType.ES5Array) {
if (hasArrayChanges(state)) {
markChangedES5(state)
assigned.length = true
}

if (draft.length < base.length) {
for (let i = draft.length; i < base.length; i++) assigned[i] = false
} else {
for (let i = base.length; i < draft.length; i++) assigned[i] = true
}
for (let i = 0; i < draft.length; i++) {

// Minimum count is enough, the other parts has been processed.
const min = Math.min(draft.length, base.length)

for (let i = 0; i < min; i++) {
// Only untouched indices trigger recursion.
if (assigned[i] === undefined) markChangesRecursively(draft[i])
}
Expand Down