Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions __tests__/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,18 @@ function runBaseTest(
expect(() => m.set("x", 3)).toThrowErrorMatchingSnapshot()
})

it("revokes map proxies for reads that do not go through get", () => {
let m
produce(baseState, s => {
m = s.aMap
})
const revoked = /has been revoked|minified error nr: 3/
expect(() => m.has("jedi")).toThrow(revoked)
expect(() => m.size).toThrow(revoked)
expect(() => m.keys()).toThrow(revoked)
expect(() => m.forEach(() => {})).toThrow(revoked)
})

it("does not draft map keys", () => {
// anything else would be terribly confusing
const key = {a: 1}
Expand Down Expand Up @@ -2638,6 +2650,14 @@ function runBaseTest(
expect(() => m.add("x")).toThrowErrorMatchingSnapshot()
})

it("revokes set size reads", () => {
let m
produce(baseState, s => {
m = s.aSet
})
expect(() => m.size).toThrow(/has been revoked|minified error nr: 3/)
})

it("does support instanceof Set", () => {
const set = new Set()
produce(set, d => {
Expand Down
17 changes: 13 additions & 4 deletions src/plugins/mapset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ export function enableMapSet() {
}

get size(): number {
return latest(this[DRAFT_STATE]).size
const state: MapState = this[DRAFT_STATE]
assertUnrevoked(state)
return latest(state).size
}

has(key: any): boolean {
return latest(this[DRAFT_STATE]).has(key)
const state: MapState = this[DRAFT_STATE]
assertUnrevoked(state)
return latest(state).has(key)
}

set(key: any, value: any) {
Expand Down Expand Up @@ -109,6 +113,7 @@ export function enableMapSet() {

forEach(cb: (value: any, key: any, self: any) => void, thisArg?: any) {
const state: MapState = this[DRAFT_STATE]
assertUnrevoked(state)
latest(state).forEach((_value: any, key: any, _map: any) => {
cb.call(thisArg, this.get(key), key, this)
})
Expand All @@ -132,7 +137,9 @@ export function enableMapSet() {
}

keys(): IterableIterator<any> {
return latest(this[DRAFT_STATE]).keys()
const state: MapState = this[DRAFT_STATE]
assertUnrevoked(state)
return latest(state).keys()
}

values(): IterableIterator<any> {
Expand Down Expand Up @@ -225,7 +232,9 @@ export function enableMapSet() {
}

get size(): number {
return latest(this[DRAFT_STATE]).size
const state: SetState = this[DRAFT_STATE]
assertUnrevoked(state)
return latest(state).size
}

has(value: any): boolean {
Expand Down