Skip to content
Myr

patches.ts

This file

Path

src/plugins/patches.ts

268 lines · 6.4 KiB · TypeScript

Last changed by

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

History of this file

Take it

More WIPMichel Weststratebefore adoptionee1c977

lines 1–11why, one step back
1// Import types only! TODO: force in TS 3.8
2import {3 ImmerState,4 Patch,5 SetState,6 ES5ArrayState,7 ProxyArrayState,8 MapState,9 ES5ObjectState,10 ProxyObjectState,11 PatchPath,

TSDXMichel Weststratebefore adoption4c4fecc

lines 12–21why, one step back
12 get,
13 each,14 has,15 die,16 getArchtype,17 ProxyType,18 Archtype,19 isSet,20 isMap,21 loadPlugin

More WIPMichel Weststratebefore adoptionee1c977

line 22why, one step back
22} from "../internal"

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 23why, one step back
23import invariant from "tiny-invariant"

More WIPMichel Weststratebefore adoptionee1c977

line 24why, one step back
24

TSDXMichel Weststratebefore adoption4c4fecc

lines 25–51why, one step back
25export function enablePatches() {
26 function generatePatches(27 state: ImmerState,28 basePath: PatchPath,29 patches: Patch[],30 inversePatches: Patch[]31 ): void {32 switch (state.type) {33 case ProxyType.ProxyObject:34 case ProxyType.ES5Object:35 case ProxyType.Map:36 return generatePatchesFromAssigned(37 state,38 basePath,39 patches,40 inversePatches41 )42 case ProxyType.ES5Array:43 case ProxyType.ProxyArray:44 return generateArrayPatches(state, basePath, patches, inversePatches)45 case ProxyType.Set:46 return generateSetPatches(47 (state as any) as SetState,48 basePath,49 patches,50 inversePatches51 )

More WIPMichel Weststratebefore adoptionee1c977

line 52why, one step back
52 }

TSDXMichel Weststratebefore adoption4c4fecc

line 53why, one step back
53 }

More WIPMichel Weststratebefore adoptionee1c977

line 54why, one step back
54

TSDXMichel Weststratebefore adoption4c4fecc

lines 55–63why, one step back
55 function generateArrayPatches(
56 state: ES5ArrayState | ProxyArrayState,57 basePath: PatchPath,58 patches: Patch[],59 inversePatches: Patch[]60 ) {61 let {base, assigned, copy} = state62 /* istanbul ignore next */63 if (!copy) die()

More WIPMichel Weststratebefore adoptionee1c977

line 64why, one step back
64

TSDXMichel Weststratebefore adoption4c4fecc

lines 65–70why, one step back
65 // Reduce complexity by ensuring `base` is never longer.
66 if (copy.length < base.length) {67 // @ts-ignore68 ;[base, copy] = [copy, base]69 ;[patches, inversePatches] = [inversePatches, patches]70 }

More WIPMichel Weststratebefore adoptionee1c977

line 71why, one step back
71

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 73why, one step back
73

TSDXMichel Weststratebefore adoption4c4fecc

lines 74–78why, one step back
74 // Find the first replaced index.
75 let start = 076 while (base[start] === copy[start] && start < base.length) {77 ++start78 }

More WIPMichel Weststratebefore adoptionee1c977

line 79why, one step back
79

TSDXMichel Weststratebefore adoption4c4fecc

lines 80–84why, one step back
80 // Find the last replaced index. Search from the end to optimize splice patches.
81 let end = base.length82 while (end > start && base[end - 1] === copy[end + delta - 1]) {83 --end84 }

More WIPMichel Weststratebefore adoptionee1c977

line 85why, one step back
85

TSDXMichel Weststratebefore adoption4c4fecc

lines 86–88why, one step back
86 // Process replaced indices.
87 for (let i = start; i < end; ++i) {88 if (assigned[i] && copy[i] !== base[i]) {

More WIPMichel Weststratebefore adoptionee1c977

line 89why, one step back
89 const path = basePath.concat([i])

TSDXMichel Weststratebefore adoption4c4fecc

lines 90–91why, one step back
90 patches.push({
91 op: "replace",

More WIPMichel Weststratebefore adoptionee1c977

lines 92–93why, one step back
92 path,
93 value: copy[i]

TSDXMichel Weststratebefore adoption4c4fecc

line 94why, one step back
94 })

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 96–98why, one step back
96 op: "replace",
97 path,98 value: base[i]

More WIPMichel Weststratebefore adoptionee1c977

lines 99–102why, one step back
99 })
100 }101 }102

TSDXMichel Weststratebefore adoption4c4fecc

lines 103–115why, one step back
103 const replaceCount = patches.length
104105 // Process added indices.106 for (let i = end + delta - 1; i >= end; --i) {107 const path = basePath.concat([i])108 patches[replaceCount + i - end] = {109 op: "add",110 path,111 value: copy[i]112 }113 inversePatches.push({114 op: "remove",115 path

More WIPMichel Weststratebefore adoptionee1c977

lines 116–117why, one step back
116 })
117 }

TSDXMichel Weststratebefore adoption4c4fecc

line 118why, one step back
118 }

More WIPMichel Weststratebefore adoptionee1c977

line 119why, one step back
119

TSDXMichel Weststratebefore adoption4c4fecc

lines 120–143why, one step back
120 // This is used for both Map objects and normal objects.
121 function generatePatchesFromAssigned(122 state: MapState | ES5ObjectState | ProxyObjectState,123 basePath: PatchPath,124 patches: Patch[],125 inversePatches: Patch[]126 ) {127 const {base, copy} = state128 each(state.assigned!, (key, assignedValue) => {129 const origValue = get(base, key)130 const value = get(copy!, key)131 const op = !assignedValue ? "remove" : has(base, key) ? "replace" : "add"132 if (origValue === value && op === "replace") return133 const path = basePath.concat(key as any)134 patches.push(op === "remove" ? {op, path} : {op, path, value})135 inversePatches.push(136 op === "add"137 ? {op: "remove", path}138 : op === "remove"139 ? {op: "add", path, value: origValue}140 : {op: "replace", path, value: origValue}141 )142 })143 }

More WIPMichel Weststratebefore adoptionee1c977

line 144why, one step back
144

TSDXMichel Weststratebefore adoption4c4fecc

lines 145–151why, one step back
145 function generateSetPatches(
146 state: SetState,147 basePath: PatchPath,148 patches: Patch[],149 inversePatches: Patch[]150 ) {151 let {base, copy} = state

More WIPMichel Weststratebefore adoptionee1c977

line 152why, one step back
152

TSDXMichel Weststratebefore adoption4c4fecc

lines 153–187why, one step back
153 let i = 0
154 base.forEach(value => {155 if (!copy!.has(value)) {156 const path = basePath.concat([i])157 patches.push({158 op: "remove",159 path,160 value161 })162 inversePatches.unshift({163 op: "add",164 path,165 value166 })167 }168 i++169 })170 i = 0171 copy!.forEach(value => {172 if (!base.has(value)) {173 const path = basePath.concat([i])174 patches.push({175 op: "add",176 path,177 value178 })179 inversePatches.unshift({180 op: "remove",181 path,182 value183 })184 }185 i++186 })187 }

More WIPMichel Weststratebefore adoptionee1c977

line 188why, one step back
188

TSDXMichel Weststratebefore adoption4c4fecc

lines 189–191why, one step back
189 function applyPatches<T>(draft: T, patches: Patch[]): T {
190 patches.forEach(patch => {191 const {path, op} = patch

More WIPMichel Weststratebefore adoptionee1c977

line 192why, one step back
192

TSDXMichel Weststratebefore adoption4c4fecc

lines 193–198why, one step back
193 /* istanbul ignore next */
194 if (!path.length) die()195196 let base: any = draft197 for (let i = 0; i < path.length - 1; i++) {198 base = get(base, path[i])

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 199why, one step back
199 invariant(typeof base === "object","Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignore

TSDXMichel Weststratebefore adoption4c4fecc

line 200why, one step back
200 }

More WIPMichel Weststratebefore adoptionee1c977

line 201why, one step back
201

TSDXMichel Weststratebefore adoption4c4fecc

lines 202–211why, one step back
202 const type = getArchtype(base)
203 const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #411204 const key = path[path.length - 1]205 switch (op) {206 case "replace":207 switch (type) {208 case Archtype.Map:209 return base.set(key, value)210 /* istanbul ignore next */211 case Archtype.Set:

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 212why, one step back
212 invariant(false, 'Sets cannot have "replace" patches.')

TSDXMichel Weststratebefore adoption4c4fecc

lines 213–242why, one step back
213 default:
214 // if value is an object, then it's assigned by reference215 // in the following add or remove ops, the value field inside the patch will also be modifyed216 // so we use value from the cloned patch217 // @ts-ignore218 return (base[key] = value)219 }220 case "add":221 switch (type) {222 case Archtype.Array:223 return base.splice(key as any, 0, value)224 case Archtype.Map:225 return base.set(key, value)226 case Archtype.Set:227 return base.add(value)228 default:229 return (base[key] = value)230 }231 case "remove":232 switch (type) {233 case Archtype.Array:234 return base.splice(key as any, 1)235 case Archtype.Map:236 return base.delete(key)237 case Archtype.Set:238 return base.delete(patch.value)239 default:240 return delete base[key]241 }242 default:

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 243why, one step back
243 invariant(false, "Unsupported patch operation: " + op)

TSDXMichel Weststratebefore adoption4c4fecc

lines 244–245why, one step back
244 }
245 })

More WIPMichel Weststratebefore adoptionee1c977

line 246why, one step back
246

TSDXMichel Weststratebefore adoption4c4fecc

lines 247–248why, one step back
247 return draft
248 }

More WIPMichel Weststratebefore adoptionee1c977

line 249why, one step back
249

TSDXMichel Weststratebefore adoption4c4fecc

lines 250–264why, one step back
250 // TODO: optimize: this is quite a performance hit, can we detect intelligently when it is needed?
251 // E.g. auto-draft when new objects from outside are assigned and modified?252 // (See failing test when deepClone just returns obj)253 function deepClonePatchValue<T>(obj: T): T254 function deepClonePatchValue(obj: any) {255 if (!obj || typeof obj !== "object") return obj256 if (Array.isArray(obj)) return obj.map(deepClonePatchValue)257 if (isMap(obj))258 return new Map(259 Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])260 )261 if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))262 const cloned = Object.create(Object.getPrototypeOf(obj))263 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])264 return cloned

More WIPMichel Weststratebefore adoptionee1c977

line 265why, one step back
265 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 266–268why, one step back
266
267 loadPlugin("patches", {applyPatches, generatePatches})268}