patches.js
This file
Path
src/patches.js
136 lines · 3.4 KiB · JavaScript
Last changed by
style: use tabsuse-tabsbefore adoptiona9c0fd0
History of this fileTake it
The walk stopped after 200 commits: lines older than 72850a9 point at it.
fix: revert Map supportAlec Larsonbefore adoption47529d3
line 1why, one step back1import {each} from "./common"fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
line 2why, one step back2feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 3why, one step back3export function generatePatches(state, basePath, patches, inversePatches) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 4–6why, one step back4 Array.isArray(state.base)5 ? generateArrayPatches(state, basePath, patches, inversePatches)6 : generateObjectPatches(state, basePath, patches, inversePatches)fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 7–8why, one step back7}8fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
line 9why, one step back9function generateArrayPatches(state, basePath, patches, inversePatches) {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 10why, one step back10 let {base, copy, assigned} = statefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 11why, one step back11style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 12–16why, one step back12 // Reduce complexity by ensuring `base` is never longer.13 if (copy.length < base.length) {14 ;[base, copy] = [copy, base]15 ;[patches, inversePatches] = [inversePatches, patches]16 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 17why, one step back17style: use tabsuse-tabsbefore adoptiona9c0fd0
line 18why, one step back18 const delta = copy.length - base.lengthfeat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 19why, one step back19style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 20–24why, one step back20 // Find the first replaced index.21 let start = 022 while (base[start] === copy[start] && start < base.length) {23 ++start24 }feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 25why, one step back25style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 26–30why, one step back26 // Find the last replaced index. Search from the end to optimize splice patches.27 let end = base.length28 while (end > start && base[end - 1] === copy[end + delta - 1]) {29 --end30 }feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 31why, one step back31style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 32–47why, one step back32 // Process replaced indices.33 for (let i = start; i < end; ++i) {34 if (assigned[i] && copy[i] !== base[i]) {35 const path = basePath.concat([i])36 patches.push({37 op: "replace",38 path,39 value: copy[i]40 })41 inversePatches.push({42 op: "replace",43 path,44 value: base[i]45 })46 }47 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 48why, one step back48style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 49–50why, one step back49 const useRemove = end != base.length50 const replaceCount = patches.lengthrefactor: clean up #320Alec Larsonbefore adoptionfad74d3
line 51why, one step back51style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 52–66why, one step back52 // Process added indices.53 for (let i = end + delta - 1; i >= end; --i) {54 const path = basePath.concat([i])55 patches[replaceCount + i - end] = {56 op: "add",57 path,58 value: copy[i]59 }60 if (useRemove) {61 inversePatches.push({62 op: "remove",63 path64 })65 }66 }feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 67why, one step back67style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 68–75why, one step back68 // One "replace" patch reverses all non-splicing "add" patches.69 if (!useRemove) {70 inversePatches.push({71 op: "replace",72 path: basePath.concat(["length"]),73 value: base.length74 })75 }fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 76–77why, one step back76}77fix: revert Map supportAlec Larsonbefore adoption47529d3
line 78why, one step back78function generateObjectPatches(state, basePath, patches, inversePatches) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 79–94why, one step back79 const {base, copy} = state80 each(state.assigned, (key, assignedValue) => {81 const origValue = base[key]82 const value = copy[key]83 const op = !assignedValue ? "remove" : key in base ? "replace" : "add"84 if (origValue === value && op === "replace") return85 const path = basePath.concat(key)86 patches.push(op === "remove" ? {op, path} : {op, path, value})87 inversePatches.push(88 op === "add"89 ? {op: "remove", path}90 : op === "remove"91 ? {op: "add", path, value: origValue}92 : {op: "replace", path, value: origValue}93 )94 })fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
lines 95–97why, one step back95}9697export function applyPatches(draft, patches) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 98–135why, one step back98 for (let i = 0; i < patches.length; i++) {99 const patch = patches[i]100 const {path} = patch101 if (path.length === 0 && patch.op === "replace") {102 draft = patch.value103 } else {104 let base = draft105 for (let i = 0; i < path.length - 1; i++) {106 base = base[path[i]]107 if (!base || typeof base !== "object")108 throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignore109 }110 const key = path[path.length - 1]111 switch (patch.op) {112 case "replace":113 base[key] = patch.value114 break115 case "add":116 if (Array.isArray(base)) {117 // TODO: support "foo/-" paths for appending to an array118 base.splice(key, 0, patch.value)119 } else {120 base[key] = patch.value121 }122 break123 case "remove":124 if (Array.isArray(base)) {125 base.splice(key, 1)126 } else {127 delete base[key]128 }129 break130 default:131 throw new Error("Unsupported patch operation: " + patch.op)132 }133 }134 }135 return draftfix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9
line 136why, one step back136}