finalize.ts
This file
Path
src/core/finalize.ts
156 lines · 4.7 KiB · TypeScript
Last changed by
Immer 7Michel Weststratebefore adoption467ea5d
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–18why, one step back1import {2 ImmerScope,3 DRAFT_STATE,4 isDraftable,5 NOTHING,6 PatchPath,7 each,8 has,9 freeze,10 ImmerState,11 isDraft,12 SetState,13 set,14 ProxyTypeES5Object,15 ProxyTypeES5Array,16 ProxyTypeSet,17 getPlugin,18 die,Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
line 19why, one step back19 revokeScope,Immer 7Michel Weststratebefore adoption467ea5d
lines 20–21why, one step back20 isFrozen,21 shallowCopyMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 22–60why, one step back22} 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 revokeScope(scope)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 if (scope.patches_) {41 getPlugin("Patches").generateReplacementPatches_(42 baseDraft[DRAFT_STATE],43 result,44 scope.patches_,45 scope.inversePatches_!46 )47 }48 } else {49 // Finalize the base draft.50 result = finalize(scope, baseDraft, [])51 }52 revokeScope(scope)53 if (scope.patches_) {54 scope.patchListener_!(scope.patches_, scope.inversePatches_!)55 }56 return result !== NOTHING ? result : undefined57}5859function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {60 // Don't recurse in tho recursive data structuresMerge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
line 61why, one step back61 if (isFrozen(value)) return valueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 62–65why, one step back6263 const state: ImmerState = value[DRAFT_STATE]64 // A plain object, might need freezing, might contain drafts65 if (!state) {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 66–70why, one step back66 each(67 value,68 (key, childValue) =>69 finalizeProperty(rootScope, state, value, key, childValue, path),70 true // See #590, don't recurse into non-enumarable of non drafted objectsMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 71–87why, one step back71 )72 return value73 }74 // Never finalize drafts owned by another scope.75 if (state.scope_ !== rootScope) return value76 // Unmodified draft, return the (frozen) original77 if (!state.modified_) {78 maybeFreeze(rootScope, state.base_, true)79 return state.base_80 }81 // Not finalized yet, let's do that now82 if (!state.finalized_) {83 state.finalized_ = true84 state.scope_.unfinalizedDrafts_--85 const result =86 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.87 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5ArrayImmer 7Michel Weststratebefore adoption467ea5d
line 88why, one step back88 ? (state.copy_ = shallowCopy(state.draft_))Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 89–135why, one step back89 : state.copy_90 // finalize all children of the copy91 each(result as any, (key, childValue) =>92 finalizeProperty(rootScope, state, result, key, childValue, path)93 )94 // everything inside is frozen, we can freeze here95 maybeFreeze(rootScope, result, false)96 // first time finalizing, let's create those patches97 if (path && rootScope.patches_) {98 getPlugin("Patches").generatePatches_(99 state,100 path,101 rootScope.patches_,102 rootScope.inversePatches_!103 )104 }105 }106 return state.copy_107}108109function finalizeProperty(110 rootScope: ImmerScope,111 parentState: undefined | ImmerState,112 targetObject: any,113 prop: string | number,114 childValue: any,115 rootPath?: PatchPath116) {117 if (__DEV__ && childValue === targetObject) die(5)118 if (isDraft(childValue)) {119 const path =120 rootPath &&121 parentState &&122 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.123 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.124 ? rootPath!.concat(prop)125 : undefined126 // Drafts owned by `scope` are finalized here.127 const res = finalize(rootScope, childValue, path)128 set(targetObject, prop, res)129 // Drafts from another scope must prevented to be frozen130 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze131 if (isDraft(res)) {132 rootScope.canAutoFreeze_ = false133 } else return134 }135 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.Immer 7Michel Weststratebefore adoption467ea5d
line 136why, one step back136 if (isDraftable(childValue) && !Object.isFrozen(childValue)) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 137–156why, one step back137 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {138 // optimization: if an object is not a draft, and we don't have to139 // deepfreeze everything, and we are sure that no drafts are left in the remaining object140 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.141 // This benefits especially adding large data tree's without further processing.142 // See add-data.js perf test143 return144 }145 finalize(rootScope, childValue)146 // immer deep freezes plain objects, so if there is no parent state, we freeze as well147 if (!parentState || !parentState.scope_.parent_)148 maybeFreeze(rootScope, childValue)149 }150}151152function maybeFreeze(scope: ImmerScope, value: any, deep = false) {153 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {154 freeze(value, deep)155 }156}