Skip to content
Myr

finalize.ts

This file

Path

src/finalize.ts

207 lines · 5.1 KiB · TypeScript

Last changed by

fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524Michel Weststratebefore adoptionacc5e54

History of this file

Take it

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 1why, one step back
1import {

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 2–3why, one step back
2 Immer,
3 ImmerScope,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 4why, one step back
4 DRAFT_STATE,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 5why, one step back
5 isDraftable,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 6why, one step back
6 NOTHING,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 7–11why, one step back
7 Drafted,
8 PatchPath,9 ProxyType,10 each,11 has,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 12why, one step back
12 freeze,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 13why, one step back
13 generatePatches,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 14why, one step back
14 shallowCopy,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 15–22why, one step back
15 ImmerState,
16 isSet,17 isDraft,18 SetState,19 set,20 is,21 get22} from "./internal"

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524Michel Weststratebefore adoptionacc5e54

line 36why, one step back
36 if (!scope.parent) maybeFreeze(immer, result)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 37–181why, one step back
37 }
38 if (scope.patches) {39 scope.patches.push({40 op: "replace",41 path: [],42 value: result43 })44 scope.inversePatches!.push({45 op: "replace",46 path: [],47 value: baseDraft[DRAFT_STATE].base48 })49 }50 } else {51 // Finalize the base draft.52 result = finalize(immer, baseDraft, scope, [])53 }54 scope.revoke()55 if (scope.patches) {56 scope.patchListener!(scope.patches, scope.inversePatches!)57 }58 return result !== NOTHING ? result : undefined59}6061function finalize(62 immer: Immer,63 draft: Drafted,64 scope: ImmerScope,65 path?: PatchPath66) {67 const state = draft[DRAFT_STATE]68 if (!state) {69 if (Object.isFrozen(draft)) return draft70 return finalizeTree(immer, draft, scope)71 }72 // Never finalize drafts owned by another scope.73 if (state.scope !== scope) {74 return draft75 }76 if (!state.modified) {77 maybeFreeze(immer, state.base, true)78 return state.base79 }80 if (!state.finalized) {81 state.finalized = true82 finalizeTree(immer, state.draft, scope, path)8384 // We cannot really delete anything inside of a Set. We can only replace the whole Set.85 if (immer.onDelete && state.type !== ProxyType.Set) {86 // The `assigned` object is unreliable with ES5 drafts.87 if (immer.useProxies) {88 const {assigned} = state89 each(assigned, (prop, exists) => {90 if (!exists) immer.onDelete!(state, prop as any)91 })92 } else {93 const {base, copy} = state94 each(base, prop => {95 if (!has(copy, prop)) immer.onDelete!(state, prop as any)96 })97 }98 }99 if (immer.onCopy) {100 immer.onCopy(state)101 }102103 // At this point, all descendants of `state.copy` have been finalized,104 // so we can be sure that `scope.canAutoFreeze` is accurate.105 if (immer.autoFreeze && scope.canAutoFreeze) {106 freeze(state.copy, false)107 }108109 if (path && scope.patches) {110 generatePatches(state, path, scope.patches, scope.inversePatches!)111 }112 }113 return state.copy114}115116function finalizeTree(117 immer: Immer,118 root: Drafted,119 scope: ImmerScope,120 rootPath?: PatchPath121) {122 const state = root[DRAFT_STATE]123 if (state) {124 if (125 state.type === ProxyType.ES5Object ||126 state.type === ProxyType.ES5Array127 ) {128 // Create the final copy, with added keys and without deleted keys.129 state.copy = shallowCopy(state.draft, true)130 }131 root = state.copy132 }133 each(root, (key, value) =>134 finalizeProperty(immer, scope, root, state, root, key, value, rootPath)135 )136 return root137}138139function finalizeProperty(140 immer: Immer,141 scope: ImmerScope,142 root: Drafted,143 rootState: ImmerState,144 parentValue: Drafted,145 prop: string | number,146 childValue: any,147 rootPath?: PatchPath148) {149 if (childValue === parentValue) {150 throw Error("Immer forbids circular references")151 }152153 // In the `finalizeTree` method, only the `root` object may be a draft.154 const isDraftProp = !!rootState && parentValue === root155 const isSetMember = isSet(parentValue)156157 if (isDraft(childValue)) {158 const path =159 rootPath &&160 isDraftProp &&161 !isSetMember && // Set objects are atomic since they have no keys.162 !has((rootState as Exclude<ImmerState, SetState>).assigned!, prop) // Skip deep patches for assigned keys.163 ? rootPath!.concat(prop)164 : undefined165166 // Drafts owned by `scope` are finalized here.167 childValue = finalize(immer, childValue, scope, path)168 set(parentValue, prop, childValue)169170 // Drafts from another scope must prevent auto-freezing.171 if (isDraft(childValue)) {172 scope.canAutoFreeze = false173 }174 }175 // Unchanged draft properties are ignored.176 else if (isDraftProp && is(childValue, get(rootState.base, prop))) {177 return178 }179 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.180 // TODO: the recursion over here looks weird, shouldn't non-draft stuff have it's own recursion?181 // especially the passing on of root and rootState doesn't make sense...

fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524Michel Weststratebefore adoptionacc5e54

line 182why, one step back
182 else if (isDraftable(childValue)) {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 183–194why, one step back
183 each(childValue, (key, grandChild) =>
184 finalizeProperty(185 immer,186 scope,187 root,188 rootState,189 childValue,190 key,191 grandChild,192 rootPath193 )194 )

fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524Michel Weststratebefore adoptionacc5e54

line 195why, one step back
195 if (!scope.parent) maybeFreeze(immer, childValue)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 196–207why, one step back
196 }
197198 if (isDraftProp && immer.onAssign && !isSetMember) {199 immer.onAssign(rootState, prop, childValue)200 }201}202203export function maybeFreeze(immer: Immer, value: any, deep = false) {204 if (immer.autoFreeze && !isDraft(value)) {205 freeze(value, deep)206 }207}