diff --git a/__tests__/patch.js b/__tests__/patch.js index f9ca9f35..eee94a45 100644 --- a/__tests__/patch.js +++ b/__tests__/patch.js @@ -1256,3 +1256,24 @@ test("maps can store __proto__, prototype and constructor props", () => { expect(newMap.get("prototype").polluted).toBe("yes") expect(obj.polluted).toBe(undefined) }) + +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 + } + ]) +}) 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