finalize.ts
This file
Path
src/core/finalize.ts
157 lines · 4.7 KiB · TypeScript
Last changed by
Processed some todo'sMichel Weststratebefore adoptioncb3b9d6
History of this fileTake it
Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 1–28why, 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 die22} 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_)Tests for pluginsMichel Weststratebefore adoptiondb16520
line 29why, one step back29 getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced)Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 30–40why, one step back30 if (isReplaced) {31 if (baseDraft[DRAFT_STATE].modified_) {32 scope.revoke_()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_) {Processed some todo'sMichel Weststratebefore adoptioncb3b9d6
lines 41–46why, one step back41 getPlugin("Patches").generateReplacementPatches_(42 baseDraft[DRAFT_STATE],43 result,44 scope.patches_,45 scope.inversePatches_!46 )Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 47–94why, one step back47 }48 } else {49 // Finalize the base draft.50 result = finalize(scope, baseDraft, [])51 }52 scope.revoke_()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 structures61 if (Object.isFrozen(value)) return value6263 const state: ImmerState = value[DRAFT_STATE]64 // A plain object, might need freezing, might contain drafts65 if (!state) {66 each(value, (key, childValue) =>67 finalizeProperty(rootScope, state, value, key, childValue, path)68 )69 return value70 }71 // Never finalize drafts owned by another scope.72 if (state.scope_ !== rootScope) return value73 // Unmodified draft, return the (frozen) original74 if (!state.modified_) {75 maybeFreeze(rootScope, state.base_, true)76 return state.base_77 }78 // Not finalized yet, let's do that now79 if (!state.finalized_) {80 state.finalized_ = true81 state.scope_.unfinalizedDrafts_--82 const result =83 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.84 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array85 ? (state.copy_ = shallowCopy(state.draft_, true))86 : state.copy_87 // finalize all children of the copy88 each(result as any, (key, childValue) =>89 finalizeProperty(rootScope, state, result, key, childValue, path)90 )91 // everything inside is frozen, we can freeze here92 maybeFreeze(rootScope, result, false)93 // first time finalizing, let's create those patches94 if (path && rootScope.patches_) {Tests for pluginsMichel Weststratebefore adoptiondb16520
line 95why, one step back95 getPlugin("Patches").generatePatches_(Moving files into foldersMichel Weststratebefore adoptionab49edc
lines 96–157why, one step back96 state,97 path,98 rootScope.patches_,99 rootScope.inversePatches_!100 )101 }102 }103 return state.copy_104}105106function finalizeProperty(107 rootScope: ImmerScope,108 parentState: undefined | ImmerState,109 targetObject: any,110 prop: string | number,111 childValue: any,112 rootPath?: PatchPath113) {114 if (__DEV__ && childValue === targetObject) die(5)115 if (isDraft(childValue)) {116 const path =117 rootPath &&118 parentState &&119 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.120 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.121 ? rootPath!.concat(prop)122 : undefined123 // Drafts owned by `scope` are finalized here.124 const res = finalize(rootScope, childValue, path)125 set(targetObject, prop, res)126 // Drafts from another scope must prevented to be frozen127 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze128 if (isDraft(res)) {129 rootScope.canAutoFreeze_ = false130 } else return131 }132 // Unchanged draft properties are ignored.133 if (parentState && is(childValue, get(parentState!.base_, prop))) {134 return135 }136 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.137 if (isDraftable(childValue)) {138 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {139 // optimization: if an object is not a draft, and we don't have to140 // deepfreeze everything, and we are sure that no drafts are left in the remaining object141 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.142 // This benefits especially adding large data tree's without further processing.143 // See add-data.js perf test144 return145 }146 finalize(rootScope, childValue)147 // immer deep freezes plain objects, so if there is no parent state, we freeze as well148 if (!parentState || !parentState.scope_.parent_)149 maybeFreeze(rootScope, childValue)150 }151}152153function maybeFreeze(scope: ImmerScope, value: any, deep = false) {154 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {155 freeze(value, deep)156 }157}