Skip to content
Myr

fix: `Set` inside patches were not correctly cloned. Fixes #521

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

authored by Michel Weststrate · committed by Michel Weststrate · parent fb0f3a0 · Browse files · 2 files+22 added−2 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+22 added−2 removed

  • tests:+19 added−0 removed
  • other:+3 added−2 removed

2 files+22 added−2 removed

modified:__tests__/patch.js+19 added
975 unmodified lines
976977 const final = applyPatches(state, patches)978 expect(final).toEqual(nextState)979})980981test("#521", () => {982 const state = new Map()983984 const [nextState, patches] = produceWithPatches(state, draft => {985 draft.set("hello", new Set(["world"]))986 })987988 let patchedState = applyPatches(state, patches)989 expect(patchedState).toEqual(nextState)990991 const [nextStateV2, patchesV2] = produceWithPatches(nextState, draft => {992 draft.get("hello").add("immer")993 })994995 expect(applyPatches(nextState, patchesV2)).toEqual(996 new Map([["hello", new Set(["world", "immer"])]])997 )998})
modified:src/patches.ts+3 added-2 removed
12 unmodified lines
13 MapState,14 ES5ObjectState,15 ProxyObjectState,16 Archtype,17 isMap17 isMap,18 isSet19} from "./internal"2021export type PatchPath = (string | number)[]22
232 unmodified lines
255 if (isMap(obj))256 return new Map(257 Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])258 )258 // Not needed: if (isSet(obj)) return new Set(Array.from(obj.values()).map(deepClone))259 if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))260 const cloned = Object.create(Object.getPrototypeOf(obj))261 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])262 return cloned263}