Skip to content

Commit

Permalink
fix: Ignore equal reference assignments. Fixes #648
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Mar 17, 2021
2 parents cabcd3d + d435d0c commit 3b4286d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
])
})
3 changes: 3 additions & 0 deletions src/core/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ export const objectTraps: ProxyHandler<ProxyState> = {
prepareCopy(state)
markChanged(state)
}

if (state.copy_![prop] === value && typeof value !== "number") return true

// @ts-ignore
state.copy_![prop] = value
state.assigned_[prop] = true
Expand Down

0 comments on commit 3b4286d

Please sign in to comment.