From 3892e4d36060ad6f1ddb4144d3eb017e0d95618b Mon Sep 17 00:00:00 2001 From: Stefan Date: Wed, 21 Oct 2020 08:38:45 -0700 Subject: [PATCH 1/2] Proxy: Ignore setting a reference to itself. Fixes immerjs/immer#648 --- src/core/proxy.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/proxy.ts b/src/core/proxy.ts index 2469d73e..bfa8049b 100644 --- a/src/core/proxy.ts +++ b/src/core/proxy.ts @@ -157,6 +157,9 @@ export const objectTraps: ProxyHandler = { prepareCopy(state) markChanged(state) } + + if (state.copy_![prop] === value && typeof value !== "number") return true + // @ts-ignore state.copy_![prop] = value state.assigned_[prop] = true From 462f50667cc9c9913822debd5b9d8a4d94cb7ea6 Mon Sep 17 00:00:00 2001 From: Stefan Date: Tue, 17 Nov 2020 08:46:32 -0800 Subject: [PATCH 2/2] Added tests --- __tests__/patch.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/__tests__/patch.js b/__tests__/patch.js index 7baba6e5..3cdf4bbd 100644 --- a/__tests__/patch.js +++ b/__tests__/patch.js @@ -1147,3 +1147,24 @@ test("#676 patching Date objects", () => { ) expect(rebuilt.date).toEqual(new Date(2020, 10, 10, 8, 8, 8, 3)) }) + +test("#648 assigning object to itself should not change patches", () => { + const input = { + obj: { + value: 200 + } + } + + const [nextState, patches] = produceWithPatches(input, draft => { + draft.obj.value = 1 + draft.obj = draft.obj + }) + + expect(patches).toEqual([ + { + op: "replace", + path: ["obj", "value"], + value: 1 + } + ]) +})