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 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–17why, one step back12 get,13 each,14 has,15 getArchtype,16 isSet,17 isMap,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 18–26why, one step back18 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 back27 ArchtypeArray,28 dieMore WIPMichel Weststratebefore adoptionee1c977
lines 29–30why, one step back29} from "../internal"30TSDXMichel Weststratebefore adoption4c4fecc
line 31why, one step back31export function enablePatches() {scramble plugin names, -56Michel Weststratebefore adoption037b474
line 32why, one step back32 function generatePatches_(TSDXMichel Weststratebefore adoption4c4fecc
lines 33–37why, one step back33 state: ImmerState,34 basePath: PatchPath,35 patches: Patch[],36 inversePatches: Patch[]37 ): void {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 38why, one step back38 switch (state.type_) {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 39–41why, one step back39 case ProxyTypeProxyObject:40 case ProxyTypeES5Object:41 case ProxyTypeMap:TSDXMichel Weststratebefore adoption4c4fecc
lines 42–47why, one step back42 return generatePatchesFromAssigned(43 state,44 basePath,45 patches,46 inversePatches47 )Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 48–49why, one step back48 case ProxyTypeES5Array:49 case ProxyTypeProxyArray:TSDXMichel Weststratebefore adoption4c4fecc
line 50why, one step back50 return generateArrayPatches(state, basePath, patches, inversePatches)Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 51why, one step back51 case ProxyTypeSet:TSDXMichel Weststratebefore adoption4c4fecc
lines 52–57why, one step back52 return generateSetPatches(53 (state as any) as SetState,54 basePath,55 patches,56 inversePatches57 )More WIPMichel Weststratebefore adoptionee1c977
line 58why, one step back58 }TSDXMichel Weststratebefore adoption4c4fecc
line 59why, one step back59 }More WIPMichel Weststratebefore adoptionee1c977
line 60why, one step back60TSDXMichel Weststratebefore adoption4c4fecc
lines 61–66why, one step back61 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 back67 let {base_, assigned_} = state68 let copy_ = state.copy_!More WIPMichel Weststratebefore adoptionee1c977
line 69why, one step back69TSDXMichel Weststratebefore adoption4c4fecc
line 70why, one step back70 // Reduce complexity by ensuring `base` is never longer.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 71why, one step back71 if (copy_.length < base_.length) {TSDXMichel Weststratebefore adoption4c4fecc
line 72why, one step back72 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 73why, one step back73 ;[base_, copy_] = [copy_, base_]TSDXMichel Weststratebefore adoption4c4fecc
lines 74–75why, one step back74 ;[patches, inversePatches] = [inversePatches, patches]75 }More WIPMichel Weststratebefore adoptionee1c977
line 76why, one step back76Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 77why, one step back77 const delta = copy_.length - base_.lengthMore WIPMichel Weststratebefore adoptionee1c977
line 78why, one step back78TSDXMichel Weststratebefore adoption4c4fecc
lines 79–80why, one step back79 // Find the first replaced index.80 let start = 0Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 81why, one step back81 while (base_[start] === copy_[start] && start < base_.length) {TSDXMichel Weststratebefore adoption4c4fecc
lines 82–83why, one step back82 ++start83 }More WIPMichel Weststratebefore adoptionee1c977
line 84why, one step back84TSDXMichel Weststratebefore adoption4c4fecc
line 85why, one step back85 // Find the last replaced index. Search from the end to optimize splice patches.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 86–87why, one step back86 let end = base_.length87 while (end > start && base_[end - 1] === copy_[end + delta - 1]) {TSDXMichel Weststratebefore adoption4c4fecc
lines 88–89why, one step back88 --end89 }More WIPMichel Weststratebefore adoptionee1c977
line 90why, one step back90TSDXMichel Weststratebefore adoption4c4fecc
lines 91–92why, one step back91 // Process replaced indices.92 for (let i = start; i < end; ++i) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 93why, one step back93 if (assigned_[i] && copy_[i] !== base_[i]) {More WIPMichel Weststratebefore adoptionee1c977
line 94why, one step back94 const path = basePath.concat([i])TSDXMichel Weststratebefore adoption4c4fecc
lines 95–96why, one step back95 patches.push({96 op: "replace",More WIPMichel Weststratebefore adoptionee1c977
line 97why, one step back97 path,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 98why, one step back98 value: copy_[i]TSDXMichel Weststratebefore adoption4c4fecc
line 99why, one step back99 })More WIPMichel Weststratebefore adoptionee1c977
line 100why, one step back100 inversePatches.push({TSDXMichel Weststratebefore adoption4c4fecc
lines 101–102why, one step back101 op: "replace",102 path,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 103why, one step back103 value: base_[i]More WIPMichel Weststratebefore adoptionee1c977
lines 104–107why, one step back104 })105 }106 }107TSDXMichel Weststratebefore adoption4c4fecc
lines 108–115why, one step back108 const replaceCount = patches.length109110 // 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 back116 value: copy_[i]TSDXMichel Weststratebefore adoption4c4fecc
lines 117–120why, one step back117 }118 inversePatches.push({119 op: "remove",120 pathMore WIPMichel Weststratebefore adoptionee1c977
lines 121–122why, one step back121 })122 }TSDXMichel Weststratebefore adoption4c4fecc
line 123why, one step back123 }More WIPMichel Weststratebefore adoptionee1c977
line 124why, one step back124TSDXMichel Weststratebefore adoption4c4fecc
lines 125–131why, one step back125 // 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 back132 const {base_, copy_} = state133 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 back137 if (origValue === value && op === "replace") return138 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 back149TSDXMichel Weststratebefore adoption4c4fecc
lines 150–155why, one step back150 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 back156 let {base_, copy_} = stateMore WIPMichel Weststratebefore adoptionee1c977
line 157why, one step back157TSDXMichel Weststratebefore adoption4c4fecc
line 158why, one step back158 let i = 0Moving files into foldersMichel Weststratebefore adoptionab49edc
line 159why, one step back159 base_.forEach((value: any) => {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 160why, one step back160 if (!copy_!.has(value)) {TSDXMichel Weststratebefore adoption4c4fecc
lines 161–175why, one step back161 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 = 0Moving files into foldersMichel Weststratebefore adoptionab49edc
line 176why, one step back176 copy_!.forEach((value: any) => {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 177why, one step back177 if (!base_.has(value)) {TSDXMichel Weststratebefore adoption4c4fecc
lines 178–192why, one step back178 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 back193scramble plugin names, -56Michel Weststratebefore adoption037b474
line 194why, one step back194 function applyPatches_<T>(draft: T, patches: Patch[]): T {TSDXMichel Weststratebefore adoption4c4fecc
lines 195–200why, one step back195 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 back201 if (typeof base !== "object") die(15, path.join("/"))TSDXMichel Weststratebefore adoption4c4fecc
line 202why, one step back202 }More WIPMichel Weststratebefore adoptionee1c977
line 203why, one step back203TSDXMichel Weststratebefore adoption4c4fecc
lines 204–209why, one step back204 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 back210 case ArchtypeMap:TSDXMichel Weststratebefore adoption4c4fecc
lines 211–212why, one step back211 return base.set(key, value)212 /* istanbul ignore next */Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 213why, one step back213 case ArchtypeSet:Preserve error codes in productionMichel Weststratebefore adoptiond005fab
line 214why, one step back214 die(16)TSDXMichel Weststratebefore adoption4c4fecc
lines 215–223why, one step back215 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 back224 case ArchtypeArray:TSDXMichel Weststratebefore adoption4c4fecc
line 225why, one step back225 return base.splice(key as any, 0, value)Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 226why, one step back226 case ArchtypeMap:TSDXMichel Weststratebefore adoption4c4fecc
line 227why, one step back227 return base.set(key, value)Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 228why, one step back228 case ArchtypeSet:TSDXMichel Weststratebefore adoption4c4fecc
lines 229–234why, one step back229 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 back235 case ArchtypeArray:TSDXMichel Weststratebefore adoption4c4fecc
line 236why, one step back236 return base.splice(key as any, 1)Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 237why, one step back237 case ArchtypeMap:TSDXMichel Weststratebefore adoption4c4fecc
line 238why, one step back238 return base.delete(key)Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 239why, one step back239 case ArchtypeSet:TSDXMichel Weststratebefore adoption4c4fecc
lines 240–244why, one step back240 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 back245 die(17, op)TSDXMichel Weststratebefore adoption4c4fecc
lines 246–247why, one step back246 }247 })More WIPMichel Weststratebefore adoptionee1c977
line 248why, one step back248TSDXMichel Weststratebefore adoption4c4fecc
lines 249–250why, one step back249 return draft250 }More WIPMichel Weststratebefore adoptionee1c977
line 251why, one step back251TSDXMichel Weststratebefore adoption4c4fecc
lines 252–266why, one step back252 // 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 clonedMore WIPMichel Weststratebefore adoptionee1c977
line 267why, one step back267 }TSDXMichel Weststratebefore adoption4c4fecc
line 268why, one step back268scramble plugin names, -56Michel Weststratebefore adoption037b474
line 269why, one step back269 loadPlugin("patches", {applyPatches_, generatePatches_})TSDXMichel Weststratebefore adoption4c4fecc
line 270why, one step back270}