Skip to content

Commit

Permalink
fix: clear map creates invalid patches, fixes #663
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Sep 9, 2020
2 parents bf90358 + 08f72a0 commit bacc1e0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
22 changes: 22 additions & 0 deletions __tests__/map-set.js
Expand Up @@ -244,5 +244,27 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
expect(map1.has("key")).toBe(true)
expect(map1.get("key")).toBe(undefined)
})

test("#663 - clear map", () => {
const map = new Map([
["a", "b"],
["b", "c"]
])
const result = produceWithPatches(map, draft => {
draft.clear()
})

expect(result).toEqual([
new Map(),
[
{op: "remove", path: ["a"]},
{op: "remove", path: ["b"]}
],
[
{op: "add", path: ["a"], value: "b"},
{op: "add", path: ["b"], value: "c"}
]
])
})
})
}
6 changes: 5 additions & 1 deletion src/plugins/mapset.ts
Expand Up @@ -15,7 +15,8 @@ import {
markChanged,
ProxyTypeMap,
ProxyTypeSet,
die
die,
each
} from "../internal"

export function enableMapSet() {
Expand Down Expand Up @@ -110,6 +111,9 @@ export function enableMapSet() {
prepareMapCopy(state)
markChanged(state)
state.assigned_ = new Map()
each(state.base_, key => {
state.assigned_!.set(key, false)
})
return state.copy_!.clear()
}

Expand Down

0 comments on commit bacc1e0

Please sign in to comment.