fix: Ensure values in patches are never a draft, fixes #559
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by GitHub · parents 0ddddfc, 2b54ac4 · Browse files · 2 files·+51 added−6 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+51 added−6 removed
- tests:+35 added−1 removed
- other:+16 added−5 removed
Provenance
- Commit
33ecbd6- Tree
a23b62e- Author
Michel Weststrate <mweststrate@fb.com>- Committer
GitHub <noreply@github.com>- Parent 1
0ddddfc- Parent 2
2b54ac4
2 files changed+51 added−6 removed
modified:__tests__/patch.js+35 added-1 removed
1 unmodified line2 import produce, {3 setUseProxies,4 applyPatches,5 produceWithPatches,6- enableAllPlugins6+ enableAllPlugins,7+ isDraft8 } from "../src/immer"9 10 enableAllPlugins()11 1002 unmodified lines1014 expect(applyPatches(nextState, patchesV2)).toEqual(1015 new Map([["hello", new Set(["world", "immer"])]])1016 )1017 })1018+1019+test("#559 patches works in a nested reducer with proxies", () => {1020+ setUseProxies(true)1021+1022+ const state = {1023+ x: 1,1024+ sub: {1025+ y: [{a: 0}, {a: 1}]1026+ }1027+ }1028+1029+ const changes = []1030+ const inverseChanges = []1031+1032+ const newState = produce(state, draft => {1033+ draft.sub = produce(1034+ draft.sub,1035+ draft => {1036+ draft.y.pop()1037+ },1038+ (patches, inversePatches) => {1039+ expect(isDraft(inversePatches[0].value)).toBeFalsy()1040+ expect(inversePatches[0].value).toMatchObject({a: 1})1041+ changes.push(...patches)1042+ inverseChanges.push(...inversePatches)1043+ }1044+ )1045+ })1046+1047+ const reversedSubState = applyPatches(newState.sub, inverseChanges)1048+1049+ expect(reversedSubState).toMatchObject(state.sub)1050+})modified:src/plugins/patches.ts+16 added-5 removed
24 unmodified lines25 ArchtypeSet,26 ArchtypeArray,27 die28 } from "../internal"29+import {isDraft} from "../utils/common"30 31 export function enablePatches() {32 const REPLACE = "replace"33 const ADD = "add"64 unmodified lines98 const path = basePath.concat([i])99 patches.push({100 op: REPLACE,101 path,101- value: copy_[i]102+ // Need to maybe clone it, as it can in fact be the original value103+ // due to the base/copy inversion at the start of this function104+ value: clonePatchValueIfNeeded(copy_[i])105 })106 inversePatches.push({107 op: REPLACE,108 path,106- value: base_[i]109+ value: clonePatchValueIfNeeded(base_[i])110 })111 }112 }113 4 unmodified lines118 const path = basePath.concat([i])119 patches[replaceCount + i - end] = {120 op: ADD,121 path,119- value: copy_[i]122+ // Need to maybe clone it, as it can in fact be the original value123+ // due to the base/copy inversion at the start of this function124+ value: clonePatchValueIfNeeded(copy_[i])125 }126 inversePatches.push({127 op: REMOVE,128 path19 unmodified lines148 inversePatches.push(149 op === ADD150 ? {op: REMOVE, path}151 : op === REMOVE147- ? {op: ADD, path, value: origValue}148- : {op: REPLACE, path, value: origValue}152+ ? {op: ADD, path, value: clonePatchValueIfNeeded(origValue)}153+ : {op: REPLACE, path, value: clonePatchValueIfNeeded(origValue)}154 )155 })156 }157 133 unmodified lines291 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])292 return cloned293 }294 295+ function clonePatchValueIfNeeded<T>(obj: T): T {296+ if (isDraft(obj)) {297+ return deepClonePatchValue(obj)298+ } else return obj299+ }300+301 loadPlugin("Patches", {302 applyPatches_,303 generatePatches_,304 generateReplacementPatches_2 unmodified lines