finalize.ts
This file
Path
src/core/finalize.ts
161 lines · 4.8 KiB · TypeScript
Last changed by
Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–20why, 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 is,15 get,16 ProxyTypeES5Object,17 ProxyTypeES5Array,18 ProxyTypeSet,19 getPlugin,20 die,Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
lines 21–22why, one step back21 revokeScope,22 isFrozenMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 23–61why, one step back23} from "../internal"2425export function processResult(result: any, scope: ImmerScope) {26 scope.unfinalizedDrafts_ = scope.drafts_.length27 const baseDraft = scope.drafts_![0]28 const isReplaced = result !== undefined && result !== baseDraft29 if (!scope.immer_.useProxies_)30 getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced)31 if (isReplaced) {32 if (baseDraft[DRAFT_STATE].modified_) {33 revokeScope(scope)34 die(4)35 }36 if (isDraftable(result)) {37 // Finalize the result in case it contains (or is) a subset of the draft.38 result = finalize(scope, result)39 if (!scope.parent_) maybeFreeze(scope, result)40 }41 if (scope.patches_) {42 getPlugin("Patches").generateReplacementPatches_(43 baseDraft[DRAFT_STATE],44 result,45 scope.patches_,46 scope.inversePatches_!47 )48 }49 } else {50 // Finalize the base draft.51 result = finalize(scope, baseDraft, [])52 }53 revokeScope(scope)54 if (scope.patches_) {55 scope.patchListener_!(scope.patches_, scope.inversePatches_!)56 }57 return result !== NOTHING ? result : undefined58}5960function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {61 // Don't recurse in tho recursive data structuresMerge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
line 62why, one step back62 if (isFrozen(value)) return valueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 63–66why, one step back6364 const state: ImmerState = value[DRAFT_STATE]65 // A plain object, might need freezing, might contain drafts66 if (!state) {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 67–71why, one step back67 each(68 value,69 (key, childValue) =>70 finalizeProperty(rootScope, state, value, key, childValue, path),71 true // See #590, don't recurse into non-enumarable of non drafted objectsMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 72–88why, one step back72 )73 return value74 }75 // Never finalize drafts owned by another scope.76 if (state.scope_ !== rootScope) return value77 // Unmodified draft, return the (frozen) original78 if (!state.modified_) {79 maybeFreeze(rootScope, state.base_, true)80 return state.base_81 }82 // Not finalized yet, let's do that now83 if (!state.finalized_) {84 state.finalized_ = true85 state.scope_.unfinalizedDrafts_--86 const result =87 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.88 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5ArrayReimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71
line 89why, one step back89 ? (state.copy_ = getPlugin("ES5").createFinalCopy_(state))Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 90–161why, one step back90 : state.copy_91 // finalize all children of the copy92 each(result as any, (key, childValue) =>93 finalizeProperty(rootScope, state, result, key, childValue, path)94 )95 // everything inside is frozen, we can freeze here96 maybeFreeze(rootScope, result, false)97 // first time finalizing, let's create those patches98 if (path && rootScope.patches_) {99 getPlugin("Patches").generatePatches_(100 state,101 path,102 rootScope.patches_,103 rootScope.inversePatches_!104 )105 }106 }107 return state.copy_108}109110function finalizeProperty(111 rootScope: ImmerScope,112 parentState: undefined | ImmerState,113 targetObject: any,114 prop: string | number,115 childValue: any,116 rootPath?: PatchPath117) {118 if (__DEV__ && childValue === targetObject) die(5)119 if (isDraft(childValue)) {120 const path =121 rootPath &&122 parentState &&123 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.124 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.125 ? rootPath!.concat(prop)126 : undefined127 // Drafts owned by `scope` are finalized here.128 const res = finalize(rootScope, childValue, path)129 set(targetObject, prop, res)130 // Drafts from another scope must prevented to be frozen131 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze132 if (isDraft(res)) {133 rootScope.canAutoFreeze_ = false134 } else return135 }136 // Unchanged draft properties are ignored.137 if (parentState && is(childValue, get(parentState!.base_, prop))) {138 return139 }140 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.141 if (isDraftable(childValue)) {142 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {143 // optimization: if an object is not a draft, and we don't have to144 // deepfreeze everything, and we are sure that no drafts are left in the remaining object145 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.146 // This benefits especially adding large data tree's without further processing.147 // See add-data.js perf test148 return149 }150 finalize(rootScope, childValue)151 // immer deep freezes plain objects, so if there is no parent state, we freeze as well152 if (!parentState || !parentState.scope_.parent_)153 maybeFreeze(rootScope, childValue)154 }155}156157function maybeFreeze(scope: ImmerScope, value: any, deep = false) {158 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {159 freeze(value, deep)160 }161}