Skip to content
Myr

patches.ts

This file

Path

src/plugins/patches.ts

263 lines · 6.3 KiB · TypeScript

Last changed by

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

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

More WIPMichel Weststratebefore adoptionee1c977

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

More WIPMichel Weststratebefore adoptionee1c977

line 23why, one step back
23

TSDXMichel Weststratebefore adoption4c4fecc

lines 24–30why, one step back
24export function enablePatches() {
25 function generatePatches(26 state: ImmerState,27 basePath: PatchPath,28 patches: Patch[],29 inversePatches: Patch[]30 ): void {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 32–50why, one step back
32 case ProxyType.ProxyObject:
33 case ProxyType.ES5Object:34 case ProxyType.Map:35 return generatePatchesFromAssigned(36 state,37 basePath,38 patches,39 inversePatches40 )41 case ProxyType.ES5Array:42 case ProxyType.ProxyArray:43 return generateArrayPatches(state, basePath, patches, inversePatches)44 case ProxyType.Set:45 return generateSetPatches(46 (state as any) as SetState,47 basePath,48 patches,49 inversePatches50 )

More WIPMichel Weststratebefore adoptionee1c977

line 51why, one step back
51 }

TSDXMichel Weststratebefore adoption4c4fecc

line 52why, one step back
52 }

More WIPMichel Weststratebefore adoptionee1c977

line 53why, one step back
53

TSDXMichel Weststratebefore adoption4c4fecc

lines 54–59why, one step back
54 function generateArrayPatches(
55 state: ES5ArrayState | ProxyArrayState,56 basePath: PatchPath,57 patches: Patch[],58 inversePatches: Patch[]59 ) {

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

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

More WIPMichel Weststratebefore adoptionee1c977

line 62why, one step back
62

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 67–68why, one step back
67 ;[patches, inversePatches] = [inversePatches, patches]
68 }

More WIPMichel Weststratebefore adoptionee1c977

line 69why, one step back
69

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 71why, one step back
71

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 75–76why, one step back
75 ++start
76 }

More WIPMichel Weststratebefore adoptionee1c977

line 77why, one step back
77

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 81–82why, one step back
81 --end
82 }

More WIPMichel Weststratebefore adoptionee1c977

line 83why, one step back
83

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 90why, one step back
90 path,

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 92why, one step back
92 })

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 94–95why, one step back
94 op: "replace",
95 path,

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

lines 97–100why, one step back
97 })
98 }99 }100

TSDXMichel Weststratebefore adoption4c4fecc

lines 101–108why, one step back
101 const replaceCount = patches.length
102103 // Process added indices.104 for (let i = end + delta - 1; i >= end; --i) {105 const path = basePath.concat([i])106 patches[replaceCount + i - end] = {107 op: "add",108 path,

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 110–113why, one step back
110 }
111 inversePatches.push({112 op: "remove",113 path

More WIPMichel Weststratebefore adoptionee1c977

lines 114–115why, one step back
114 })
115 }

TSDXMichel Weststratebefore adoption4c4fecc

line 116why, one step back
116 }

More WIPMichel Weststratebefore adoptionee1c977

line 117why, one step back
117

TSDXMichel Weststratebefore adoption4c4fecc

lines 118–124why, one step back
118 // This is used for both Map objects and normal objects.
119 function generatePatchesFromAssigned(120 state: MapState | ES5ObjectState | ProxyObjectState,121 basePath: PatchPath,122 patches: Patch[],123 inversePatches: Patch[]124 ) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 125–129why, one step back
125 const {base_, copy_} = state
126 each(state.assigned_!, (key, assignedValue) => {127 const origValue = get(base_, key)128 const value = get(copy_!, key)129 const op = !assignedValue ? "remove" : has(base_, key) ? "replace" : "add"

TSDXMichel Weststratebefore adoption4c4fecc

lines 130–141why, one step back
130 if (origValue === value && op === "replace") return
131 const path = basePath.concat(key as any)132 patches.push(op === "remove" ? {op, path} : {op, path, value})133 inversePatches.push(134 op === "add"135 ? {op: "remove", path}136 : op === "remove"137 ? {op: "add", path, value: origValue}138 : {op: "replace", path, value: origValue}139 )140 })141 }

More WIPMichel Weststratebefore adoptionee1c977

line 142why, one step back
142

TSDXMichel Weststratebefore adoption4c4fecc

lines 143–148why, one step back
143 function generateSetPatches(
144 state: SetState,145 basePath: PatchPath,146 patches: Patch[],147 inversePatches: Patch[]148 ) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 150why, one step back
150

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 152–153why, one step back
152 base_.forEach(value => {
153 if (!copy_!.has(value)) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 154–168why, one step back
154 const path = basePath.concat([i])
155 patches.push({156 op: "remove",157 path,158 value159 })160 inversePatches.unshift({161 op: "add",162 path,163 value164 })165 }166 i++167 })168 i = 0

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 169–170why, one step back
169 copy_!.forEach(value => {
170 if (!base_.has(value)) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 171–185why, one step back
171 const path = basePath.concat([i])
172 patches.push({173 op: "add",174 path,175 value176 })177 inversePatches.unshift({178 op: "remove",179 path,180 value181 })182 }183 i++184 })185 }

More WIPMichel Weststratebefore adoptionee1c977

line 186why, one step back
186

TSDXMichel Weststratebefore adoption4c4fecc

lines 187–193why, one step back
187 function applyPatches<T>(draft: T, patches: Patch[]): T {
188 patches.forEach(patch => {189 const {path, op} = patch190191 let base: any = draft192 for (let i = 0; i < path.length - 1; i++) {193 base = get(base, path[i])

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

TSDXMichel Weststratebefore adoption4c4fecc

line 195why, one step back
195 }

More WIPMichel Weststratebefore adoptionee1c977

line 196why, one step back
196

TSDXMichel Weststratebefore adoption4c4fecc

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

TSDXMichel Weststratebefore adoption4c4fecc

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 239–240why, one step back
239 }
240 })

More WIPMichel Weststratebefore adoptionee1c977

line 241why, one step back
241

TSDXMichel Weststratebefore adoption4c4fecc

lines 242–243why, one step back
242 return draft
243 }

More WIPMichel Weststratebefore adoptionee1c977

line 244why, one step back
244

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 260why, one step back
260 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 261–263why, one step back
261
262 loadPlugin("patches", {applyPatches, generatePatches})263}