finalize.ts
This file
Path
src/core/finalize.ts
162 lines · 4.8 KiB · TypeScript
Last changed by
Moving files into foldersMichel Weststratebefore adoptionab49edc
History of this fileTake it
Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 1–162why, one step back1import {2 ImmerScope,3 DRAFT_STATE,4 isDraftable,5 NOTHING,6 PatchPath,7 each,8 has,9 freeze,10 shallowCopy,11 ImmerState,12 isDraft,13 SetState,14 set,15 is,16 get,17 ProxyTypeES5Object,18 ProxyTypeES5Array,19 ProxyTypeSet,20 getPlugin,21 die22} from "../internal"2324export function processResult(result: any, scope: ImmerScope) {25 scope.unfinalizedDrafts_ = scope.drafts_.length26 const baseDraft = scope.drafts_![0]27 const isReplaced = result !== undefined && result !== baseDraft28 if (!scope.immer_.useProxies_)29 getPlugin("es5").willFinalizeES5_(scope, result, isReplaced)30 if (isReplaced) {31 if (baseDraft[DRAFT_STATE].modified_) {32 scope.revoke_()33 die(4)34 }35 if (isDraftable(result)) {36 // Finalize the result in case it contains (or is) a subset of the draft.37 result = finalize(scope, result)38 if (!scope.parent_) maybeFreeze(scope, result)39 }40 // TODO: move to plugin?41 if (scope.patches_) {42 scope.patches_.push({43 op: "replace",44 path: [],45 value: result46 })47 scope.inversePatches_!.push({48 op: "replace",49 path: [],50 value: (baseDraft[DRAFT_STATE] as ImmerState).base_51 })52 }53 } else {54 // Finalize the base draft.55 result = finalize(scope, baseDraft, [])56 }57 scope.revoke_()58 if (scope.patches_) {59 scope.patchListener_!(scope.patches_, scope.inversePatches_!)60 }61 return result !== NOTHING ? result : undefined62}6364function 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 drafts70 if (!state) {71 each(value, (key, childValue) =>72 finalizeProperty(rootScope, state, value, key, childValue, path)73 )74 return value75 }76 // Never finalize drafts owned by another scope.77 if (state.scope_ !== rootScope) return value78 // Unmodified draft, return the (frozen) original79 if (!state.modified_) {80 maybeFreeze(rootScope, state.base_, true)81 return state.base_82 }83 // Not finalized yet, let's do that now84 if (!state.finalized_) {85 state.finalized_ = true86 state.scope_.unfinalizedDrafts_--87 const result =88 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.89 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array90 ? (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_) {100 getPlugin("patches").generatePatches_(101 state,102 path,103 rootScope.patches_,104 rootScope.inversePatches_!105 )106 }107 }108 return state.copy_109}110111function finalizeProperty(112 rootScope: ImmerScope,113 parentState: undefined | ImmerState,114 targetObject: any,115 prop: string | number,116 childValue: any,117 rootPath?: PatchPath118) {119 if (__DEV__ && childValue === targetObject) die(5)120 if (isDraft(childValue)) {121 const path =122 rootPath &&123 parentState &&124 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.125 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.126 ? rootPath!.concat(prop)127 : undefined128 // Drafts owned by `scope` are finalized here.129 const res = finalize(rootScope, childValue, path)130 set(targetObject, prop, res)131 // 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 freeze133 if (isDraft(res)) {134 rootScope.canAutoFreeze_ = false135 } else return136 }137 // Unchanged draft properties are ignored.138 if (parentState && is(childValue, get(parentState!.base_, prop))) {139 return140 }141 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.142 if (isDraftable(childValue)) {143 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 }151 finalize(rootScope, childValue)152 // immer deep freezes plain objects, so if there is no parent state, we freeze as well153 if (!parentState || !parentState.scope_.parent_)154 maybeFreeze(rootScope, childValue)155 }156}157158function maybeFreeze(scope: ImmerScope, value: any, deep = false) {159 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {160 freeze(value, deep)161 }162}