From fb5c3161d0ec485c28ce973c657f18c07e6a6986 Mon Sep 17 00:00:00 2001 From: youchao liu Date: Wed, 5 Feb 2020 00:50:43 +0800 Subject: [PATCH] fix: patches when delete object property in array --- __tests__/patch.js | 15 +++++++++++++++ src/es5.ts | 15 +++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/__tests__/patch.js b/__tests__/patch.js index f74dd5de..8eab494e 100644 --- a/__tests__/patch.js +++ b/__tests__/patch.js @@ -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]), diff --git a/src/es5.ts b/src/es5.ts index 7cd58e20..159e5fad 100644 --- a/src/es5.ts +++ b/src/es5.ts @@ -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]) }