finalize.ts
This file
Path
src/core/finalize.ts
165 lines · 5.1 KiB · TypeScript
Last changed by
fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
History of this fileTake it
The walk stopped after 200 commits: lines older than 02265f9 point at it.
docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 1–13why, one step back1import {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 back14 ArchType,docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 15–17why, one step back15 getPlugin,16 die,17 revokeScope,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 18why, one step back18 isFrozendocs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 19–36why, one step back19} 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 back37 baseDraft[DRAFT_STATE].base_,docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 38–60why, one step back38 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) {fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
lines 61–62why, one step back61 each(value, (key, childValue) =>62 finalizeProperty(rootScope, state, value, key, childValue, path)docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 63–76why, one step back63 )64 return value65 }66 // Never finalize drafts owned by another scope.67 if (state.scope_ !== rootScope) return value68 // Unmodified draft, return the (frozen) original69 if (!state.modified_) {70 maybeFreeze(rootScope, state.base_, true)71 return state.base_72 }73 // Not finalized yet, let's do that now74 if (!state.finalized_) {75 state.finalized_ = true76 state.scope_.unfinalizedDrafts_--Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 77why, one step back77 const result = state.copy_docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 78–79why, one step back78 // Finalize all children of the copy79 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628fix: Preserve insertion order of Sets, fixes #819 (#976)jdipbefore adoptionb3eeb69
lines 80–83why, one step back80 // To preserve insertion order in all cases we then clear the set81 // And we let finalizeProperty know it needs to re-add non-draft children back to the target82 let resultEach = result83 let isSet = falseMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 84why, one step back84 if (state.type_ === ArchType.Set) {fix: Preserve insertion order of Sets, fixes #819 (#976)jdipbefore adoptionb3eeb69
lines 85–90why, one step back85 resultEach = new Set(result)86 result.clear()87 isSet = true88 }89 each(resultEach, (key, childValue) =>90 finalizeProperty(rootScope, state, result, key, childValue, path, isSet)docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 91–112why, one step back91 )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_) {96 getPlugin("Patches").generatePatches_(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,fix: Preserve insertion order of Sets, fixes #819 (#976)jdipbefore adoptionb3eeb69
lines 113–114why, one step back113 rootPath?: PatchPath,114 targetIsSet?: booleandocs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
line 115why, one step back115) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 116–117why, one step back116 if (process.env.NODE_ENV !== "production" && childValue === targetObject)117 die(5)docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 118–121why, one step back118 if (isDraft(childValue)) {119 const path =120 rootPath &&121 parentState &&Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 122why, one step back122 parentState!.type_ !== ArchType.Set && // Set objects are atomic since they have no keys.docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 123–133why, one step back123 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.124 ? rootPath!.concat(prop)125 : undefined126 // Drafts owned by `scope` are finalized here.127 const res = finalize(rootScope, childValue, path)128 set(targetObject, prop, res)129 // Drafts from another scope must prevented to be frozen130 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze131 if (isDraft(res)) {132 rootScope.canAutoFreeze_ = false133 } else returnfix: Preserve insertion order of Sets, fixes #819 (#976)jdipbefore adoptionb3eeb69
lines 134–135why, one step back134 } else if (targetIsSet) {135 targetObject.add(childValue)docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 136–147why, one step back136 }137 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.138 if (isDraftable(childValue) && !isFrozen(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)fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
lines 148–155why, one step back148 // Immer deep freezes plain objects, so if there is no parent state, we freeze as well149 // Per #590, we never freeze symbolic properties. Just to make sure don't accidentally interfere150 // with other frameworks.151 if (152 (!parentState || !parentState.scope_.parent_) &&153 typeof prop !== "symbol" &&154 Object.prototype.propertyIsEnumerable.call(targetObject, prop)155 )docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 156–160why, one step back156 maybeFreeze(rootScope, childValue)157 }158}159160function 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 161–162why, one step back161 // we never freeze for a non-root scope; as it would prevent pruning for drafts inside wrapping objects162 if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {docs: Typo in performance.md (#671)Imad Shatalibefore adoption02265f9
lines 163–165why, one step back163 freeze(value, deep)164 }165}