patches.js
This file
Path
src/patches.js
109 lines · 3.8 KiB · JavaScript
Last changed by
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
History of this fileTake it
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 1–2why, one step back1import {each} from "./common"2feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 3–6why, one step back3export function generatePatches(state, basePath, patches, inversePatches) {4 Array.isArray(state.base)5 ? generateArrayPatches(state, basePath, patches, inversePatches)6 : generateObjectPatches(state, basePath, patches, inversePatches)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 7–8why, one step back7}8feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 9–18why, one step back9export function generateArrayPatches(state, basePath, patches, inversePatches) {10 const {base, copy, assigned} = state11 const minLength = Math.min(base.length, copy.length)1213 // Look for replaced indices.14 for (let i = 0; i < minLength; i++) {15 if (assigned[i] && base[i] !== copy[i]) {16 const path = basePath.concat(i)17 patches.push({op: "replace", path, value: copy[i]})18 inversePatches.push({op: "replace", path, value: base[i]})Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 19–20why, one step back19 }20 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 21–29why, one step back2122 // Did the array expand?23 if (minLength < copy.length) {24 for (let i = minLength; i < copy.length; i++) {25 patches.push({26 op: "add",27 path: basePath.concat(i),28 value: copy[i]29 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 30–32why, one step back30 }31 inversePatches.push({32 op: "replace",feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 33–34why, one step back33 path: basePath.concat("length"),34 value: base.lengthMerge branch 'patches'Michel Weststratebefore adoption2513fb8
line 35why, one step back35 })feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 36–39why, one step back36 }3738 // ...or did it shrink?39 else if (minLength < base.length) {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 40–41why, one step back40 patches.push({41 op: "replace",feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 42–43why, one step back42 path: basePath.concat("length"),43 value: copy.lengthMerge branch 'patches'Michel Weststratebefore adoption2513fb8
line 44why, one step back44 })feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 45–50why, one step back45 for (let i = minLength; i < base.length; i++) {46 inversePatches.push({47 op: "add",48 path: basePath.concat(i),49 value: base[i]50 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 51–54why, one step back51 }52 }53}54feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 55–56why, one step back55function generateObjectPatches(state, basePath, patches, inversePatches) {56 const {base, copy} = stateMerge branch 'patches'Michel Weststratebefore adoption2513fb8
line 57why, one step back57 each(state.assigned, (key, assignedValue) => {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 58–62why, one step back58 const origValue = base[key]59 const value = copy[key]60 const op = !assignedValue ? "remove" : key in base ? "replace" : "add"61 if (origValue === base && op === "replace") return62 const path = basePath.concat(key)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 63–67why, one step back63 patches.push(op === "remove" ? {op, path} : {op, path, value})64 inversePatches.push(65 op === "add"66 ? {op: "remove", path}67 : op === "remove"feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 68–69why, one step back68 ? {op: "add", path, value: origValue}69 : {op: "replace", path, value: origValue}Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 70–76why, one step back70 )71 })72}7374export function applyPatches(draft, patches) {75 for (let i = 0; i < patches.length; i++) {76 const patch = patches[i]improve applyPatches perfaleclarsonbefore adoptionfe82ed9
lines 77–78why, one step back77 const {path} = patch78 if (path.length === 0 && patch.op === "replace") {Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 79–80why, one step back79 draft = patch.value80 } else {improve applyPatches perfaleclarsonbefore adoptionfe82ed9
lines 81–84why, one step back81 let base = draft82 for (let i = 0; i < path.length - 1; i++) {83 base = base[path[i]]84 if (!base || typeof base !== "object")feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 85why, one step back85 throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignoreimprove applyPatches perfaleclarsonbefore adoptionfe82ed9
lines 86–87why, one step back86 }87 const key = path[path.length - 1]Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 88–95why, one step back88 switch (patch.op) {89 case "replace":90 case "add":91 // TODO: add support is not extensive, it does not support insertion or `-` atm!92 base[key] = patch.value93 break94 case "remove":95 if (Array.isArray(base)) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 96–101why, one step back96 if (key !== base.length - 1)97 throw new Error(`Only the last index of an array can be removed, index: ${key}, length: ${base.length}`) // prettier-ignore98 base.length -= 199 } else {100 delete base[key]101 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 102–109why, one step back102 break103 default:104 throw new Error("Unsupported patch operation: " + patch.op)105 }106 }107 }108 return draft109}