Skip to content
Myr

finalize.ts

This file

Path

src/core/finalize.ts

161 lines · 5.0 KiB · TypeScript

Last changed by

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

History of this file

Take it

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 1–18why, 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,14 ProxyTypeES5Object,15 ProxyTypeES5Array,16 ProxyTypeSet,17 getPlugin,18 die,

Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de

line 19why, one step back
19 revokeScope,

Immer 7Michel Weststratebefore adoption467ea5d

lines 20–21why, one step back
20 isFrozen,
21 shallowCopy

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 22–60why, one step back
22} from "../internal"
2324export function processResult(result: any, scope: ImmerScope) {25 scope.unfinalizedDrafts_ = scope.drafts_.length26 const baseDraft = scope.drafts_![0]27 const isReplaced = result !== undefined && result !== baseDraft28 if (!scope.immer_.useProxies_)29 getPlugin("ES5").willFinalizeES5_(scope, result, isReplaced)30 if (isReplaced) {31 if (baseDraft[DRAFT_STATE].modified_) {32 revokeScope(scope)33 die(4)34 }35 if (isDraftable(result)) {36 // Finalize the result in case it contains (or is) a subset of the draft.37 result = finalize(scope, result)38 if (!scope.parent_) maybeFreeze(scope, result)39 }40 if (scope.patches_) {41 getPlugin("Patches").generateReplacementPatches_(42 baseDraft[DRAFT_STATE],43 result,44 scope.patches_,45 scope.inversePatches_!46 )47 }48 } else {49 // Finalize the base draft.50 result = finalize(scope, baseDraft, [])51 }52 revokeScope(scope)53 if (scope.patches_) {54 scope.patchListener_!(scope.patches_, scope.inversePatches_!)55 }56 return result !== NOTHING ? result : undefined57}5859function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {60 // Don't recurse in tho recursive data structures

Merge pull request #602 from immerjs/fix-600-isFrozenMichel Weststratebefore adoption8d4e0de

line 61why, one step back
61 if (isFrozen(value)) return value

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 66–70why, one step back
66 each(
67 value,68 (key, childValue) =>69 finalizeProperty(rootScope, state, value, key, childValue, path),70 true // See #590, don't recurse into non-enumarable of non drafted objects

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 71–87why, one step back
71 )
72 return value73 }74 // Never finalize drafts owned by another scope.75 if (state.scope_ !== rootScope) return value76 // Unmodified draft, return the (frozen) original77 if (!state.modified_) {78 maybeFreeze(rootScope, state.base_, true)79 return state.base_80 }81 // Not finalized yet, let's do that now82 if (!state.finalized_) {83 state.finalized_ = true84 state.scope_.unfinalizedDrafts_--85 const result =86 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.87 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array

Immer 7Michel Weststratebefore adoption467ea5d

line 88why, one step back
88 ? (state.copy_ = shallowCopy(state.draft_))

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 89why, one step back
89 : state.copy_

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

lines 90–97why, one step back
90 // Finalize all children of the copy
91 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #62892 // Although the original test case doesn't seem valid anyway, so if this in the way we can turn the next line93 // back to each(result, ....)94 each(95 state.type_ === ProxyTypeSet ? new Set(result) : result,96 (key, childValue) =>97 finalizeProperty(rootScope, state, result, key, childValue, path)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 98–140why, one step back
98 )
99 // everything inside is frozen, we can freeze here100 maybeFreeze(rootScope, result, false)101 // first time finalizing, let's create those patches102 if (path && rootScope.patches_) {103 getPlugin("Patches").generatePatches_(104 state,105 path,106 rootScope.patches_,107 rootScope.inversePatches_!108 )109 }110 }111 return state.copy_112}113114function finalizeProperty(115 rootScope: ImmerScope,116 parentState: undefined | ImmerState,117 targetObject: any,118 prop: string | number,119 childValue: any,120 rootPath?: PatchPath121) {122 if (__DEV__ && childValue === targetObject) die(5)123 if (isDraft(childValue)) {124 const path =125 rootPath &&126 parentState &&127 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.128 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.129 ? rootPath!.concat(prop)130 : undefined131 // Drafts owned by `scope` are finalized here.132 const res = finalize(rootScope, childValue, path)133 set(targetObject, prop, res)134 // Drafts from another scope must prevented to be frozen135 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze136 if (isDraft(res)) {137 rootScope.canAutoFreeze_ = false138 } else return139 }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)) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 142–161why, 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) {158 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {159 freeze(value, deep)160 }161}