Skip to content
Myr

fix: undraftable values should not be cloned for patches, fixes #676

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 12f4cf8 · Browse files · 2 files+31 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+31 added−2 removed

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

2 files+31 added−2 removed

modified:__tests__/patch.js+28 added
1117 unmodified lines
1118 },1119 [{op: "add", path: ["num"], value: 42}]1120 )1121})11221123test("#676 patching Date objects", () => {1124 class Test {1125 constructor() {1126 this.test = true1127 }1128 perform() {1129 return "tested!"1130 }1131 }11321133 const [nextState, patches] = produceWithPatches({}, function(draft) {1134 draft.date = new Date(2020, 10, 10, 8, 8, 8, 3)1135 draft.test = new Test()1136 })11371138 expect(nextState.date.toJSON()).toMatchInlineSnapshot(1139 `"2020-11-10T08:08:08.003Z"`1140 )1141 expect(nextState.test.perform()).toBe("tested!")11421143 const rebuilt = applyPatches({}, patches)1144 expect(rebuilt.date).toBeInstanceOf(Date)1145 expect(rebuilt.date.toJSON()).toMatchInlineSnapshot(1146 `"2020-11-10T08:08:08.003Z"`1147 )1148 expect(rebuilt.date).toEqual(new Date(2020, 10, 10, 8, 8, 8, 3))1149})
modified:src/plugins/patches.ts+3 added-2 removed
23 unmodified lines
24 ArchtypeMap,25 ArchtypeSet,26 ArchtypeArray,27 die,28 isDraft28 isDraft,29 isDraftable30} from "../internal"3132export function enablePatches() {33 const REPLACE = "replace"
233 unmodified lines
267 // E.g. auto-draft when new objects from outside are assigned and modified?268 // (See failing test when deepClone just returns obj)269 function deepClonePatchValue<T>(obj: T): T270 function deepClonePatchValue(obj: any) {270 if (!obj || typeof obj !== "object") return obj271 if (!isDraftable(obj)) return obj272 if (Array.isArray(obj)) return obj.map(deepClonePatchValue)273 if (isMap(obj))274 return new Map(275 Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])
19 unmodified lines