Skip to content
Myr

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 file

Take 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 back
1import {get, each, isMap, isSet, has, clone} from "./common"

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

line 2why, one step back
2

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 3why, one step back
3export function generatePatches(state, basePath, patches, inversePatches) {

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 4–10why, one step back
4 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 back
11}
12

fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77

line 13why, one step back
13function generateArrayPatches(state, basePath, patches, inversePatches) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 14why, one step back
14 let {base, copy, assigned} = state

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 15why, one step back
15

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 16–20why, one step back
16 // 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 back
21

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 22why, one step back
22 const delta = copy.length - base.length

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 23why, one step back
23

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 24–28why, one step back
24 // 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 back
29

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 30–34why, one step back
30 // 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 back
35

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 36–51why, one step back
36 // 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 back
52

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 53why, one step back
53 const replaceCount = patches.length

refactor: clean up #320Alec Larsonbefore adoptionfad74d3

line 54why, one step back
54

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 55–63why, one step back
55 // 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 back
64 op: "remove",
65 path

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 66–67why, one step back
66 })
67 }

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

lines 68–69why, one step back
68}
69

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 70–71why, one step back
70// 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 back
72 const {base, copy} = state
73 each(state.assigned, (key, assignedValue) => {

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 74–76why, one step back
74 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 back
77 if (origValue === value && op === "replace") return
78 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 back
88}
89

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 90–128why, one step back
90function 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}128

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

lines 129–132why, one step back
129export const applyPatches = (draft, patches) => {
130 for (const patch of patches) {131 const {path, op} = patch132

fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e

line 133why, one step back
133 if (!path.length) throw new Error("Illegal state")

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

line 134why, one step back
134

fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e

lines 135–136why, one step back
135 let base = draft
136 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 back
137 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 back
138 if (!base || typeof base !== "object")

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

line 139why, one step back
139 throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignore

fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e

line 140why, one step back
140 }

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

line 141why, one step back
141

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 142–143why, one step back
142 const value = clone(patch.value) // used to clone patch to ensure original patch is not modified, see #411
143

fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e

line 144why, one step back
144 const key = path[path.length - 1]

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

line 145why, one step back
145 switch (op) {

fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e

line 146why, one step back
146 case "replace":

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 147–150why, one step back
147 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 back
151 } else {

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 152–154why, one step back
152 // if value is an object, then it's assigned by reference
153 // in the following add or remove ops, the value field inside the patch will also be modifyed154 // so we use value from the cloned patch

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

line 155why, one step back
155 base[key] = value

fix: don't unnecessary modify draft if replaying patches results in a new state anywayMichel Weststratebefore adoption96f2b2e

lines 156–157why, one step back
156 }
157 break

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 158–160why, one step back
158 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 back
161 }

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 162–178why, one step back
162
163 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 back
179 break
180 default:

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

line 181why, one step back
181 throw new Error("Unsupported patch operation: " + op)

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 182–183why, one step back
182 }
183 }

fix: ensure that patches are reusable. Fixes #411Michel Weststratebefore adoptionaf3a59d

line 184why, one step back
184

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 185why, one step back
185 return draft

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

line 186why, one step back
186}