[go: up one dir, main page]

Skip to content

Commit

Permalink
fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (i…
Browse files Browse the repository at this point in the history
  • Loading branch information
jscheid authored Jan 11, 2022
1 parent 8507692 commit e140918
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 21 additions & 0 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -1319,3 +1319,24 @@ test("#791 patch for nothing is stored as undefined", () => {

expect(applyPatches({}, patches)).toEqual(undefined)
})

test("#876 Ensure empty patch set for atomic set+delete on Map", () => {
{
const [newState, patches] = produceWithPatches(
new Map([["foo", "baz"]]),
draft => {
draft.set("foo", "bar")
draft.delete("foo")
}
)
expect(patches).toEqual([{op: "remove", path: ["foo"]}])
}

{
const [newState, patches] = produceWithPatches(new Map(), draft => {
draft.set("foo", "bar")
draft.delete("foo")
})
expect(patches).toEqual([])
}
})
6 changes: 5 additions & 1 deletion src/plugins/mapset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ export function enableMapSet() {
assertUnrevoked(state)
prepareMapCopy(state)
markChanged(state)
state.assigned_!.set(key, false)
if (state.base_.has(key)) {
state.assigned_!.set(key, false)
} else {
state.assigned_!.delete(key)
}
state.copy_!.delete(key)
return true
}
Expand Down

0 comments on commit e140918

Please sign in to comment.