fix: ensure patches are reusable
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Shuumatsu · committed by Shuumatsu · parent a16deda · Browse files · 2 files·+55 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+55 added−12 removed
- tests:+27 added−1 removed
- other:+28 added−11 removed
Provenance
- Commit
c2e69d9- Tree
af9f239- Author
Shuumatsu <VanishingDante@gmail.com>- Committer
Shuumatsu <VanishingDante@gmail.com>- Parent
a16deda
2 files changed+55 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+28 added-11 removed
93 unmodified lines94959697989910010110298991001011021031041051061071081091101111121131141151161171181191081201211221231111241251261271281131291301311321331181341351201361371381391402 unmodified lines14314414514613114714814915015115293 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 (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, value} = clone(patch)
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}