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 fileTake it
More WIPMichel Weststratebefore adoptionee1c977
lines 1–11why, one step back1// Import types only! TODO: force in TS 3.82import {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 back12 get,13 each,14 has,15 getArchtype,16 ProxyType,17 Archtype,18 isSet,19 isMap,20 loadPluginMore WIPMichel Weststratebefore adoptionee1c977
line 21why, one step back21} from "../internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 22why, one step back22import invariant from "tiny-invariant"More WIPMichel Weststratebefore adoptionee1c977
line 23why, one step back23TSDXMichel Weststratebefore adoption4c4fecc
lines 24–30why, one step back24export 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 back31 switch (state.type_) {TSDXMichel Weststratebefore adoption4c4fecc
lines 32–50why, one step back32 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 back51 }TSDXMichel Weststratebefore adoption4c4fecc
line 52why, one step back52 }More WIPMichel Weststratebefore adoptionee1c977
line 53why, one step back53TSDXMichel Weststratebefore adoption4c4fecc
lines 54–59why, one step back54 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 back60 let {base_, assigned_} = state61 let copy_ = state.copy_!More WIPMichel Weststratebefore adoptionee1c977
line 62why, one step back62TSDXMichel Weststratebefore adoption4c4fecc
line 63why, one step back63 // Reduce complexity by ensuring `base` is never longer.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 64why, one step back64 if (copy_.length < base_.length) {TSDXMichel Weststratebefore adoption4c4fecc
line 65why, one step back65 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 66why, one step back66 ;[base_, copy_] = [copy_, base_]TSDXMichel Weststratebefore adoption4c4fecc
lines 67–68why, one step back67 ;[patches, inversePatches] = [inversePatches, patches]68 }More WIPMichel Weststratebefore adoptionee1c977
line 69why, one step back69Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 70why, one step back70 const delta = copy_.length - base_.lengthMore WIPMichel Weststratebefore adoptionee1c977
line 71why, one step back71TSDXMichel Weststratebefore adoption4c4fecc
lines 72–73why, one step back72 // Find the first replaced index.73 let start = 0Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 74why, one step back74 while (base_[start] === copy_[start] && start < base_.length) {TSDXMichel Weststratebefore adoption4c4fecc
lines 75–76why, one step back75 ++start76 }More WIPMichel Weststratebefore adoptionee1c977
line 77why, one step back77TSDXMichel Weststratebefore adoption4c4fecc
line 78why, one step back78 // Find the last replaced index. Search from the end to optimize splice patches.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 79–80why, one step back79 let end = base_.length80 while (end > start && base_[end - 1] === copy_[end + delta - 1]) {TSDXMichel Weststratebefore adoption4c4fecc
lines 81–82why, one step back81 --end82 }More WIPMichel Weststratebefore adoptionee1c977
line 83why, one step back83TSDXMichel Weststratebefore adoption4c4fecc
lines 84–85why, one step back84 // Process replaced indices.85 for (let i = start; i < end; ++i) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 86why, one step back86 if (assigned_[i] && copy_[i] !== base_[i]) {More WIPMichel Weststratebefore adoptionee1c977
line 87why, one step back87 const path = basePath.concat([i])TSDXMichel Weststratebefore adoption4c4fecc
lines 88–89why, one step back88 patches.push({89 op: "replace",More WIPMichel Weststratebefore adoptionee1c977
line 90why, one step back90 path,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 91why, one step back91 value: copy_[i]TSDXMichel Weststratebefore adoption4c4fecc
line 92why, one step back92 })More WIPMichel Weststratebefore adoptionee1c977
line 93why, one step back93 inversePatches.push({TSDXMichel Weststratebefore adoption4c4fecc
lines 94–95why, one step back94 op: "replace",95 path,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 96why, one step back96 value: base_[i]More WIPMichel Weststratebefore adoptionee1c977
lines 97–100why, one step back97 })98 }99 }100TSDXMichel Weststratebefore adoption4c4fecc
lines 101–108why, one step back101 const replaceCount = patches.length102103 // 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 back109 value: copy_[i]TSDXMichel Weststratebefore adoption4c4fecc
lines 110–113why, one step back110 }111 inversePatches.push({112 op: "remove",113 pathMore WIPMichel Weststratebefore adoptionee1c977
lines 114–115why, one step back114 })115 }TSDXMichel Weststratebefore adoption4c4fecc
line 116why, one step back116 }More WIPMichel Weststratebefore adoptionee1c977
line 117why, one step back117TSDXMichel Weststratebefore adoption4c4fecc
lines 118–124why, one step back118 // 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 back125 const {base_, copy_} = state126 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 back130 if (origValue === value && op === "replace") return131 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 back142TSDXMichel Weststratebefore adoption4c4fecc
lines 143–148why, one step back143 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 back149 let {base_, copy_} = stateMore WIPMichel Weststratebefore adoptionee1c977
line 150why, one step back150TSDXMichel Weststratebefore adoption4c4fecc
line 151why, one step back151 let i = 0Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 152–153why, one step back152 base_.forEach(value => {153 if (!copy_!.has(value)) {TSDXMichel Weststratebefore adoption4c4fecc
lines 154–168why, one step back154 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 = 0Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 169–170why, one step back169 copy_!.forEach(value => {170 if (!base_.has(value)) {TSDXMichel Weststratebefore adoption4c4fecc
lines 171–185why, one step back171 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 back186TSDXMichel Weststratebefore adoption4c4fecc
lines 187–193why, one step back187 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 back194 invariant(typeof base === "object","Cannot apply patch, path doesn't resolve: " + path.join("/")) // prettier-ignoreTSDXMichel Weststratebefore adoption4c4fecc
line 195why, one step back195 }More WIPMichel Weststratebefore adoptionee1c977
line 196why, one step back196TSDXMichel Weststratebefore adoption4c4fecc
lines 197–206why, one step back197 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 back207 invariant(false, 'Sets cannot have "replace" patches.')TSDXMichel Weststratebefore adoption4c4fecc
lines 208–237why, one step back208 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 back238 invariant(false, "Unsupported patch operation: " + op)TSDXMichel Weststratebefore adoption4c4fecc
lines 239–240why, one step back239 }240 })More WIPMichel Weststratebefore adoptionee1c977
line 241why, one step back241TSDXMichel Weststratebefore adoption4c4fecc
lines 242–243why, one step back242 return draft243 }More WIPMichel Weststratebefore adoptionee1c977
line 244why, one step back244TSDXMichel Weststratebefore adoption4c4fecc
lines 245–259why, one step back245 // 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 clonedMore WIPMichel Weststratebefore adoptionee1c977
line 260why, one step back260 }TSDXMichel Weststratebefore adoption4c4fecc
lines 261–263why, one step back261262 loadPlugin("patches", {applyPatches, generatePatches})263}