Skip to content
Myr

finalize.ts

This file

Path

src/core/finalize.ts

168 lines · 5.2 KiB · TypeScript

Last changed by

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

History of this file

Take it

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

chore(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

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(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

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(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

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(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

lines 39–61why, 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 value5859 const state: ImmerState = value[DRAFT_STATE]60 // A plain object, might need freezing, might contain drafts61 if (!state) {

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

lines 62–63why, one step back
62 each(value, (key, childValue) =>
63 finalizeProperty(rootScope, state, value, key, childValue, path)

chore(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

lines 64–77why, one step back
64 )
65 return value66 }67 // Never finalize drafts owned by another scope.68 if (state.scope_ !== rootScope) return value69 // Unmodified draft, return the (frozen) original70 if (!state.modified_) {71 maybeFreeze(rootScope, state.base_, true)72 return state.base_73 }74 // Not finalized yet, let's do that now75 if (!state.finalized_) {76 state.finalized_ = true77 state.scope_.unfinalizedDrafts_--

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

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

chore(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

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

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

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

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

lines 86–91why, one step back
86 resultEach = new Set(result)
87 result.clear()88 isSet = true89 }90 each(resultEach, (key, childValue) =>91 finalizeProperty(rootScope, state, result, key, childValue, path, isSet)

chore(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

lines 92–113why, one step back
92 )
93 // everything inside is frozen, we can freeze here94 maybeFreeze(rootScope, result, false)95 // first time finalizing, let's create those patches96 if (path && rootScope.patches_) {97 getPlugin("Patches").generatePatches_(98 state,99 path,100 rootScope.patches_,101 rootScope.inversePatches_!102 )103 }104 }105 return state.copy_106}107108function finalizeProperty(109 rootScope: ImmerScope,110 parentState: undefined | ImmerState,111 targetObject: any,112 prop: string | number,113 childValue: any,

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

lines 114–115why, one step back
114 rootPath?: PatchPath,
115 targetIsSet?: boolean

chore(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

line 116why, one step back
116) {

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

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

chore(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

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

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

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

chore(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

lines 124–134why, one step back
124 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.
125 ? rootPath!.concat(prop)126 : undefined127 // Drafts owned by `scope` are finalized here.128 const res = finalize(rootScope, childValue, path)129 set(targetObject, prop, res)130 // Drafts from another scope must prevented to be frozen131 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze132 if (isDraft(res)) {133 rootScope.canAutoFreeze_ = false134 } else return

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

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

chore(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

lines 137–148why, one step back
137 }
138 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.139 if (isDraftable(childValue) && !isFrozen(childValue)) {140 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)

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

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

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

lines 155–157why, one step back
155 (isMap(targetObject)
156 ? targetObject.has(prop)157 : 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 158why, one step back
158 )

chore(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

lines 159–163why, one step back
159 maybeFreeze(rootScope, childValue)
160 }161}162163function 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 164–165why, one step back
164 // we never freeze for a non-root scope; as it would prevent pruning for drafts inside wrapping objects
165 if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {

chore(docs): Fix typo in pitfalls.md (#729)jfrantybefore adoptiond8121d6

lines 166–168why, one step back
166 freeze(value, deep)
167 }168}