Skip to content

Commit be4400b

Browse files
committed
fix(patches): fix array patches
1 parent 860714e commit be4400b

File tree

10 files changed

+19757
-376
lines changed

10 files changed

+19757
-376
lines changed

src/patch.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import {
44
type Patches,
55
type ProxyDraft,
66
} from './interface';
7-
import { cloneIfNeeded, escapePath, get, has, isEqual } from './utils';
7+
import {
8+
cloneIfNeeded,
9+
escapePath,
10+
get,
11+
getProxyDraft,
12+
has,
13+
isEqual,
14+
latest,
15+
} from './utils';
816

917
function generateArrayPatches(
1018
proxyState: ProxyDraft<Array<any>>,
@@ -20,7 +28,12 @@ function generateArrayPatches(
2028
[patches, inversePatches] = [inversePatches, patches];
2129
}
2230
for (let index = 0; index < original.length; index += 1) {
23-
if (assignedMap!.get(index.toString()) && copy[index] !== original[index]) {
31+
const currentProxyDraft = getProxyDraft(copy[index]);
32+
if (
33+
assignedMap!.get(index.toString()) &&
34+
(currentProxyDraft ? latest(currentProxyDraft) : copy[index]) !==
35+
original[index]
36+
) {
2437
const _path = basePath.concat([index]);
2538
const path = escapePath(_path, pathAsArray);
2639
patches.push({

src/utils/finalize.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ export function finalizePatches(
9999
target.assignedMap &&
100100
target.assignedMap.size > 0 &&
101101
!target.finalized;
102+
const parentAssigned = target.parent?.assignedMap?.get(
103+
target.parent.type === DraftType.Array ? target.key!.toString() : target.key
104+
);
102105
if (shouldFinalize) {
103-
if (patches && inversePatches) {
106+
if (!parentAssigned && patches && inversePatches) {
104107
const basePath = getPath(target);
105108
if (basePath) {
106109
generatePatches(target, basePath, patches, inversePatches);

0 commit comments

Comments
 (0)