Skip to content

Commit

Permalink
fix(array): fix issue
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Jan 4, 2025
1 parent f4e9ac9 commit d1df210
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,18 @@ const proxyHandler: ProxyHandler<ProxyDraft> = {
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,
Expand All @@ -142,9 +150,10 @@ const proxyHandler: ProxyHandler<ProxyDraft> = {
}
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;
Expand Down Expand Up @@ -299,7 +308,11 @@ export function createDraft<T extends object>(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);
Expand Down
108 changes: 108 additions & 0 deletions test/__snapshots__/apply.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -1957,6 +1993,15 @@ exports[`array - update 8`] = `

exports[`array - update 9`] = `
[
{
"op": "replace",
"path": [
"a",
0,
"i",
],
"value": 2,
},
{
"op": "replace",
"path": [
Expand Down Expand Up @@ -2160,6 +2205,15 @@ exports[`array - update 9`] = `

exports[`array - update 10`] = `
[
{
"op": "replace",
"path": [
"a",
0,
"i",
],
"value": 1,
},
{
"op": "replace",
"path": [
Expand Down Expand Up @@ -2365,6 +2419,15 @@ exports[`array - update 10`] = `

exports[`array - update 11`] = `
[
{
"op": "replace",
"path": [
"a",
0,
"i",
],
"value": 2,
},
{
"op": "replace",
"path": [
Expand Down Expand Up @@ -2568,6 +2631,15 @@ exports[`array - update 11`] = `

exports[`array - update 12`] = `
[
{
"op": "replace",
"path": [
"a",
0,
"i",
],
"value": 1,
},
{
"op": "replace",
"path": [
Expand Down Expand Up @@ -3658,6 +3730,15 @@ exports[`array - update 16`] = `

exports[`array - update 17`] = `
[
{
"op": "replace",
"path": [
"a",
2,
"i",
],
"value": 2,
},
{
"op": "replace",
"path": [
Expand Down Expand Up @@ -3873,6 +3954,15 @@ exports[`array - update 17`] = `

exports[`array - update 18`] = `
[
{
"op": "replace",
"path": [
"a",
2,
"i",
],
"value": 1,
},
{
"op": "replace",
"path": [
Expand Down Expand Up @@ -4496,6 +4586,15 @@ exports[`array - update 20`] = `

exports[`array - update 21`] = `
[
{
"op": "replace",
"path": [
"a",
2,
"i",
],
"value": 18,
},
{
"op": "replace",
"path": [
Expand Down Expand Up @@ -4701,6 +4800,15 @@ exports[`array - update 21`] = `

exports[`array - update 22`] = `
[
{
"op": "replace",
"path": [
"a",
2,
"i",
],
"value": 17,
},
{
"op": "replace",
"path": [
Expand Down

0 comments on commit d1df210

Please sign in to comment.