finalize.ts
This file
Path
src/core/finalize.ts
159 lines · 5.0 KiB · TypeScript
Last changed by
chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–13why, 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,chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 14why, one step back14 ProxyType,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 15–16why, one step back15 getPlugin,16 die,Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
line 17why, one step back17 revokeScope,Immer 7Michel Weststratebefore adoption467ea5d
lines 18–19why, one step back18 isFrozen,19 shallowCopyMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 20–58why, one step back20} from "../internal"2122export function processResult(result: any, scope: ImmerScope) {23 scope.unfinalizedDrafts_ = scope.drafts_.length24 const baseDraft = scope.drafts_![0]25 const isReplaced = result !== undefined && result !== baseDraft26 if (!scope.immer_.useProxies_)27 getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced)28 if (isReplaced) {29 if (baseDraft[DRAFT_STATE].modified_) {30 revokeScope(scope)31 die(4)32 }33 if (isDraftable(result)) {34 // Finalize the result in case it contains (or is) a subset of the draft.35 result = finalize(scope, result)36 if (!scope.parent_) maybeFreeze(scope, result)37 }38 if (scope.patches_) {39 getPlugin("Patches").generateReplacementPatches_(40 baseDraft[DRAFT_STATE],41 result,42 scope.patches_,43 scope.inversePatches_!44 )45 }46 } else {47 // Finalize the base draft.48 result = finalize(scope, baseDraft, [])49 }50 revokeScope(scope)51 if (scope.patches_) {52 scope.patchListener_!(scope.patches_, scope.inversePatches_!)53 }54 return result !== NOTHING ? result : undefined55}5657function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {58 // Don't recurse in tho recursive data structuresMerge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de
line 59why, one step back59 if (isFrozen(value)) return valueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 60–63why, one step back6061 const state: ImmerState = value[DRAFT_STATE]62 // A plain object, might need freezing, might contain drafts63 if (!state) {fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0
lines 64–68why, one step back64 each(65 value,66 (key, childValue) =>67 finalizeProperty(rootScope, state, value, key, childValue, path),68 true // See #590, don't recurse into non-enumarable of non drafted objectsMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 69–84why, one step back69 )70 return value71 }72 // Never finalize drafts owned by another scope.73 if (state.scope_ !== rootScope) return value74 // Unmodified draft, return the (frozen) original75 if (!state.modified_) {76 maybeFreeze(rootScope, state.base_, true)77 return state.base_78 }79 // Not finalized yet, let's do that now80 if (!state.finalized_) {81 state.finalized_ = true82 state.scope_.unfinalizedDrafts_--83 const result =84 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 85why, one step back85 state.type_ === ProxyType.ES5Object || state.type_ === ProxyType.ES5ArrayImmer 7Michel Weststratebefore adoption467ea5d
line 86why, one step back86 ? (state.copy_ = shallowCopy(state.draft_))Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 87why, one step back87 : state.copy_fix: Set finalization can get stuck in a loop, fixes #628Michel Weststratebefore adoptionb12e5c9
lines 88–92why, one step back88 // Finalize all children of the copy89 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #62890 // Although the original test case doesn't seem valid anyway, so if this in the way we can turn the next line91 // back to each(result, ....)92 each(chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 93why, one step back93 state.type_ === ProxyType.Set ? new Set(result) : result,fix: Set finalization can get stuck in a loop, fixes #628Michel Weststratebefore adoptionb12e5c9
lines 94–95why, one step back94 (key, childValue) =>95 finalizeProperty(rootScope, state, result, key, childValue, path)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 96–124why, one step back96 )97 // everything inside is frozen, we can freeze here98 maybeFreeze(rootScope, result, false)99 // first time finalizing, let's create those patches100 if (path && rootScope.patches_) {101 getPlugin("Patches").generatePatches_(102 state,103 path,104 rootScope.patches_,105 rootScope.inversePatches_!106 )107 }108 }109 return state.copy_110}111112function finalizeProperty(113 rootScope: ImmerScope,114 parentState: undefined | ImmerState,115 targetObject: any,116 prop: string | number,117 childValue: any,118 rootPath?: PatchPath119) {120 if (__DEV__ && childValue === targetObject) die(5)121 if (isDraft(childValue)) {122 const path =123 rootPath &&124 parentState &&chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 125why, one step back125 parentState!.type_ !== ProxyType.Set && // Set objects are atomic since they have no keys.Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 126–138why, one step back126 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.127 ? rootPath!.concat(prop)128 : undefined129 // Drafts owned by `scope` are finalized here.130 const res = finalize(rootScope, childValue, path)131 set(targetObject, prop, res)132 // Drafts from another scope must prevented to be frozen133 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze134 if (isDraft(res)) {135 rootScope.canAutoFreeze_ = false136 } else return137 }138 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.fix: Fixed regression in Object.frozen for IE11. Fixes #600Michel Weststratebefore adoption6371d05
line 139why, one step back139 if (isDraftable(childValue) && !isFrozen(childValue)) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 140–159why, one step back140 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {141 // optimization: if an object is not a draft, and we don't have to142 // deepfreeze everything, and we are sure that no drafts are left in the remaining object143 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.144 // This benefits especially adding large data tree's without further processing.145 // See add-data.js perf test146 return147 }148 finalize(rootScope, childValue)149 // immer deep freezes plain objects, so if there is no parent state, we freeze as well150 if (!parentState || !parentState.scope_.parent_)151 maybeFreeze(rootScope, childValue)152 }153}154155function maybeFreeze(scope: ImmerScope, value: any, deep = false) {156 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {157 freeze(value, deep)158 }159}