From d1df210f49ea36451eb054d39a7140c6853af97a Mon Sep 17 00:00:00 2001 From: unadlib Date: Sat, 4 Jan 2025 20:34:02 +0800 Subject: [PATCH] fix(array): fix issue --- src/draft.ts | 23 ++++-- test/__snapshots__/apply.test.ts.snap | 108 ++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 5 deletions(-) diff --git a/src/draft.ts b/src/draft.ts index 0dd43bd..9286c07 100644 --- a/src/draft.ts +++ b/src/draft.ts @@ -123,10 +123,18 @@ const proxyHandler: ProxyHandler = { return value; } // Ensure that the assigned values are not drafted - if (value === peek(target.original, key) && !arrayHandling) { + if ( + !arrayHandling && + (value === peek(target.original, key) || + target.options.skipFinalization!.has(value)) + ) { + const has = target.options.skipFinalization!.has(value); + if (target.options.skipFinalization!.has(value)) { + target.options.skipFinalization!.delete(value); + } ensureShallowCopy(target); target.copy![key] = createDraft({ - original: target.original[key], + original: has ? target.copy![key] : target.original[key], parentDraft: target, key: target.type === DraftType.Array ? Number(key) : key, finalities: target.finalities, @@ -142,9 +150,10 @@ const proxyHandler: ProxyHandler = { } return target.copy![key]; } - if (arrayHandling && !isDraft(value)) { + if (arrayHandling && !isDraft(value) && isDraftable(value)) { target.options.skipFinalization!.add(value); - } else if (target.options.skipFinalization!.has(value)) { + } + if (!arrayHandling && target.options.skipFinalization!.has(value)) { target.options.skipFinalization!.delete(value); } return value; @@ -299,7 +308,11 @@ export function createDraft(createDraftOptions: { } finalizeSetValue(proxyDraft); finalizePatches(proxyDraft, generatePatches, patches, inversePatches); - if (__DEV__ && target.options.enableAutoFreeze) { + if ( + __DEV__ && + target.options.enableAutoFreeze && + typeof updatedValue === 'object' + ) { target.options.updatedValues = target.options.updatedValues ?? new WeakMap(); target.options.updatedValues.set(updatedValue, proxyDraft.original); diff --git a/test/__snapshots__/apply.test.ts.snap b/test/__snapshots__/apply.test.ts.snap index 6f9f0e7..e469c94 100644 --- a/test/__snapshots__/apply.test.ts.snap +++ b/test/__snapshots__/apply.test.ts.snap @@ -1549,6 +1549,24 @@ exports[`array - update 6`] = ` exports[`array - update 7`] = ` [ + { + "op": "replace", + "path": [ + "a", + 10, + "i", + ], + "value": 12, + }, + { + "op": "replace", + "path": [ + "a", + 0, + "i", + ], + "value": 2, + }, { "op": "replace", "path": [ @@ -1752,6 +1770,24 @@ exports[`array - update 7`] = ` exports[`array - update 8`] = ` [ + { + "op": "replace", + "path": [ + "a", + 10, + "i", + ], + "value": 11, + }, + { + "op": "replace", + "path": [ + "a", + 0, + "i", + ], + "value": 1, + }, { "op": "replace", "path": [ @@ -1957,6 +1993,15 @@ exports[`array - update 8`] = ` exports[`array - update 9`] = ` [ + { + "op": "replace", + "path": [ + "a", + 0, + "i", + ], + "value": 2, + }, { "op": "replace", "path": [ @@ -2160,6 +2205,15 @@ exports[`array - update 9`] = ` exports[`array - update 10`] = ` [ + { + "op": "replace", + "path": [ + "a", + 0, + "i", + ], + "value": 1, + }, { "op": "replace", "path": [ @@ -2365,6 +2419,15 @@ exports[`array - update 10`] = ` exports[`array - update 11`] = ` [ + { + "op": "replace", + "path": [ + "a", + 0, + "i", + ], + "value": 2, + }, { "op": "replace", "path": [ @@ -2568,6 +2631,15 @@ exports[`array - update 11`] = ` exports[`array - update 12`] = ` [ + { + "op": "replace", + "path": [ + "a", + 0, + "i", + ], + "value": 1, + }, { "op": "replace", "path": [ @@ -3658,6 +3730,15 @@ exports[`array - update 16`] = ` exports[`array - update 17`] = ` [ + { + "op": "replace", + "path": [ + "a", + 2, + "i", + ], + "value": 2, + }, { "op": "replace", "path": [ @@ -3873,6 +3954,15 @@ exports[`array - update 17`] = ` exports[`array - update 18`] = ` [ + { + "op": "replace", + "path": [ + "a", + 2, + "i", + ], + "value": 1, + }, { "op": "replace", "path": [ @@ -4496,6 +4586,15 @@ exports[`array - update 20`] = ` exports[`array - update 21`] = ` [ + { + "op": "replace", + "path": [ + "a", + 2, + "i", + ], + "value": 18, + }, { "op": "replace", "path": [ @@ -4701,6 +4800,15 @@ exports[`array - update 21`] = ` exports[`array - update 22`] = ` [ + { + "op": "replace", + "path": [ + "a", + 2, + "i", + ], + "value": 17, + }, { "op": "replace", "path": [