finalize.ts
This file
Path
src/core/finalize.ts
162 lines · 4.8 KiB · TypeScript
Last changed by
Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–21why, 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 die,Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
lines 22–23why, one step back22 revokeScope,23 isFrozenMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 24–62why, one step back24} from "../internal"2526export function processResult(result: any, scope: ImmerScope) {27 scope.unfinalizedDrafts_ = scope.drafts_.length28 const baseDraft = scope.drafts_![0]29 const isReplaced = result !== undefined && result !== baseDraft30 if (!scope.immer_.useProxies_)31 getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced)32 if (isReplaced) {33 if (baseDraft[DRAFT_STATE].modified_) {34 revokeScope(scope)35 die(4)36 }37 if (isDraftable(result)) {38 // Finalize the result in case it contains (or is) a subset of the draft.39 result = finalize(scope, result)40 if (!scope.parent_) maybeFreeze(scope, result)41 }42 if (scope.patches_) {43 getPlugin("Patches").generateReplacementPatches_(44 baseDraft[DRAFT_STATE],45 result,46 scope.patches_,47 scope.inversePatches_!48 )49 }50 } else {51 // Finalize the base draft.52 result = finalize(scope, baseDraft, [])53 }54 revokeScope(scope)55 if (scope.patches_) {56 scope.patchListener_!(scope.patches_, scope.inversePatches_!)57 }58 return result !== NOTHING ? result : undefined59}6061function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {62 // Don't recurse in tho recursive data structuresMerge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
line 63why, one step back63 if (isFrozen(value)) return valueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 64–67why, one step back6465 const state: ImmerState = value[DRAFT_STATE]66 // A plain object, might need freezing, might contain drafts67 if (!state) {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 68–72why, one step back68 each(69 value,70 (key, childValue) =>71 finalizeProperty(rootScope, state, value, key, childValue, path),72 true // See #590, don't recurse into non-enumarable of non drafted objectsMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 73–162why, one step back73 )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}