Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map #878

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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