finalize.ts
This file
Path
src/finalize.ts
203 lines · 5.2 KiB · TypeScript
Last changed by
CleanupMichel Weststratebefore adoption28ce953
History of this fileTake it
Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 1why, one step back1import {Immer} from "./immer"Enums FTWMichel Weststratebefore adoptionfd76f1c
line 2why, one step back2import {ImmerState, Drafted, ProxyType} from "./types"Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 3–12why, one step back3import {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 back13 shallowCopy,14 setFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 15–17why, one step back15} from "./common"16import {isDraft, isDraftable} from "./index"17import {SetState} from "./set"TS type improvementsMichel Weststratebefore adoptiondd1808e
line 18why, one step back18import {generatePatches, PatchPath} from "./patches"Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 19–30why, one step back1920export 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 back31 result = finalize(immer, result, scope)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 32–47why, one step back32 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 back48 result = finalize(immer, baseDraft, scope, [])Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 49–59why, one step back49 }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 back60 scope: ImmerScope,61 path?: PatchPathFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 62–65why, one step back62) {63 const state = draft[DRAFT_STATE]64 if (!state) {65 if (Object.isFrozen(draft)) return draftTS type improvementsMichel Weststratebefore adoptiondd1808e
line 66why, one step back66 return finalizeTree(immer, draft, scope)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 67–77why, one step back67 }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 = trueTS type improvementsMichel Weststratebefore adoptiondd1808e
line 78why, one step back78 finalizeTree(immer, state.draft, scope, path)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 79–114why, one step back7980 // 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 back115 scope: ImmerScope,116 rootPath?: PatchPathFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 117–119why, one step back117) {118 const state = root[DRAFT_STATE]119 if (state) {Enums FTWMichel Weststratebefore adoptionfd76f1c
lines 120–123why, one step back120 if (121 state.type === ProxyType.ES5Object ||122 state.type === ProxyType.ES5Array123 ) {Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 124why, one step back124 // Create the final copy, with added keys and without deleted keys.TS type improvementsMichel Weststratebefore adoptiondd1808e
line 125why, one step back125 state.copy = shallowCopy(state.draft, true)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 126–129why, one step back126 }127 root = state.copy128 }129 each(root, (key, value) =>CleanupMichel Weststratebefore adoption28ce953
line 130why, one step back130 finalizeProperty(immer, scope, root, state, root, key, value, rootPath)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 131–137why, one step back131 )132 return root133}134135function finalizeProperty(136 immer: Immer,137 scope: ImmerScope,CleanupMichel Weststratebefore adoption28ce953
lines 138–142why, one step back138 root: Drafted,139 rootState: ImmerState,140 parentValue: Drafted,141 prop: string | number,142 childValue: any,TS type improvementsMichel Weststratebefore adoptiondd1808e
line 143why, one step back143 rootPath?: PatchPathFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 144why, one step back144) {CleanupMichel Weststratebefore adoption28ce953
line 145why, one step back145 if (childValue === parentValue) {Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 146–149why, one step back146 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 back150 const isDraftProp = !!rootState && parentValue === root151 const isSetMember = isSet(parentValue)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 152why, one step back152CleanupMichel Weststratebefore adoption28ce953
line 153why, one step back153 if (isDraft(childValue)) {Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 154why, one step back154 const path =CleanupMichel Weststratebefore adoption28ce953
line 155why, one step back155 rootPath &&Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 156–157why, one step back156 isDraftProp &&157 !isSetMember && // Set objects are atomic since they have no keys.CleanupMichel Weststratebefore adoption28ce953
line 158why, one step back158 !has((rootState as Exclude<ImmerState, SetState>).assigned!, prop) // Skip deep patches for assigned keys.Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 159why, one step back159 ? rootPath!.concat(prop)TS type improvementsMichel Weststratebefore adoptiondd1808e
line 160why, one step back160 : undefinedFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 161–162why, one step back161162 // Drafts owned by `scope` are finalized here.CleanupMichel Weststratebefore adoption28ce953
lines 163–164why, one step back163 childValue = finalize(immer, childValue, scope, path)164 set(parentValue, prop, childValue)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 165–166why, one step back165166 // Drafts from another scope must prevent auto-freezing.CleanupMichel Weststratebefore adoption28ce953
line 167why, one step back167 if (isDraft(childValue)) {Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 168–171why, one step back168 scope.canAutoFreeze = false169 }170 }171 // Unchanged draft properties are ignored.CleanupMichel Weststratebefore adoption28ce953
line 172why, one step back172 else if (isDraftProp && is(childValue, get(rootState.base, prop))) {Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 173–175why, one step back173 return174 }175 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.CleanupMichel Weststratebefore adoption28ce953
lines 176–179why, one step back176 // 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 back180 finalizeProperty(181 immer,CleanupMichel Weststratebefore adoption28ce953
line 182why, one step back182 scope,Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 183why, one step back183 root,CleanupMichel Weststratebefore adoption28ce953
lines 184–185why, one step back184 rootState,185 childValue,Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 186why, one step back186 key,CleanupMichel Weststratebefore adoption28ce953
line 187why, one step back187 grandChild,Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 188–190why, one step back188 rootPath189 )190 )CleanupMichel Weststratebefore adoption28ce953
line 191why, one step back191 maybeFreeze(immer, childValue)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 192–194why, one step back192 }193194 if (isDraftProp && immer.onAssign && !isSetMember) {CleanupMichel Weststratebefore adoption28ce953
line 195why, one step back195 immer.onAssign(rootState, prop, childValue)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 196–203why, one step back196 }197}198199export function maybeFreeze(immer: Immer, value: any, deep = false) {200 if (immer.autoFreeze && !isDraft(value)) {201 freeze(value, deep)202 }203}