Skip to content
Myr

finalize.ts

This file

Path

src/finalize.ts

203 lines · 5.2 KiB · TypeScript

Last changed by

CleanupMichel Weststratebefore adoption28ce953

History of this file

Take it

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 1why, one step back
1import {Immer} from "./immer"

Enums FTWMichel Weststratebefore adoptionfd76f1c

line 2why, one step back
2import {ImmerState, Drafted, ProxyType} from "./types"

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 3–12why, one step back
3import {ImmerScope} from "./scope"
4import {5 isSet,6 has,7 is,8 get,9 each,10 DRAFT_STATE,11 NOTHING,12 freeze,

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 13–14why, one step back
13 shallowCopy,
14 set

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 15–17why, one step back
15} from "./common"
16import {isDraft, isDraftable} from "./index"17import {SetState} from "./set"

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 18why, one step back
18import {generatePatches, PatchPath} from "./patches"

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 19–30why, one step back
19
20export function processResult(immer: Immer, result: any, scope: ImmerScope) {21 const baseDraft = scope.drafts![0]22 const isReplaced = result !== undefined && result !== baseDraft23 immer.willFinalize(scope, result, isReplaced)24 if (isReplaced) {25 if (baseDraft[DRAFT_STATE].modified) {26 scope.revoke()27 throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore28 }29 if (isDraftable(result)) {30 // Finalize the result in case it contains (or is) a subset of the draft.

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 31why, one step back
31 result = finalize(immer, result, scope)

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 32–47why, one step back
32 maybeFreeze(immer, result)
33 }34 if (scope.patches) {35 scope.patches.push({36 op: "replace",37 path: [],38 value: result39 })40 scope.inversePatches!.push({41 op: "replace",42 path: [],43 value: baseDraft[DRAFT_STATE].base44 })45 }46 } else {47 // Finalize the base draft.

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 48why, one step back
48 result = finalize(immer, baseDraft, scope, [])

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 49–59why, one step back
49 }
50 scope.revoke()51 if (scope.patches) {52 scope.patchListener!(scope.patches, scope.inversePatches!)53 }54 return result !== NOTHING ? result : undefined55}5657function finalize(58 immer: Immer,59 draft: Drafted,

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 60–61why, one step back
60 scope: ImmerScope,
61 path?: PatchPath

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 62–65why, one step back
62) {
63 const state = draft[DRAFT_STATE]64 if (!state) {65 if (Object.isFrozen(draft)) return draft

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 66why, one step back
66 return finalizeTree(immer, draft, scope)

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 67–77why, one step back
67 }
68 // Never finalize drafts owned by another scope.69 if (state.scope !== scope) {70 return draft71 }72 if (!state.modified) {73 maybeFreeze(immer, state.base, true)74 return state.base75 }76 if (!state.finalized) {77 state.finalized = true

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 78why, one step back
78 finalizeTree(immer, state.draft, scope, path)

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 79–114why, one step back
79
80 // We cannot really delete anything inside of a Set. We can only replace the whole Set.81 if (immer.onDelete && !isSet(state.base)) {82 // The `assigned` object is unreliable with ES5 drafts.83 if (immer.useProxies) {84 const {assigned} = state85 each(assigned, (prop, exists) => {86 if (!exists) immer.onDelete?.(state, prop as any)87 })88 } else {89 const {base, copy} = state90 each(base, prop => {91 if (!has(copy, prop)) immer.onDelete?.(state, prop as any)92 })93 }94 }95 if (immer.onCopy) {96 immer.onCopy(state)97 }9899 // At this point, all descendants of `state.copy` have been finalized,100 // so we can be sure that `scope.canAutoFreeze` is accurate.101 if (immer.autoFreeze && scope.canAutoFreeze) {102 freeze(state.copy, false)103 }104105 if (path && scope.patches) {106 generatePatches(state, path, scope.patches, scope.inversePatches!)107 }108 }109 return state.copy110}111112function finalizeTree(113 immer: Immer,114 root: Drafted,

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 115–116why, one step back
115 scope: ImmerScope,
116 rootPath?: PatchPath

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 117–119why, one step back
117) {
118 const state = root[DRAFT_STATE]119 if (state) {

Enums FTWMichel Weststratebefore adoptionfd76f1c

lines 120–123why, one step back
120 if (
121 state.type === ProxyType.ES5Object ||122 state.type === ProxyType.ES5Array123 ) {

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 124why, one step back
124 // Create the final copy, with added keys and without deleted keys.

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 125why, one step back
125 state.copy = shallowCopy(state.draft, true)

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 126–129why, one step back
126 }
127 root = state.copy128 }129 each(root, (key, value) =>

CleanupMichel Weststratebefore adoption28ce953

line 130why, one step back
130 finalizeProperty(immer, scope, root, state, root, key, value, rootPath)

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 131–137why, one step back
131 )
132 return root133}134135function finalizeProperty(136 immer: Immer,137 scope: ImmerScope,

CleanupMichel Weststratebefore adoption28ce953

lines 138–142why, one step back
138 root: Drafted,
139 rootState: ImmerState,140 parentValue: Drafted,141 prop: string | number,142 childValue: any,

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 143why, one step back
143 rootPath?: PatchPath

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 144why, one step back
144) {

CleanupMichel Weststratebefore adoption28ce953

line 145why, one step back
145 if (childValue === parentValue) {

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 146–149why, one step back
146 throw Error("Immer forbids circular references")
147 }148149 // In the `finalizeTree` method, only the `root` object may be a draft.

CleanupMichel Weststratebefore adoption28ce953

lines 150–151why, one step back
150 const isDraftProp = !!rootState && parentValue === root
151 const isSetMember = isSet(parentValue)

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 152why, one step back
152

CleanupMichel Weststratebefore adoption28ce953

line 153why, one step back
153 if (isDraft(childValue)) {

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 154why, one step back
154 const path =

CleanupMichel Weststratebefore adoption28ce953

line 155why, one step back
155 rootPath &&

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 156–157why, one step back
156 isDraftProp &&
157 !isSetMember && // Set objects are atomic since they have no keys.

CleanupMichel Weststratebefore adoption28ce953

line 158why, one step back
158 !has((rootState as Exclude<ImmerState, SetState>).assigned!, prop) // Skip deep patches for assigned keys.

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 159why, one step back
159 ? rootPath!.concat(prop)

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 160why, one step back
160 : undefined

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 161–162why, one step back
161
162 // Drafts owned by `scope` are finalized here.

CleanupMichel Weststratebefore adoption28ce953

lines 163–164why, one step back
163 childValue = finalize(immer, childValue, scope, path)
164 set(parentValue, prop, childValue)

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 165–166why, one step back
165
166 // Drafts from another scope must prevent auto-freezing.

CleanupMichel Weststratebefore adoption28ce953

line 167why, one step back
167 if (isDraft(childValue)) {

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 168–171why, one step back
168 scope.canAutoFreeze = false
169 }170 }171 // Unchanged draft properties are ignored.

CleanupMichel Weststratebefore adoption28ce953

line 172why, one step back
172 else if (isDraftProp && is(childValue, get(rootState.base, prop))) {

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 173–175why, one step back
173 return
174 }175 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.

CleanupMichel Weststratebefore adoption28ce953

lines 176–179why, one step back
176 // TODO: the recursion over here looks weird, shouldn't non-draft stuff have it's own recursion?
177 // especially the passing on of root and rootState doesn't make sense...178 else if (isDraftable(childValue) && !Object.isFrozen(childValue)) {179 each(childValue, (key, grandChild) =>

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 180–181why, one step back
180 finalizeProperty(
181 immer,

CleanupMichel Weststratebefore adoption28ce953

line 182why, one step back
182 scope,

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 183why, one step back
183 root,

CleanupMichel Weststratebefore adoption28ce953

lines 184–185why, one step back
184 rootState,
185 childValue,

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 186why, one step back
186 key,

CleanupMichel Weststratebefore adoption28ce953

line 187why, one step back
187 grandChild,

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 188–190why, one step back
188 rootPath
189 )190 )

CleanupMichel Weststratebefore adoption28ce953

line 191why, one step back
191 maybeFreeze(immer, childValue)

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 192–194why, one step back
192 }
193194 if (isDraftProp && immer.onAssign && !isSetMember) {

CleanupMichel Weststratebefore adoption28ce953

line 195why, one step back
195 immer.onAssign(rootState, prop, childValue)

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 196–203why, one step back
196 }
197}198199export function maybeFreeze(immer: Immer, value: any, deep = false) {200 if (immer.autoFreeze && !isDraft(value)) {201 freeze(value, deep)202 }203}