patches.ts
This file
Path
src/plugins/patches.ts
317 lines · 7.6 KiB · TypeScript
Last changed by
fix: Make `applyPatches` to accept `readonly Patch[]` (#1094)Mateusz Burzyńskibefore adoption4da2e0d
History of this fileTake it
The walk stopped after 200 commits: lines older than 6555173 point at it.
fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 1–13why, one step back1import {immerable} from "../immer"2import {3 ImmerState,4 Patch,5 SetState,6 ProxyArrayState,7 MapState,8 ProxyObjectState,9 PatchPath,10 get,11 each,12 has,13 getArchtype,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 14why, one step back14 getPrototypeOf,fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 15–17why, one step back15 isSet,16 isMap,17 loadPlugin,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 18why, one step back18 ArchType,fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 19–20why, one step back19 die,20 isDraft,fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f
line 21why, one step back21 isDraftable,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 22–23why, one step back22 NOTHING,23 errorsfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 24–26why, one step back24} from "../internal"2526export function enablePatches() {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 27–40why, one step back27 const errorOffset = 1628 if (process.env.NODE_ENV !== "production") {29 errors.push(30 'Sets cannot have "replace" patches.',31 function(op: string) {32 return "Unsupported patch operation: " + op33 },34 function(path: string) {35 return "Cannot apply patch, path doesn't resolve: " + path36 },37 "Patching reserved attributes like __proto__, prototype and constructor is not allowed"38 )39 }40fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 41–51why, one step back41 const REPLACE = "replace"42 const ADD = "add"43 const REMOVE = "remove"4445 function generatePatches_(46 state: ImmerState,47 basePath: PatchPath,48 patches: Patch[],49 inversePatches: Patch[]50 ): void {51 switch (state.type_) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 52–53why, one step back52 case ArchType.Object:53 case ArchType.Map:fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 54–59why, one step back54 return generatePatchesFromAssigned(55 state,56 basePath,57 patches,58 inversePatches59 )Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 60why, one step back60 case ArchType.Array:fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 61why, one step back61 return generateArrayPatches(state, basePath, patches, inversePatches)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 62why, one step back62 case ArchType.Set:fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 63–72why, one step back63 return generateSetPatches(64 (state as any) as SetState,65 basePath,66 patches,67 inversePatches68 )69 }70 }7172 function generateArrayPatches(Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 73why, one step back73 state: ProxyArrayState,fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 74–117why, one step back74 basePath: PatchPath,75 patches: Patch[],76 inversePatches: Patch[]77 ) {78 let {base_, assigned_} = state79 let copy_ = state.copy_!8081 // Reduce complexity by ensuring `base` is never longer.82 if (copy_.length < base_.length) {83 // @ts-ignore84 ;[base_, copy_] = [copy_, base_]85 ;[patches, inversePatches] = [inversePatches, patches]86 }8788 // Process replaced indices.89 for (let i = 0; i < base_.length; i++) {90 if (assigned_[i] && copy_[i] !== base_[i]) {91 const path = basePath.concat([i])92 patches.push({93 op: REPLACE,94 path,95 // Need to maybe clone it, as it can in fact be the original value96 // due to the base/copy inversion at the start of this function97 value: clonePatchValueIfNeeded(copy_[i])98 })99 inversePatches.push({100 op: REPLACE,101 path,102 value: clonePatchValueIfNeeded(base_[i])103 })104 }105 }106107 // Process added indices.108 for (let i = base_.length; i < copy_.length; i++) {109 const path = basePath.concat([i])110 patches.push({111 op: ADD,112 path,113 // Need to maybe clone it, as it can in fact be the original value114 // due to the base/copy inversion at the start of this function115 value: clonePatchValueIfNeeded(copy_[i])116 })117 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 118–119why, one step back118 for (let i = copy_.length - 1; base_.length <= i; --i) {119 const path = basePath.concat([i])fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 120why, one step back120 inversePatches.push({Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 121–122why, one step back121 op: REMOVE,122 pathfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 123–128why, one step back123 })124 }125 }126127 // This is used for both Map objects and normal objects.128 function generatePatchesFromAssigned(Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 129why, one step back129 state: MapState | ProxyObjectState,fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 130–196why, one step back130 basePath: PatchPath,131 patches: Patch[],132 inversePatches: Patch[]133 ) {134 const {base_, copy_} = state135 each(state.assigned_!, (key, assignedValue) => {136 const origValue = get(base_, key)137 const value = get(copy_!, key)138 const op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD139 if (origValue === value && op === REPLACE) return140 const path = basePath.concat(key as any)141 patches.push(op === REMOVE ? {op, path} : {op, path, value})142 inversePatches.push(143 op === ADD144 ? {op: REMOVE, path}145 : op === REMOVE146 ? {op: ADD, path, value: clonePatchValueIfNeeded(origValue)}147 : {op: REPLACE, path, value: clonePatchValueIfNeeded(origValue)}148 )149 })150 }151152 function generateSetPatches(153 state: SetState,154 basePath: PatchPath,155 patches: Patch[],156 inversePatches: Patch[]157 ) {158 let {base_, copy_} = state159160 let i = 0161 base_.forEach((value: any) => {162 if (!copy_!.has(value)) {163 const path = basePath.concat([i])164 patches.push({165 op: REMOVE,166 path,167 value168 })169 inversePatches.unshift({170 op: ADD,171 path,172 value173 })174 }175 i++176 })177 i = 0178 copy_!.forEach((value: any) => {179 if (!base_.has(value)) {180 const path = basePath.concat([i])181 patches.push({182 op: ADD,183 path,184 value185 })186 inversePatches.unshift({187 op: REMOVE,188 path,189 value190 })191 }192 i++193 })194 }195196 function generateReplacementPatches_(fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
line 197why, one step back197 baseValue: any,fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 198–204why, one step back198 replacement: any,199 patches: Patch[],200 inversePatches: Patch[]201 ): void {202 patches.push({203 op: REPLACE,204 path: [],fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f
line 205why, one step back205 value: replacement === NOTHING ? undefined : replacementfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 206–209why, one step back206 })207 inversePatches.push({208 op: REPLACE,209 path: [],fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
line 210why, one step back210 value: baseValuefix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 211–213why, one step back211 })212 }213fix: Make `applyPatches` to accept `readonly Patch[]` (#1094)Mateusz Burzyńskibefore adoption4da2e0d
line 214why, one step back214 function applyPatches_<T>(draft: T, patches: readonly Patch[]): T {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 215–220why, one step back215 patches.forEach(patch => {216 const {path, op} = patch217218 let base: any = draft219 for (let i = 0; i < path.length - 1; i++) {220 const parentType = getArchtype(base)fix: patching maps failed when using number keys (#1025)Marco Schumacherbefore adoptiondd83e2e
lines 221–225why, one step back221 let p = path[i]222 if (typeof p !== "string" && typeof p !== "number") {223 p = "" + p224 }225fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 226–227why, one step back226 // See #738, avoid prototype pollution227 if (Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 228why, one step back228 (parentType === ArchType.Object || parentType === ArchType.Array) &&fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 229–230why, one step back229 (p === "__proto__" || p === "constructor")230 )Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 231–233why, one step back231 die(errorOffset + 3)232 if (typeof base === "function" && p === "prototype")233 die(errorOffset + 3)fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 234why, one step back234 base = get(base, p)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 235why, one step back235 if (typeof base !== "object") die(errorOffset + 2, path.join("/"))fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 236–243why, one step back236 }237238 const type = getArchtype(base)239 const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #411240 const key = path[path.length - 1]241 switch (op) {242 case REPLACE:243 switch (type) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 244why, one step back244 case ArchType.Map:fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 245–246why, one step back245 return base.set(key, value)246 /* istanbul ignore next */Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 247–248why, one step back247 case ArchType.Set:248 die(errorOffset)fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 249–257why, one step back249 default:250 // if value is an object, then it's assigned by reference251 // in the following add or remove ops, the value field inside the patch will also be modifyed252 // so we use value from the cloned patch253 // @ts-ignore254 return (base[key] = value)255 }256 case ADD:257 switch (type) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 258why, one step back258 case ArchType.Array:fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
lines 259–261why, one step back259 return key === "-"260 ? base.push(value)261 : base.splice(key as any, 0, value)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 262why, one step back262 case ArchType.Map:fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 263why, one step back263 return base.set(key, value)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 264why, one step back264 case ArchType.Set:fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 265–270why, one step back265 return base.add(value)266 default:267 return (base[key] = value)268 }269 case REMOVE:270 switch (type) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 271why, one step back271 case ArchType.Array:fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 272why, one step back272 return base.splice(key as any, 1)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 273why, one step back273 case ArchType.Map:fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 274why, one step back274 return base.delete(key)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 275why, one step back275 case ArchType.Set:fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 276–280why, one step back276 return base.delete(patch.value)277 default:278 return delete base[key]279 }280 default:Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 281why, one step back281 die(errorOffset + 1, op)fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 282–299why, one step back282 }283 })284285 return draft286 }287288 // optimize: this is quite a performance hit, can we detect intelligently when it is needed?289 // E.g. auto-draft when new objects from outside are assigned and modified?290 // (See failing test when deepClone just returns obj)291 function deepClonePatchValue<T>(obj: T): T292 function deepClonePatchValue(obj: any) {293 if (!isDraftable(obj)) return obj294 if (Array.isArray(obj)) return obj.map(deepClonePatchValue)295 if (isMap(obj))296 return new Map(297 Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])298 )299 if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 300why, one step back300 const cloned = Object.create(getPrototypeOf(obj))fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 301–317why, one step back301 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])302 if (has(obj, immerable)) cloned[immerable] = obj[immerable]303 return cloned304 }305306 function clonePatchValueIfNeeded<T>(obj: T): T {307 if (isDraft(obj)) {308 return deepClonePatchValue(obj)309 } else return obj310 }311312 loadPlugin("Patches", {313 applyPatches_,314 generatePatches_,315 generateReplacementPatches_316 })317}