patches.js
This file
Path
src/patches.js
136 lines · 4.3 KiB · JavaScript
Last changed by
refactor: clean up #320Alec Larsonbefore adoptionfad74d3
History of this fileTake it
The walk stopped after 200 commits: lines older than 0d262e1 point at it.
Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
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)Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
lines 7–8why, one step back7}8fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
line 9why, one step back9function generateArrayPatches(state, basePath, patches, inversePatches) {feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 10why, one step back10 let {base, copy, assigned} = statefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 11why, one step back11refactor: clean up #320Alec Larsonbefore adoptionfad74d3
line 12why, one step back12 // Reduce complexity by ensuring `base` is never longer.feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 13–15why, one step back13 if (copy.length < base.length) {14 ;[base, copy] = [copy, base]15 ;[patches, inversePatches] = [inversePatches, patches]Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
line 16why, one step back16 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 17why, one step back17feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 18–19why, one step back18 const delta = copy.length - base.length19refactor: clean up #320Alec Larsonbefore adoptionfad74d3
line 20why, one step back20 // Find the first replaced index.feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 21–25why, one step back21 let start = 022 while (base[start] === copy[start] && start < base.length) {23 ++start24 }25refactor: clean up #320Alec Larsonbefore adoptionfad74d3
lines 26–29why, 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 --endfeat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 30–31why, one step back30 }31refactor: clean up #320Alec Larsonbefore adoptionfad74d3
lines 32–35why, 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])feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 36why, one step back36 patches.push({feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 37–38why, one step back37 op: "replace",38 path,refactor: clean up #320Alec Larsonbefore adoptionfad74d3
line 39why, one step back39 value: copy[i]feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 40why, one step back40 })feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 41–43why, one step back41 inversePatches.push({42 op: "replace",43 path,refactor: clean up #320Alec Larsonbefore adoptionfad74d3
line 44why, one step back44 value: base[i]feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 45why, one step back45 })Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
line 46why, one step back46 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 47–48why, one step back47 }48refactor: clean up #320Alec Larsonbefore adoptionfad74d3
lines 49–55why, one step back49 const useRemove = end != base.length50 const replaceCount = patches.length5152 // Process added indices.53 for (let i = end + delta - 1; i >= end; --i) {54 const path = basePath.concat([i])55 patches[replaceCount + i - end] = {feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 56–57why, one step back56 op: "add",57 path,refactor: clean up #320Alec Larsonbefore adoptionfad74d3
line 58why, one step back58 value: copy[i]feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 59–60why, one step back59 }60 if (useRemove) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 61why, one step back61 inversePatches.push({feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 62–63why, one step back62 op: "remove",63 pathfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 64why, one step back64 })Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
lines 65–66why, one step back65 }66 }feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 67why, one step back67refactor: clean up #320Alec Larsonbefore adoptionfad74d3
line 68why, one step back68 // One "replace" patch reverses all non-splicing "add" patches.feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 69–75why, one step back69 if (!useRemove) {70 inversePatches.push({71 op: "replace",72 path: basePath.concat(["length"]),73 value: base.length74 })75 }Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
lines 76–77why, one step back76}77feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 78–79why, one step back78function generateObjectPatches(state, basePath, patches, inversePatches) {79 const {base, copy} = stateFixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
line 80why, one step back80 each(state.assigned, (key, assignedValue) => {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 81–83why, one step back81 const origValue = base[key]82 const value = copy[key]83 const op = !assignedValue ? "remove" : key in base ? "replace" : "add"fix: comparison typoaleclarsonbefore adoption48b371c
line 84why, one step back84 if (origValue === value && op === "replace") returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 85why, one step back85 const path = basePath.concat(key)Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
lines 86–90why, one step back86 patches.push(op === "remove" ? {op, path} : {op, path, value})87 inversePatches.push(88 op === "add"89 ? {op: "remove", path}90 : op === "remove"feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 91–92why, one step back91 ? {op: "add", path, value: origValue}92 : {op: "replace", path, value: origValue}Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
lines 93–99why, one step back93 )94 })95}9697export function applyPatches(draft, patches) {98 for (let i = 0; i < patches.length; i++) {99 const patch = patches[i]improve applyPatches perfaleclarsonbefore adoptionfe82ed9
lines 100–101why, one step back100 const {path} = patch101 if (path.length === 0 && patch.op === "replace") {Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
lines 102–103why, one step back102 draft = patch.value103 } else {improve applyPatches perfaleclarsonbefore adoptionfe82ed9
lines 104–107why, one step back104 let base = draft105 for (let i = 0; i < path.length - 1; i++) {106 base = base[path[i]]107 if (!base || typeof base !== "object")feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 108why, one step back108 throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignoreimprove applyPatches perfaleclarsonbefore adoptionfe82ed9
lines 109–110why, one step back109 }110 const key = path[path.length - 1]Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
lines 111–114why, one step back111 switch (patch.op) {112 case "replace":113 base[key] = patch.value114 breakfeat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
lines 115–122why, one step back115 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 breakFixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
lines 123–124why, one step back123 case "remove":124 if (Array.isArray(base)) {feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 125why, one step back125 base.splice(key, 1)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 126–128why, one step back126 } else {127 delete base[key]128 }Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1
lines 129–136why, one step back129 break130 default:131 throw new Error("Unsupported patch operation: " + patch.op)132 }133 }134 }135 return draft136}