Skip to content
Myr

fix: Clearing empty Set&Map should be noop (#682). Fixes #680

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Björgvin Ragnarsson · committed by GitHub · parent c9e7116 · Browse files · 2 files+28 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+28 added−10 removed

  • tests:+14 added−0 removed
  • other:+14 added−10 removed

2 files+28 added−10 removed

modified:__tests__/map-set.js+14 added
264 unmodified lines
265 {op: "add", path: ["b"], value: "c"}266 ]267 ])268 })269270 test("#680 - Clearing empty Set&Map should be noop", () => {271 const map = new Map()272 let result = produce(map, draft => {273 draft.clear()274 })275 expect(result).toBe(map)276277 const set = new Set()278 result = produce(set, draft => {279 draft.clear()280 })281 expect(result).toBe(set)282 })283 })284}
modified:src/plugins/mapset.ts+14 added-10 removed
106 unmodified lines
107108 p.clear = function() {109 const state: MapState = this[DRAFT_STATE]110 assertUnrevoked(state)111 if (latest(state).size) {111 prepareMapCopy(state)112 markChanged(state)113 state.assigned_ = new Map()114 each(state.base_, key => {115 state.assigned_!.set(key, false)116 })117 return state.copy_!.clear()112 prepareMapCopy(state)113 markChanged(state)114 state.assigned_ = new Map()115 each(state.base_, key => {116 state.assigned_!.set(key, false)117 })118 state.copy_!.clear()119 }120 }121122 p.forEach = function(123 cb: (value: any, key: any, self: any) => void,
150 unmodified lines
274275 p.clear = function() {276 const state: SetState = this[DRAFT_STATE]277 assertUnrevoked(state)278 if (latest(state).size) {276 prepareSetCopy(state)277 markChanged(state)278 return state.copy_!.clear()279 prepareSetCopy(state)280 markChanged(state)281 state.copy_!.clear()282 }283 }284285 p.values = function(): IterableIterator<any> {286 const state: SetState = this[DRAFT_STATE]
59 unmodified lines