finalize.ts
This file
Path
src/core/finalize.ts
162 lines · 4.9 KiB · TypeScript
Last changed by
fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5
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,Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
lines 20–21why, one step back20 isFrozen,21 shallowCopyMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 22why, one step back22} from "../internal"fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5
line 23why, one step back23import {ProxyTypeProxyArray} from "../types/types-internal"Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 24–61why, one step back2425export 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_ === ProxyTypeES5ArraySome cleanupMichel Weststratebefore adoptionb3b43b9
line 89why, one step back89 ? (state.copy_ = shallowCopy(state.draft_))Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 90why, one step back90 : state.copy_fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5
lines 91–94why, one step back91 const originalCopy =92 state.type_ === ProxyTypeES5Array || state.type_ === ProxyTypeProxyArray93 ? result.slice()94 : undefinedMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 95–106why, one step back95 // finalize all children of the copy96 each(result as any, (key, childValue) =>97 finalizeProperty(rootScope, state, result, key, childValue, path)98 )99 // everything inside is frozen, we can freeze here100 maybeFreeze(rootScope, result, false)101 // first time finalizing, let's create those patches102 if (path && rootScope.patches_) {103 getPlugin("Patches").generatePatches_(104 state,105 path,106 rootScope.patches_,fix: Refactored patch array generation, fixes #468Michel Weststratebefore adoption9f75bf5
lines 107–108why, one step back107 rootScope.inversePatches_!,108 originalCopyMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 109–141why, one step back109 )110 }111 }112 return state.copy_113}114115function finalizeProperty(116 rootScope: ImmerScope,117 parentState: undefined | ImmerState,118 targetObject: any,119 prop: string | number,120 childValue: any,121 rootPath?: PatchPath122) {123 if (__DEV__ && childValue === targetObject) die(5)124 if (isDraft(childValue)) {125 const path =126 rootPath &&127 parentState &&128 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.129 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.130 ? rootPath!.concat(prop)131 : undefined132 // Drafts owned by `scope` are finalized here.133 const res = finalize(rootScope, childValue, path)134 set(targetObject, prop, res)135 // Drafts from another scope must prevented to be frozen136 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze137 if (isDraft(res)) {138 rootScope.canAutoFreeze_ = false139 } else return140 }141 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.Made ES5 version workMichel Weststratebefore adoption870f44e
line 142why, one step back142 if (isDraftable(childValue) && !Object.isFrozen(childValue)) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 143–162why, 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 }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}