Skip to content

Commit 3877289

Browse files
committed
fix(finalize): fix finalization issue
1 parent 4472293 commit 3877289

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

src/apply.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export function apply<T extends object, F extends boolean = false>(
5252
let base: any = draft;
5353
for (let index = 0; index < path.length - 1; index += 1) {
5454
const parentType = getType(base);
55-
const key = String(path[index]);
55+
let key = path[index];
56+
if (typeof key !== 'string' && typeof key !== 'number') {
57+
key = String(key);
58+
}
5659
if (
5760
((parentType === DraftType.Object ||
5861
parentType === DraftType.Array) &&

src/utils/finalize.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ export function handleValue(target: any, handledSet: WeakSet<any>) {
2727
if (isDraft(value)) {
2828
const proxyDraft = getProxyDraft(value)!;
2929
ensureShallowCopy(proxyDraft);
30-
const updatedValue = proxyDraft.assignedMap?.size
31-
? proxyDraft.copy
32-
: proxyDraft.original;
30+
// A draft where a child node has been changed, or assigned a value
31+
const updatedValue =
32+
proxyDraft.assignedMap?.size || proxyDraft.operated
33+
? proxyDraft.copy
34+
: proxyDraft.original;
3335
// final update value
3436
set(isSet ? setMap! : target, key, updatedValue);
3537
} else {

test/index.test.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,8 +2926,8 @@ test('#18 - map: assigning a non-draft with the same key - enablePatches', () =>
29262926
);
29272927
expect(created[0].map.get(0).one.two).toBe(2);
29282928

2929-
// expect(apply(baseState, created[1])).toEqual(created[0]);
2930-
// expect(apply(created[0], created[2])).toEqual(baseState);
2929+
expect(apply(baseState, created[1])).toEqual(created[0]);
2930+
expect(apply(created[0], created[2])).toEqual(baseState);
29312931
});
29322932

29332933
test('#18 - set: assigning a non-draft with the same key', () => {
@@ -3002,3 +3002,42 @@ test('#18 - array: assigning a non-draft with the same key - enablePatches', ()
30023002
expect(apply(baseState, created[1])).toEqual(created[0]);
30033003
expect(apply(created[0], created[2])).toEqual(baseState);
30043004
});
3005+
3006+
test('#18: assigning a non-draft with the different key - enablePatches', () => {
3007+
const baseState = {
3008+
array: [
3009+
{
3010+
one: {
3011+
two: 3,
3012+
},
3013+
},
3014+
],
3015+
};
3016+
3017+
const created = create(
3018+
baseState,
3019+
(draft) => {
3020+
draft.array[0].one.two = 2;
3021+
// @ts-ignore
3022+
draft.array1 = [0, { c: draft.array[0] }];
3023+
// @ts-ignore
3024+
draft.map = [0, new Map([[0, draft.array[0]]])];
3025+
// @ts-ignore
3026+
draft.set = [0, new Set([draft.array[0]])];
3027+
},
3028+
{
3029+
enablePatches: true,
3030+
}
3031+
);
3032+
// @ts-ignore
3033+
expect(created[0].array[0].one.two).toBe(2);
3034+
// @ts-ignore
3035+
expect(created[0].array1[1].c.one.two).toBe(2);
3036+
// @ts-ignore
3037+
expect(created[0].map[1].get(0).one.two).toBe(2);
3038+
// @ts-ignore
3039+
expect(Array.from(created[0].set[1])[0].one.two).toBe(2);
3040+
3041+
expect(apply(baseState, created[1])).toEqual(created[0]);
3042+
expect(apply(created[0], created[2])).toEqual(baseState);
3043+
});

0 commit comments

Comments
 (0)