Skip to content

Commit

Permalink
set assigned keys when clear map
Browse files Browse the repository at this point in the history
  • Loading branch information
LYF1999 committed Sep 3, 2020
1 parent bf90358 commit 08f72a0
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 08f72a0

Please # to comment.