Skip to content
Myr

Ensure values in patches are never a draft

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

When you are generating patches in a nested reducer, patches values can be a Proxy. They need to be cloned before stored into the patch otherwise the patch can not be applied as the proxy will be revoked. Fixes #559

authored by Renaud Chaput · committed by Renaud Chaput · parent 0722281 · 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

2 files+51 added−6 removed

modified:__tests__/patch.js+35 added-1 removed
1 unmodified line
2import produce, {3 setUseProxies,4 applyPatches,5 produceWithPatches,6 enableAllPlugins6 enableAllPlugins,7 isDraft8} from "../src/immer"910enableAllPlugins()11
1002 unmodified lines
1014 expect(applyPatches(nextState, patchesV2)).toEqual(1015 new Map([["hello", new Set(["world", "immer"])]])1016 )1017})10181019test.only("#559 patches works in a nested reducer with proxies", () => {1020 setUseProxies(true)10211022 const state = {1023 x: 1,1024 sub: {1025 y: [{a: 0}, {a: 1}]1026 }1027 }10281029 const changes = []1030 const inverseChanges = []10311032 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 })10461047 const reversedSubState = applyPatches(newState.sub, inverseChanges)10481049 expect(reversedSubState).toMatchObject(state.sub)1050})
modified:src/plugins/patches.ts+16 added-5 removed
24 unmodified lines
25 ArchtypeSet,26 ArchtypeArray,27 die28} from "../internal"29import {isDraft} from "../utils/common"3031export function enablePatches() {32 const REPLACE = "replace"33 const ADD = "add"
64 unmodified lines
98 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 lines
118 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 path
19 unmodified lines
148 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 lines
291 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])292 return cloned293 }294295 function clonePatchValueIfNeeded<T>(obj: T): T {296 if (isDraft(obj)) {297 return deepClonePatchValue(obj)298 } else return obj299 }300301 loadPlugin("Patches", {302 applyPatches_,303 generatePatches_,304 generateReplacementPatches_
2 unmodified lines