Skip to content
Myr

finalize.ts

This file

Path

src/core/finalize.ts

205 lines · 5.7 KiB · TypeScript

Last changed by

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

History of this file

Take it

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

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

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,

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

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

Fix: freezing of Map values during finalization (#1150)Chetan Satputebefore adoptionb4f71a6

lines 18–19why, one step back
18 isFrozen,
19 isMap

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 20–37why, one step back
20} 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 (isReplaced) {27 if (baseDraft[DRAFT_STATE].modified_) {28 revokeScope(scope)29 die(4)30 }31 if (isDraftable(result)) {32 // Finalize the result in case it contains (or is) a subset of the draft.33 result = finalize(scope, result)34 if (!scope.parent_) maybeFreeze(scope, result)35 }36 if (scope.patches_) {37 getPlugin("Patches").generateReplacementPatches_(

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

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

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 39–58why, one step back
39 result,
40 scope.patches_,41 scope.inversePatches_!42 )43 }44 } else {45 // Finalize the base draft.46 result = finalize(scope, baseDraft, [])47 }48 revokeScope(scope)49 if (scope.patches_) {50 scope.patchListener_!(scope.patches_, scope.inversePatches_!)51 }52 return result !== NOTHING ? result : undefined53}5455function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {56 // Don't recurse in tho recursive data structures57 if (isFrozen(value)) return value58

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

lines 59–60why, one step back
59 const useStrictIteration = rootScope.immer_.shouldUseStrictIteration()
60

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 61–63why, one step back
61 const state: ImmerState = value[DRAFT_STATE]
62 // A plain object, might need freezing, might contain drafts63 if (!state) {

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

lines 64–68why, one step back
64 each(
65 value,66 (key, childValue) =>67 finalizeProperty(rootScope, state, value, key, childValue, path),68 useStrictIteration

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 69–82why, one step back
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_--

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

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

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 84–85why, one step back
84 // Finalize all children of the copy
85 // 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 86–89why, one step back
86 // To preserve insertion order in all cases we then clear the set
87 // And we let finalizeProperty know it needs to re-add non-draft children back to the target88 let resultEach = result89 let isSet = false

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

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

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

lines 91–94why, one step back
91 resultEach = new Set(result)
92 result.clear()93 isSet = true94 }

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

lines 95–107why, one step back
95 each(
96 resultEach,97 (key, childValue) =>98 finalizeProperty(99 rootScope,100 state,101 result,102 key,103 childValue,104 path,105 isSet106 ),107 useStrictIteration

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 108–129why, one step back
108 )
109 // everything inside is frozen, we can freeze here110 maybeFreeze(rootScope, result, false)111 // first time finalizing, let's create those patches112 if (path && rootScope.patches_) {113 getPlugin("Patches").generatePatches_(114 state,115 path,116 rootScope.patches_,117 rootScope.inversePatches_!118 )119 }120 }121 return state.copy_122}123124function finalizeProperty(125 rootScope: ImmerScope,126 parentState: undefined | ImmerState,127 targetObject: any,128 prop: string | number,129 childValue: any,

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

lines 130–131why, one step back
130 rootPath?: PatchPath,
131 targetIsSet?: boolean

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

line 132why, one step back
132) {

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

lines 133–144why, one step back
133 if (childValue == null) {
134 return135 }136137 if (typeof childValue !== "object" && !targetIsSet) {138 return139 }140 const childIsFrozen = isFrozen(childValue)141 if (childIsFrozen && !targetIsSet) {142 return143 }144

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

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

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 147–150why, one step back
147 if (isDraft(childValue)) {
148 const path =149 rootPath &&150 parentState &&

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

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

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 152–162why, one step back
152 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.
153 ? rootPath!.concat(prop)154 : undefined155 // Drafts owned by `scope` are finalized here.156 const res = finalize(rootScope, childValue, path)157 set(targetObject, prop, res)158 // Drafts from another scope must prevented to be frozen159 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze160 if (isDraft(res)) {161 rootScope.canAutoFreeze_ = false162 } else return

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

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

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 165–166why, one step back
165 }
166 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

line 167why, one step back
167 if (isDraftable(childValue) && !childIsFrozen) {

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 168–173why, one step back
168 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
169 // optimization: if an object is not a draft, and we don't have to170 // deepfreeze everything, and we are sure that no drafts are left in the remaining object171 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.172 // This benefits especially adding large data tree's without further processing.173 // See add-data.js perf test

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

lines 174–182why, one step back
174 return
175 }176 if (177 parentState &&178 parentState.base_ &&179 parentState.base_[prop] === childValue &&180 childIsFrozen181 ) {182 // Object is unchanged from base - no need to process further

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 183–185why, one step back
183 return
184 }185 finalize(rootScope, childValue)

fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e

lines 186–191why, one step back
186 // Immer deep freezes plain objects, so if there is no parent state, we freeze as well
187 // Per #590, we never freeze symbolic properties. Just to make sure don't accidentally interfere188 // with other frameworks.189 if (190 (!parentState || !parentState.scope_.parent_) &&191 typeof prop !== "symbol" &&

Fix: freezing of Map values during finalization (#1150)Chetan Satputebefore adoptionb4f71a6

lines 192–194why, one step back
192 (isMap(targetObject)
193 ? targetObject.has(prop)194 : Object.prototype.propertyIsEnumerable.call(targetObject, prop))

fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e

line 195why, one step back
195 )

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 196–200why, one step back
196 maybeFreeze(rootScope, childValue)
197 }198}199200function 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 201–202why, one step back
201 // we never freeze for a non-root scope; as it would prevent pruning for drafts inside wrapping objects
202 if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {

chore: fix links, fixes #779Michel Weststratebefore adoptionbc35916

lines 203–205why, one step back
203 freeze(value, deep)
204 }205}