patches.js
This file
Path
src/patches.js
186 lines · 4.3 KiB · JavaScript
Last changed by
feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
History of this fileTake it
The walk stopped after 200 commits: lines older than 8a1c84c point at it.
feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 1why, one step back1import {get, each, isMap, isSet, has, clone} from "./common"ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
line 2why, one step back2feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 3why, one step back3export function generatePatches(state, basePath, patches, inversePatches) {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 4–10why, one step back4 const generatePatchesFn = Array.isArray(state.base)5 ? generateArrayPatches6 : isSet(state.base)7 ? generateSetPatches8 : generatePatchesFromAssigned910 generatePatchesFn(state, basePath, patches, inversePatches)ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
lines 11–12why, one step back11}12fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77
line 13why, one step back13function generateArrayPatches(state, basePath, patches, inversePatches) {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 14why, one step back14 let {base, copy, assigned} = statefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 15why, one step back15style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 16–20why, one step back16 // Reduce complexity by ensuring `base` is never longer.17 if (copy.length < base.length) {18 ;[base, copy] = [copy, base]19 ;[patches, inversePatches] = [inversePatches, patches]20 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 21why, one step back21style: use tabsuse-tabsbefore adoptiona9c0fd0
line 22why, one step back22 const delta = copy.length - base.lengthfeat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 23why, one step back23style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 24–28why, one step back24 // Find the first replaced index.25 let start = 026 while (base[start] === copy[start] && start < base.length) {27 ++start28 }feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 29why, one step back29style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 30–34why, one step back30 // Find the last replaced index. Search from the end to optimize splice patches.31 let end = base.length32 while (end > start && base[end - 1] === copy[end + delta - 1]) {33 --end34 }feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f
line 35why, one step back35style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 36–51why, one step back36 // Process replaced indices.37 for (let i = start; i < end; ++i) {38 if (assigned[i] && copy[i] !== base[i]) {39 const path = basePath.concat([i])40 patches.push({41 op: "replace",42 path,43 value: copy[i]44 })45 inversePatches.push({46 op: "replace",47 path,48 value: base[i]49 })50 }51 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 52why, one step back52style: use tabsuse-tabsbefore adoptiona9c0fd0
line 53why, one step back53 const replaceCount = patches.lengthrefactor: clean up #320Alec Larsonbefore adoptionfad74d3
line 54why, one step back54style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 55–63why, one step back55 // Process added indices.56 for (let i = end + delta - 1; i >= end; --i) {57 const path = basePath.concat([i])58 patches[replaceCount + i - end] = {59 op: "add",60 path,61 value: copy[i]62 }63 inversePatches.push({Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-deleteMichel Weststratebefore adoptionac61b8d
lines 64–65why, one step back64 op: "remove",65 pathstyle: use tabsuse-tabsbefore adoptiona9c0fd0
lines 66–67why, one step back66 })67 }ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
lines 68–69why, one step back68}69feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 70–71why, one step back70// This is used for both Map objects and normal objects.71function generatePatchesFromAssigned(state, basePath, patches, inversePatches) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 72–73why, one step back72 const {base, copy} = state73 each(state.assigned, (key, assignedValue) => {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 74–76why, one step back74 const origValue = get(base, key)75 const value = get(copy, key)76 const op = !assignedValue ? "remove" : has(base, key) ? "replace" : "add"style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 77–87why, one step back77 if (origValue === value && op === "replace") return78 const path = basePath.concat(key)79 patches.push(op === "remove" ? {op, path} : {op, path, value})80 inversePatches.push(81 op === "add"82 ? {op: "remove", path}83 : op === "remove"84 ? {op: "add", path, value: origValue}85 : {op: "replace", path, value: origValue}86 )87 })ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
lines 88–89why, one step back88}89feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 90–128why, one step back90function generateSetPatches(state, basePath, patches, inversePatches) {91 let {base, copy} = state9293 let i = 094 for (const value of base) {95 if (!copy.has(value)) {96 const path = basePath.concat([i])97 patches.push({98 op: "remove",99 path,100 value101 })102 inversePatches.unshift({103 op: "add",104 path,105 value106 })107 }108 i++109 }110 i = 0111 for (const value of copy) {112 if (!base.has(value)) {113 const path = basePath.concat([i])114 patches.push({115 op: "add",116 path,117 value118 })119 inversePatches.unshift({120 op: "remove",121 path,122 value123 })124 }125 i++126 }127}128fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
lines 129–132why, one step back129export const applyPatches = (draft, patches) => {130 for (const patch of patches) {131 const {path, op} = patch132fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
line 133why, one step back133 if (!path.length) throw new Error("Illegal state")fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
line 134why, one step back134fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
lines 135–136why, one step back135 let base = draft136 for (let i = 0; i < path.length - 1; i++) {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 137why, one step back137 base = get(base, path[i])fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
line 138why, one step back138 if (!base || typeof base !== "object")fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
line 139why, one step back139 throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignorefix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
line 140why, one step back140 }fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
line 141why, one step back141feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 142–143why, one step back142 const value = clone(patch.value) // used to clone patch to ensure original patch is not modified, see #411143fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
line 144why, one step back144 const key = path[path.length - 1]fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
line 145why, one step back145 switch (op) {fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
line 146why, one step back146 case "replace":feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 147–150why, one step back147 if (isMap(base)) {148 base.set(key, value)149 } else if (isSet(base)) {150 throw new Error('Sets cannot have "replace" patches.')fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
line 151why, one step back151 } else {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 152–154why, one step back152 // if value is an object, then it's assigned by reference153 // in the following add or remove ops, the value field inside the patch will also be modifyed154 // so we use value from the cloned patchfix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
line 155why, one step back155 base[key] = valuefix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
lines 156–157why, one step back156 }157 breakfeat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 158–160why, one step back158 case "add":159 if (isSet(base)) {160 base.delete(patch.value)fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
line 161why, one step back161 }feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 162–178why, one step back162163 Array.isArray(base)164 ? base.splice(key, 0, value)165 : isMap(base)166 ? base.set(key, value)167 : isSet(base)168 ? base.add(value)169 : (base[key] = value)170 break171 case "remove":172 Array.isArray(base)173 ? base.splice(key, 1)174 : isMap(base)175 ? base.delete(key)176 : isSet(base)177 ? base.delete(patch.value)178 : delete base[key]fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e
lines 179–180why, one step back179 break180 default:fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
line 181why, one step back181 throw new Error("Unsupported patch operation: " + op)style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 182–183why, one step back182 }183 }fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d
line 184why, one step back184style: use tabsuse-tabsbefore adoptiona9c0fd0
line 185why, one step back185 return draftci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
line 186why, one step back186}