Skip to content
Myr

finalize.ts

This file

Path

src/core/finalize.ts

158 lines · 4.7 KiB · TypeScript

Last changed by

unclassed scopeMichel Weststratebefore adoption7a38af9

History of this file

Take it

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 1–20why, one step back
1import {
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,

unclassed scopeMichel Weststratebefore adoption7a38af9

lines 21–22why, one step back
21 die,
22 revokeScope

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 23–29why, one step back
23} 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_)

Tests for pluginsMichel Weststratebefore adoptiondb16520

line 30why, one step back
30 getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced)

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 31–32why, one step back
31 if (isReplaced) {
32 if (baseDraft[DRAFT_STATE].modified_) {

unclassed scopeMichel Weststratebefore adoption7a38af9

line 33why, one step back
33 revokeScope(scope)

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 34–41why, one step back
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_) {

Processed some todo'sMichel Weststratebefore adoptioncb3b9d6

lines 42–47why, one step back
42 getPlugin("Patches").generateReplacementPatches_(
43 baseDraft[DRAFT_STATE],44 result,45 scope.patches_,46 scope.inversePatches_!47 )

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 48–52why, one step back
48 }
49 } else {50 // Finalize the base draft.51 result = finalize(scope, baseDraft, [])52 }

unclassed scopeMichel Weststratebefore adoption7a38af9

line 53why, one step back
53 revokeScope(scope)

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 54–95why, one step back
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 structures62 if (Object.isFrozen(value)) return value6364 const state: ImmerState = value[DRAFT_STATE]65 // A plain object, might need freezing, might contain drafts66 if (!state) {67 each(value, (key, childValue) =>68 finalizeProperty(rootScope, state, value, key, childValue, path)69 )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.85 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array86 ? (state.copy_ = shallowCopy(state.draft_, true))87 : state.copy_88 // finalize all children of the copy89 each(result as any, (key, childValue) =>90 finalizeProperty(rootScope, state, result, key, childValue, path)91 )92 // everything inside is frozen, we can freeze here93 maybeFreeze(rootScope, result, false)94 // first time finalizing, let's create those patches95 if (path && rootScope.patches_) {

Tests for pluginsMichel Weststratebefore adoptiondb16520

line 96why, one step back
96 getPlugin("Patches").generatePatches_(

Moving files into foldersMichel Weststratebefore adoptionab49edc

lines 97–158why, one step back
97 state,
98 path,99 rootScope.patches_,100 rootScope.inversePatches_!101 )102 }103 }104 return state.copy_105}106107function finalizeProperty(108 rootScope: ImmerScope,109 parentState: undefined | ImmerState,110 targetObject: any,111 prop: string | number,112 childValue: any,113 rootPath?: PatchPath114) {115 if (__DEV__ && childValue === targetObject) die(5)116 if (isDraft(childValue)) {117 const path =118 rootPath &&119 parentState &&120 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.121 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.122 ? rootPath!.concat(prop)123 : undefined124 // Drafts owned by `scope` are finalized here.125 const res = finalize(rootScope, childValue, path)126 set(targetObject, prop, res)127 // Drafts from another scope must prevented to be frozen128 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze129 if (isDraft(res)) {130 rootScope.canAutoFreeze_ = false131 } else return132 }133 // Unchanged draft properties are ignored.134 if (parentState && is(childValue, get(parentState!.base_, prop))) {135 return136 }137 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.138 if (isDraftable(childValue)) {139 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {140 // optimization: if an object is not a draft, and we don't have to141 // deepfreeze everything, and we are sure that no drafts are left in the remaining object142 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.143 // This benefits especially adding large data tree's without further processing.144 // See add-data.js perf test145 return146 }147 finalize(rootScope, childValue)148 // immer deep freezes plain objects, so if there is no parent state, we freeze as well149 if (!parentState || !parentState.scope_.parent_)150 maybeFreeze(rootScope, childValue)151 }152}153154function maybeFreeze(scope: ImmerScope, value: any, deep = false) {155 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {156 freeze(value, deep)157 }158}