fix: ensure that patches are reusable. Fixes #411
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 a16deda, 49b2e7b · Browse files · 2 files·+56 added−12 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+56 added−12 removed
- tests:+27 added−1 removed
- other:+29 added−11 removed
Provenance
- Commit
af3a59d- Tree
ebc9072- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
a16deda- Parent 2
49b2e7b
2 files changed+56 added−12 removed
modified:__tests__/patch.js+27 added-1 removed
1 "use strict"2-import produce, {setUseProxies, applyPatches} from "../src/index"2+import produce, {3+ setUseProxies,4+ applyPatches,5+ produceWithPatches6+} from "../src/index"7 8 jest.setTimeout(1000)9 10 function runPatchTest(base, producer, patches, inversePathes) {74 unmodified lines85 const patch = {op: "add", path: ["a", "b", "c"], value: 1}86 applyPatches({}, [patch])87 }).toThrowErrorMatchingSnapshot()88 })89+ it("applied patches cannot be modified", () => {90+ // see also: https://github.com/immerjs/immer/issues/41191+ const s0 = {92+ items: [1]93+ }94+95+ const [s1, p1] = produceWithPatches(s0, draft => {96+ draft.items = []97+ })98+99+ const replaceValueBefore = p1[0].value.slice()100+101+ const [s2, p2] = produceWithPatches(s1, draft => {102+ draft.items.push(2)103+ })104+105+ applyPatches(s0, [...p1, ...p2])106+107+ const replaceValueAfter = p1[0].value.slice()108+109+ expect(replaceValueAfter).toStrictEqual(replaceValueBefore)110+ })111 })112 113 describe("simple assignment - 1", () => {114 runPatchTest(443 unmodified linesmodified:src/patches.js+29 added-11 removed
93 unmodified lines94959697989910010110298991001011021031041051061071081091101111121131141151161171181191201081211221231241111251261271281291131301311321331341181351361201371381391401412 unmodified lines14414514614713114814915015115215393 unmodified lines ) })}
export function applyPatches(draft, patches) { // First, find a patch that replaces the entire state, if found, we don't have to apply earlier patches and modify the state for (let i = 0; i < patches.length; i++) { const patch = patches[i] const {path} = patch// used to clone patch to ensure original patch is not modifiedconst clone = obj => { if (obj === null || typeof obj !== "object") return obj
if (Array.isArray(obj)) return obj.map(clone)
const cloned = {} for (const key in obj) cloned[key] = clone(obj[key])
return cloned}
export const applyPatches = (draft, patches) => { for (const patch of patches) { const {path, op} = patch const value = clone(patch.value)
if (!path.length) throw new Error("Illegal state")
let base = draft for (let i = 0; i < path.length - 1; i++) { base = base[path[i]] if (!base || typeof base !== "object") throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignore throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignore }
const key = path[path.length - 1] switch (patch.op) { switch (op) { case "replace": // if value is an object, then it's assigned by reference // in the following add or remove ops, the value field inside the patch will also be modifyed // so we use value from the cloned patch base[key] = patch.value base[key] = value break case "add": if (Array.isArray(base)) { // TODO: support "foo/-" paths for appending to an array base.splice(key, 0, patch.value) base.splice(key, 0, value) } else { base[key] = patch.value base[key] = value } break case "remove": if (Array.isArray(base)) {2 unmodified lines delete base[key] } break default: throw new Error("Unsupported patch operation: " + patch.op) throw new Error("Unsupported patch operation: " + op) } }
return draft}