finalize.ts
This file
Path
src/finalize.ts
215 lines · 5.2 KiB · TypeScript
Last changed by
Several refactoringsMichel Weststratebefore adoptionc7f8f5b
History of this fileTake it
Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 1–12why, one step back1import {Immer} from "./immer"2import {ImmerState, Drafted} from "./types"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 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) {TS type improvementsMichel Weststratebefore adoptiondd1808e
line 120why, one step back120 if (state.type === "es5_object" || state.type === "es5_array") {Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 121why, one step back121 // Create the final copy, with added keys and without deleted keys.TS type improvementsMichel Weststratebefore adoptiondd1808e
line 122why, one step back122 state.copy = shallowCopy(state.draft, true)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 123–149why, one step back123 }124 root = state.copy125 }126 const needPatches = !!rootPath && !!scope.patches127 each(root, (key, value) =>128 finalizeProperty(129 immer,130 root,131 state,132 key,133 value,134 root,135 needPatches,136 scope,137 rootPath138 )139 )140 return root141}142143function finalizeProperty(144 // TODO: can do with less args?145 immer: Immer,146 root: ImmerState,147 state: ImmerState,148 prop: string | number,149 value: any,TS type improvementsMichel Weststratebefore adoptiondd1808e
line 150why, one step back150 parent: Drafted,Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 151–152why, one step back151 needPatches: boolean,152 scope: ImmerScope,TS type improvementsMichel Weststratebefore adoptiondd1808e
line 153why, one step back153 rootPath?: PatchPathFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 154–169why, one step back154) {155 if (value === parent) {156 throw Error("Immer forbids circular references")157 }158159 // In the `finalizeTree` method, only the `root` object may be a draft.160 const isDraftProp = !!state && parent === root161 const isSetMember = isSet(parent)162163 if (isDraft(value)) {164 const path =165 isDraftProp &&166 needPatches &&167 !isSetMember && // Set objects are atomic since they have no keys.168 !has((state as Exclude<ImmerState, SetState>).assigned!, prop) // Skip deep patches for assigned keys.169 ? rootPath!.concat(prop)TS type improvementsMichel Weststratebefore adoptiondd1808e
line 170why, one step back170 : undefinedFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 171–172why, one step back171172 // Drafts owned by `scope` are finalized here.TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 173–174why, one step back173 value = finalize(immer, value, scope, path)174 set(parent, prop, value)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 175–215why, one step back175176 // Drafts from another scope must prevent auto-freezing.177 if (isDraft(value)) {178 scope.canAutoFreeze = false179 }180181 // Unchanged drafts are never passed to the `onAssign` hook.182 // if (isDraftProp && !isSet && value === get(state.base, prop)) return183 }184 // Unchanged draft properties are ignored.185 else if (isDraftProp && is(value, get(state.base, prop))) {186 return187 }188 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.189 else if (isDraftable(value) && !Object.isFrozen(value)) {190 each(value, (key, v) =>191 finalizeProperty(192 immer,193 root,194 state,195 key,196 v,197 value,198 needPatches,199 scope,200 rootPath201 )202 )203 maybeFreeze(immer, value)204 }205206 if (isDraftProp && immer.onAssign && !isSetMember) {207 immer.onAssign(state, prop, value)208 }209}210211export function maybeFreeze(immer: Immer, value: any, deep = false) {212 if (immer.autoFreeze && !isDraft(value)) {213 freeze(value, deep)214 }215}