Skip to content
Myr

finalize.ts

This file

Path

src/core/finalize.ts

162 lines · 5.0 KiB · TypeScript

Last changed by

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

History of this file

Take it

The walk stopped after 200 commits: lines older than 4057f79 point at it.

Update question.mdMichel Weststratebefore adoption4057f79

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

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 14why, one step back
14 ArchType,

Update question.mdMichel Weststratebefore adoption4057f79

lines 15–17why, one step back
15 getPlugin,
16 die,17 revokeScope,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 18why, one step back
18 isFrozen

Update question.mdMichel Weststratebefore adoption4057f79

lines 19–36why, one step back
19} from "../internal"
2021export function processResult(result: any, scope: ImmerScope) {22 scope.unfinalizedDrafts_ = scope.drafts_.length23 const baseDraft = scope.drafts_![0]24 const isReplaced = result !== undefined && result !== baseDraft25 if (isReplaced) {26 if (baseDraft[DRAFT_STATE].modified_) {27 revokeScope(scope)28 die(4)29 }30 if (isDraftable(result)) {31 // Finalize the result in case it contains (or is) a subset of the draft.32 result = finalize(scope, result)33 if (!scope.parent_) maybeFreeze(scope, result)34 }35 if (scope.patches_) {36 getPlugin("Patches").generateReplacementPatches_(

fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270

line 37why, one step back
37 baseDraft[DRAFT_STATE].base_,

Update question.mdMichel Weststratebefore adoption4057f79

lines 38–64why, one step back
38 result,
39 scope.patches_,40 scope.inversePatches_!41 )42 }43 } else {44 // Finalize the base draft.45 result = finalize(scope, baseDraft, [])46 }47 revokeScope(scope)48 if (scope.patches_) {49 scope.patchListener_!(scope.patches_, scope.inversePatches_!)50 }51 return result !== NOTHING ? result : undefined52}5354function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {55 // Don't recurse in tho recursive data structures56 if (isFrozen(value)) return value5758 const state: ImmerState = value[DRAFT_STATE]59 // A plain object, might need freezing, might contain drafts60 if (!state) {61 each(62 value,63 (key, childValue) =>64 finalizeProperty(rootScope, state, value, key, childValue, path),

chore: typo (#857)Kim Chanbefore adoption26355d7

line 65why, one step back
65 true // See #590, don't recurse into non-enumerable of non drafted objects

Update question.mdMichel Weststratebefore adoption4057f79

lines 66–79why, one step back
66 )
67 return value68 }69 // Never finalize drafts owned by another scope.70 if (state.scope_ !== rootScope) return value71 // Unmodified draft, return the (frozen) original72 if (!state.modified_) {73 maybeFreeze(rootScope, state.base_, true)74 return state.base_75 }76 // Not finalized yet, let's do that now77 if (!state.finalized_) {78 state.finalized_ = true79 state.scope_.unfinalizedDrafts_--

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 80why, one step back
80 const result = state.copy_

fix: Set finalization can get stuck in a loop, fixes #628Michel Weststratebefore adoptionb12e5c9

lines 81–82why, one step back
81 // Finalize all children of the copy
82 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628

fix: Preserve insertion order of Sets, fixes #819 (#976)jdipbefore adoptionb3eeb69

lines 83–86why, one step back
83 // To preserve insertion order in all cases we then clear the set
84 // And we let finalizeProperty know it needs to re-add non-draft children back to the target85 let resultEach = result86 let isSet = false

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 87why, one step back
87 if (state.type_ === ArchType.Set) {

fix: Preserve insertion order of Sets, fixes #819 (#976)jdipbefore adoptionb3eeb69

lines 88–93why, one step back
88 resultEach = new Set(result)
89 result.clear()90 isSet = true91 }92 each(resultEach, (key, childValue) =>93 finalizeProperty(rootScope, state, result, key, childValue, path, isSet)

Update question.mdMichel Weststratebefore adoption4057f79

lines 94–115why, one step back
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,

fix: Preserve insertion order of Sets, fixes #819 (#976)jdipbefore adoptionb3eeb69

lines 116–117why, one step back
116 rootPath?: PatchPath,
117 targetIsSet?: boolean

Update question.mdMichel Weststratebefore adoption4057f79

line 118why, one step back
118) {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 119–120why, one step back
119 if (process.env.NODE_ENV !== "production" && childValue === targetObject)
120 die(5)

Update question.mdMichel Weststratebefore adoption4057f79

lines 121–124why, one step back
121 if (isDraft(childValue)) {
122 const path =123 rootPath &&124 parentState &&

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 125why, one step back
125 parentState!.type_ !== ArchType.Set && // Set objects are atomic since they have no keys.

Update question.mdMichel Weststratebefore adoption4057f79

lines 126–136why, one step back
126 !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 return

fix: Preserve insertion order of Sets, fixes #819 (#976)jdipbefore adoptionb3eeb69

lines 137–138why, one step back
137 } else if (targetIsSet) {
138 targetObject.add(childValue)

Update question.mdMichel Weststratebefore adoption4057f79

lines 139–140why, one step back
139 }
140 // 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 141why, one step back
141 if (isDraftable(childValue) && !isFrozen(childValue)) {

Update question.mdMichel Weststratebefore adoption4057f79

lines 142–157why, one step back
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) {

fix: produce results should never be frozen when returned from nested produces, to prevent 'hiding' drafts. Fixes #935Michel Weststratebefore adoptiona810960

lines 158–159why, one step back
158 // we never freeze for a non-root scope; as it would prevent pruning for drafts inside wrapping objects
159 if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {

Update question.mdMichel Weststratebefore adoption4057f79

lines 160–162why, one step back
160 freeze(value, deep)
161 }162}