finalize.ts
This file
Path
src/finalize.ts
162 lines · 5.0 KiB · TypeScript
Last changed by
Optimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8
History of this fileTake it
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 1why, one step back1import {fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 2why, one step back2 ImmerScope,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 3why, one step back3 DRAFT_STATE,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 4why, one step back4 isDraftable,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 5why, one step back5 NOTHING,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 6–8why, one step back6 PatchPath,7 each,8 has,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 9–10why, one step back9 freeze,10 shallowCopy,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 11–15why, one step back11 ImmerState,12 isDraft,13 SetState,14 set,15 is,Small code cleanupMichel Weststratebefore adoption9217809
line 16why, one step back16 get,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 17–18why, one step back17 ProxyTypeES5Object,18 ProxyTypeES5Array,scramble plugin names, -56Michel Weststratebefore adoption037b474
lines 19–20why, one step back19 ProxyTypeSet,20 getPluginfix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 21why, one step back21} from "./internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 22why, one step back22import invariant from "tiny-invariant"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 23why, one step back23Small improvementsMichel Weststratebefore adoption712af5c
line 24why, one step back24export function processResult(result: any, scope: ImmerScope) {Optimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8
line 25why, one step back25 scope.unfinalizedDrafts_ = scope.drafts_.lengthMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 26why, one step back26 const baseDraft = scope.drafts_![0]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 27why, one step back27 const isReplaced = result !== undefined && result !== baseDraftscramble plugin names, -56Michel Weststratebefore adoption037b474
lines 28–29why, one step back28 if (!scope.immer_.useProxies_)29 getPlugin("es5").willFinalizeES5_(scope, result, isReplaced)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 30why, one step back30 if (isReplaced) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 31–32why, one step back31 if (baseDraft[DRAFT_STATE].modified_) {32 scope.revoke_()use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 33why, one step back33 invariant(false, "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignorefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 34–36why, one step back34 }35 if (isDraftable(result)) {36 // Finalize the result in case it contains (or is) a subset of the draft.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 37why, one step back37 result = finalize(scope, result)Small improvementsMichel Weststratebefore adoption712af5c
line 38why, one step back38 if (!scope.parent_) maybeFreeze(scope, result)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 39why, one step back39 }scramble plugin names, -56Michel Weststratebefore adoption037b474
line 40why, one step back40 // TODO: move to plugin?Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 41–42why, one step back41 if (scope.patches_) {42 scope.patches_.push({feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 43–46why, one step back43 op: "replace",44 path: [],45 value: result46 })Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 47why, one step back47 scope.inversePatches_!.push({feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 48–49why, one step back48 op: "replace",49 path: [],Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 50why, one step back50 value: (baseDraft[DRAFT_STATE] as ImmerState).base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 51–54why, one step back51 })52 }53 } else {54 // Finalize the base draft.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 55why, one step back55 result = finalize(scope, baseDraft, [])feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 56why, one step back56 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 57–59why, one step back57 scope.revoke_()58 if (scope.patches_) {59 scope.patchListener_!(scope.patches_, scope.inversePatches_!)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 60–63why, one step back60 }61 return result !== NOTHING ? result : undefined62}63Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 64–69why, one step back64function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {65 // Don't recurse in tho recursive data structures66 if (Object.isFrozen(value)) return value6768 const state: ImmerState = value[DRAFT_STATE]69 // A plain object, might need freezing, might contain draftsfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 70why, one step back70 if (!state) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 71–74why, one step back71 each(value, (key, childValue) =>72 finalizeProperty(rootScope, state, value, key, childValue, path)73 )74 return valuefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 75–76why, one step back75 }76 // Never finalize drafts owned by another scope.Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 77–78why, one step back77 if (state.scope_ !== rootScope) return value78 // Unmodified draft, return the (frozen) originalMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 79why, one step back79 if (!state.modified_) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 80why, one step back80 maybeFreeze(rootScope, state.base_, true)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 81why, one step back81 return state.base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 82why, one step back82 }Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 83why, one step back83 // Not finalized yet, let's do that nowMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 84–85why, one step back84 if (!state.finalized_) {85 state.finalized_ = trueOptimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8
line 86why, one step back86 state.scope_.unfinalizedDrafts_--Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 87–88why, one step back87 const result =88 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 89why, one step back89 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5ArrayCleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 90–99why, one step back90 ? (state.copy_ = shallowCopy(state.draft_, true))91 : state.copy_92 // finalize all children of the copy93 each(result as any, (key, childValue) =>94 finalizeProperty(rootScope, state, result, key, childValue, path)95 )96 // everything inside is frozen, we can freeze here97 maybeFreeze(rootScope, result, false)98 // first time finalizing, let's create those patches99 if (path && rootScope.patches_) {scramble plugin names, -56Michel Weststratebefore adoption037b474
line 100why, one step back100 getPlugin("patches").generatePatches_(Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 101–105why, one step back101 state,102 path,103 rootScope.patches_,104 rootScope.inversePatches_!105 )feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 106–107why, one step back106 }107 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 108why, one step back108 return state.copy_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 109–111why, one step back109}110111function finalizeProperty(Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 112–114why, one step back112 rootScope: ImmerScope,113 parentState: undefined | ImmerState,114 targetObject: any,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 115–118why, one step back115 prop: string | number,116 childValue: any,117 rootPath?: PatchPath118) {Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 119why, one step back119 invariant(childValue !== targetObject, "Immer forbids circular references")feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 120–122why, one step back120 if (isDraft(childValue)) {121 const path =122 rootPath &&Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 123why, one step back123 parentState &&Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 124why, one step back124 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 125why, one step back125 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 126–128why, one step back126 ? rootPath!.concat(prop)127 : undefined128 // Drafts owned by `scope` are finalized here.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 129–130why, one step back129 const res = finalize(rootScope, childValue, path)130 set(targetObject, prop, res)Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
lines 131–132why, one step back131 // Drafts from another scope must prevented to be frozen132 // if we got a draft back from finalize, we're in a nested produce and shouldn't freezeInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 133–134why, one step back133 if (isDraft(res)) {134 rootScope.canAutoFreeze_ = falseCleanup finalize, -57Michel Weststratebefore adoption530d81b
line 135why, one step back135 } else returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 136–137why, one step back136 }137 // Unchanged draft properties are ignored.Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 138why, one step back138 if (parentState && is(childValue, get(parentState!.base_, prop))) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 139–141why, one step back139 return140 }141 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 142why, one step back142 if (isDraftable(childValue)) {Optimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8
lines 143–150why, one step back143 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {144 // optimization: if an object is not a draft, and we don't have to145 // deepfreeze everything, and we are sure that no drafts are left in the remaining object146 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.147 // This benefits especially adding large data tree's without further processing.148 // See add-data.js perf test149 return150 }Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 151why, one step back151 finalize(rootScope, childValue)Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 152why, one step back152 // immer deep freezes plain objects, so if there is no parent state, we freeze as wellInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 153–154why, one step back153 if (!parentState || !parentState.scope_.parent_)154 maybeFreeze(rootScope, childValue)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 155–157why, one step back155 }156}157Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 158why, one step back158function maybeFreeze(scope: ImmerScope, value: any, deep = false) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 159why, one step back159 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 160–162why, one step back160 freeze(value, deep)161 }162}