Skip to content
Myr

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 file

Take 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 back
1import {each} from "./common"
2

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 3–6why, one step back
3export 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 back
7}
8

fix: applyPatches should mutate draftsaleclarsonbefore adoptiond41be77

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

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 11why, one step back
11

refactor: clean up #320Alec Larsonbefore adoptionfad74d3

line 12why, one step back
12 // Reduce complexity by ensuring `base` is never longer.

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 13–15why, one step back
13 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 back
16 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 17why, one step back
17

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 18–19why, one step back
18 const delta = copy.length - base.length
19

refactor: clean up #320Alec Larsonbefore adoptionfad74d3

line 20why, one step back
20 // Find the first replaced index.

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 21–25why, one step back
21 let start = 0
22 while (base[start] === copy[start] && start < base.length) {23 ++start24 }25

refactor: clean up #320Alec Larsonbefore adoptionfad74d3

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

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 30–31why, one step back
30 }
31

refactor: clean up #320Alec Larsonbefore adoptionfad74d3

lines 32–35why, one step back
32 // 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 back
36 patches.push({

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 37–38why, one step back
37 op: "replace",
38 path,

refactor: clean up #320Alec Larsonbefore adoptionfad74d3

line 39why, one step back
39 value: copy[i]

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 40why, one step back
40 })

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 41–43why, one step back
41 inversePatches.push({
42 op: "replace",43 path,

refactor: clean up #320Alec Larsonbefore adoptionfad74d3

line 44why, one step back
44 value: base[i]

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 45why, one step back
45 })

Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1

line 46why, one step back
46 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 47–48why, one step back
47 }
48

refactor: clean up #320Alec Larsonbefore adoptionfad74d3

lines 49–55why, one step back
49 const useRemove = end != base.length
50 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 back
56 op: "add",
57 path,

refactor: clean up #320Alec Larsonbefore adoptionfad74d3

line 58why, one step back
58 value: copy[i]

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 59–60why, one step back
59 }
60 if (useRemove) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 61why, one step back
61 inversePatches.push({

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 62–63why, one step back
62 op: "remove",
63 path

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 64why, one step back
64 })

Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1

lines 65–66why, one step back
65 }
66 }

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 67why, one step back
67

refactor: clean up #320Alec Larsonbefore adoptionfad74d3

line 68why, one step back
68 // One "replace" patch reverses all non-splicing "add" patches.

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 69–75why, one step back
69 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 back
76}
77

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 78–79why, one step back
78function generateObjectPatches(state, basePath, patches, inversePatches) {
79 const {base, copy} = state

Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1

line 80why, one step back
80 each(state.assigned, (key, assignedValue) => {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 81–83why, one step back
81 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 back
84 if (origValue === value && op === "replace") return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 85why, one step back
85 const path = basePath.concat(key)

Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1

lines 86–90why, one step back
86 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 back
91 ? {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 back
93 )
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 back
100 const {path} = patch
101 if (path.length === 0 && patch.op === "replace") {

Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1

lines 102–103why, one step back
102 draft = patch.value
103 } else {

improve applyPatches perfaleclarsonbefore adoptionfe82ed9

lines 104–107why, one step back
104 let base = draft
105 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 back
108 throw new Error("Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignore

improve applyPatches perfaleclarsonbefore adoptionfe82ed9

lines 109–110why, one step back
109 }
110 const key = path[path.length - 1]

Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1

lines 111–114why, one step back
111 switch (patch.op) {
112 case "replace":113 base[key] = patch.value114 break

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 115–122why, one step back
115 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 break

Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1

lines 123–124why, one step back
123 case "remove":
124 if (Array.isArray(base)) {

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 125why, one step back
125 base.splice(key, 1)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 126–128why, one step back
126 } else {
127 delete base[key]128 }

Fixed issue in the flow type for `nothing`Michel Weststratebefore adoption0d262e1

lines 129–136why, one step back
129 break
130 default:131 throw new Error("Unsupported patch operation: " + patch.op)132 }133 }134 }135 return draft136}