Skip to content
Myr

patches.ts

This file

Path

src/plugins/patches.ts

270 lines · 6.3 KiB · TypeScript

Last changed by

Moving files into foldersMichel Weststratebefore adoptionab49edc

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–17why, one step back
12 get,
13 each,14 has,15 getArchtype,16 isSet,17 isMap,

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 18–26why, one step back
18 loadPlugin,
19 ProxyTypeProxyObject,20 ProxyTypeES5Object,21 ProxyTypeMap,22 ProxyTypeES5Array,23 ProxyTypeProxyArray,24 ProxyTypeSet,25 ArchtypeMap,26 ArchtypeSet,

Preserve error codes in productionMichel Weststratebefore adoptiond005fab

lines 27–28why, one step back
27 ArchtypeArray,
28 die

More WIPMichel Weststratebefore adoptionee1c977

lines 29–30why, one step back
29} from "../internal"
30

TSDXMichel Weststratebefore adoption4c4fecc

line 31why, one step back
31export function enablePatches() {

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 32why, one step back
32 function generatePatches_(

TSDXMichel Weststratebefore adoption4c4fecc

lines 33–37why, one step back
33 state: ImmerState,
34 basePath: PatchPath,35 patches: Patch[],36 inversePatches: Patch[]37 ): void {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 38why, one step back
38 switch (state.type_) {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 39–41why, one step back
39 case ProxyTypeProxyObject:
40 case ProxyTypeES5Object:41 case ProxyTypeMap:

TSDXMichel Weststratebefore adoption4c4fecc

lines 42–47why, one step back
42 return generatePatchesFromAssigned(
43 state,44 basePath,45 patches,46 inversePatches47 )

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 48–49why, one step back
48 case ProxyTypeES5Array:
49 case ProxyTypeProxyArray:

TSDXMichel Weststratebefore adoption4c4fecc

line 50why, one step back
50 return generateArrayPatches(state, basePath, patches, inversePatches)

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 51why, one step back
51 case ProxyTypeSet:

TSDXMichel Weststratebefore adoption4c4fecc

lines 52–57why, one step back
52 return generateSetPatches(
53 (state as any) as SetState,54 basePath,55 patches,56 inversePatches57 )

More WIPMichel Weststratebefore adoptionee1c977

line 58why, one step back
58 }

TSDXMichel Weststratebefore adoption4c4fecc

line 59why, one step back
59 }

More WIPMichel Weststratebefore adoptionee1c977

line 60why, one step back
60

TSDXMichel Weststratebefore adoption4c4fecc

lines 61–66why, one step back
61 function generateArrayPatches(
62 state: ES5ArrayState | ProxyArrayState,63 basePath: PatchPath,64 patches: Patch[],65 inversePatches: Patch[]66 ) {

killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb

lines 67–68why, one step back
67 let {base_, assigned_} = state
68 let copy_ = state.copy_!

More WIPMichel Weststratebefore adoptionee1c977

line 69why, one step back
69

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 71why, one step back
71 if (copy_.length < base_.length) {

TSDXMichel Weststratebefore adoption4c4fecc

line 72why, one step back
72 // @ts-ignore

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 73why, one step back
73 ;[base_, copy_] = [copy_, base_]

TSDXMichel Weststratebefore adoption4c4fecc

lines 74–75why, one step back
74 ;[patches, inversePatches] = [inversePatches, patches]
75 }

More WIPMichel Weststratebefore adoptionee1c977

line 76why, one step back
76

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 77why, one step back
77 const delta = copy_.length - base_.length

More WIPMichel Weststratebefore adoptionee1c977

line 78why, one step back
78

TSDXMichel Weststratebefore adoption4c4fecc

lines 79–80why, one step back
79 // Find the first replaced index.
80 let start = 0

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 81why, one step back
81 while (base_[start] === copy_[start] && start < base_.length) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 82–83why, one step back
82 ++start
83 }

More WIPMichel Weststratebefore adoptionee1c977

line 84why, one step back
84

TSDXMichel Weststratebefore adoption4c4fecc

line 85why, one step back
85 // Find the last replaced index. Search from the end to optimize splice patches.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 86–87why, one step back
86 let end = base_.length
87 while (end > start && base_[end - 1] === copy_[end + delta - 1]) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 88–89why, one step back
88 --end
89 }

More WIPMichel Weststratebefore adoptionee1c977

line 90why, one step back
90

TSDXMichel Weststratebefore adoption4c4fecc

lines 91–92why, one step back
91 // Process replaced indices.
92 for (let i = start; i < end; ++i) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 93why, one step back
93 if (assigned_[i] && copy_[i] !== base_[i]) {

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 97why, one step back
97 path,

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 98why, one step back
98 value: copy_[i]

TSDXMichel Weststratebefore adoption4c4fecc

line 99why, one step back
99 })

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 101–102why, one step back
101 op: "replace",
102 path,

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 103why, one step back
103 value: base_[i]

More WIPMichel Weststratebefore adoptionee1c977

lines 104–107why, one step back
104 })
105 }106 }107

TSDXMichel Weststratebefore adoption4c4fecc

lines 108–115why, one step back
108 const replaceCount = patches.length
109110 // Process added indices.111 for (let i = end + delta - 1; i >= end; --i) {112 const path = basePath.concat([i])113 patches[replaceCount + i - end] = {114 op: "add",115 path,

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 116why, one step back
116 value: copy_[i]

TSDXMichel Weststratebefore adoption4c4fecc

lines 117–120why, one step back
117 }
118 inversePatches.push({119 op: "remove",120 path

More WIPMichel Weststratebefore adoptionee1c977

lines 121–122why, one step back
121 })
122 }

TSDXMichel Weststratebefore adoption4c4fecc

line 123why, one step back
123 }

More WIPMichel Weststratebefore adoptionee1c977

line 124why, one step back
124

TSDXMichel Weststratebefore adoption4c4fecc

lines 125–131why, one step back
125 // This is used for both Map objects and normal objects.
126 function generatePatchesFromAssigned(127 state: MapState | ES5ObjectState | ProxyObjectState,128 basePath: PatchPath,129 patches: Patch[],130 inversePatches: Patch[]131 ) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 132–136why, one step back
132 const {base_, copy_} = state
133 each(state.assigned_!, (key, assignedValue) => {134 const origValue = get(base_, key)135 const value = get(copy_!, key)136 const op = !assignedValue ? "remove" : has(base_, key) ? "replace" : "add"

TSDXMichel Weststratebefore adoption4c4fecc

lines 137–148why, one step back
137 if (origValue === value && op === "replace") return
138 const path = basePath.concat(key as any)139 patches.push(op === "remove" ? {op, path} : {op, path, value})140 inversePatches.push(141 op === "add"142 ? {op: "remove", path}143 : op === "remove"144 ? {op: "add", path, value: origValue}145 : {op: "replace", path, value: origValue}146 )147 })148 }

More WIPMichel Weststratebefore adoptionee1c977

line 149why, one step back
149

TSDXMichel Weststratebefore adoption4c4fecc

lines 150–155why, one step back
150 function generateSetPatches(
151 state: SetState,152 basePath: PatchPath,153 patches: Patch[],154 inversePatches: Patch[]155 ) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 156why, one step back
156 let {base_, copy_} = state

More WIPMichel Weststratebefore adoptionee1c977

line 157why, one step back
157

TSDXMichel Weststratebefore adoption4c4fecc

line 158why, one step back
158 let i = 0

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 159why, one step back
159 base_.forEach((value: any) => {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 160why, one step back
160 if (!copy_!.has(value)) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 161–175why, one step back
161 const path = basePath.concat([i])
162 patches.push({163 op: "remove",164 path,165 value166 })167 inversePatches.unshift({168 op: "add",169 path,170 value171 })172 }173 i++174 })175 i = 0

Moving files into foldersMichel Weststratebefore adoptionab49edc

line 176why, one step back
176 copy_!.forEach((value: any) => {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 177why, one step back
177 if (!base_.has(value)) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 178–192why, one step back
178 const path = basePath.concat([i])
179 patches.push({180 op: "add",181 path,182 value183 })184 inversePatches.unshift({185 op: "remove",186 path,187 value188 })189 }190 i++191 })192 }

More WIPMichel Weststratebefore adoptionee1c977

line 193why, one step back
193

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 194why, one step back
194 function applyPatches_<T>(draft: T, patches: Patch[]): T {

TSDXMichel Weststratebefore adoption4c4fecc

lines 195–200why, one step back
195 patches.forEach(patch => {
196 const {path, op} = patch197198 let base: any = draft199 for (let i = 0; i < path.length - 1; i++) {200 base = get(base, path[i])

Preserve error codes in productionMichel Weststratebefore adoptiond005fab

line 201why, one step back
201 if (typeof base !== "object") die(15, path.join("/"))

TSDXMichel Weststratebefore adoption4c4fecc

line 202why, one step back
202 }

More WIPMichel Weststratebefore adoptionee1c977

line 203why, one step back
203

TSDXMichel Weststratebefore adoption4c4fecc

lines 204–209why, one step back
204 const type = getArchtype(base)
205 const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #411206 const key = path[path.length - 1]207 switch (op) {208 case "replace":209 switch (type) {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 210why, one step back
210 case ArchtypeMap:

TSDXMichel Weststratebefore adoption4c4fecc

lines 211–212why, one step back
211 return base.set(key, value)
212 /* istanbul ignore next */

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 213why, one step back
213 case ArchtypeSet:

Preserve error codes in productionMichel Weststratebefore adoptiond005fab

line 214why, one step back
214 die(16)

TSDXMichel Weststratebefore adoption4c4fecc

lines 215–223why, one step back
215 default:
216 // if value is an object, then it's assigned by reference217 // in the following add or remove ops, the value field inside the patch will also be modifyed218 // so we use value from the cloned patch219 // @ts-ignore220 return (base[key] = value)221 }222 case "add":223 switch (type) {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 224why, one step back
224 case ArchtypeArray:

TSDXMichel Weststratebefore adoption4c4fecc

line 225why, one step back
225 return base.splice(key as any, 0, value)

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 226why, one step back
226 case ArchtypeMap:

TSDXMichel Weststratebefore adoption4c4fecc

line 227why, one step back
227 return base.set(key, value)

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 228why, one step back
228 case ArchtypeSet:

TSDXMichel Weststratebefore adoption4c4fecc

lines 229–234why, one step back
229 return base.add(value)
230 default:231 return (base[key] = value)232 }233 case "remove":234 switch (type) {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 235why, one step back
235 case ArchtypeArray:

TSDXMichel Weststratebefore adoption4c4fecc

line 236why, one step back
236 return base.splice(key as any, 1)

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 237why, one step back
237 case ArchtypeMap:

TSDXMichel Weststratebefore adoption4c4fecc

line 238why, one step back
238 return base.delete(key)

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 239why, one step back
239 case ArchtypeSet:

TSDXMichel Weststratebefore adoption4c4fecc

lines 240–244why, one step back
240 return base.delete(patch.value)
241 default:242 return delete base[key]243 }244 default:

Preserve error codes in productionMichel Weststratebefore adoptiond005fab

line 245why, one step back
245 die(17, op)

TSDXMichel Weststratebefore adoption4c4fecc

lines 246–247why, one step back
246 }
247 })

More WIPMichel Weststratebefore adoptionee1c977

line 248why, one step back
248

TSDXMichel Weststratebefore adoption4c4fecc

lines 249–250why, one step back
249 return draft
250 }

More WIPMichel Weststratebefore adoptionee1c977

line 251why, one step back
251

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 267why, one step back
267 }

TSDXMichel Weststratebefore adoption4c4fecc

line 268why, one step back
268

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 269why, one step back
269 loadPlugin("patches", {applyPatches_, generatePatches_})

TSDXMichel Weststratebefore adoption4c4fecc

line 270why, one step back
270}