finalize.ts
This file
Path
src/finalize.ts
217 lines · 5.2 KiB · TypeScript
Last changed by
TS type improvementsMichel Weststratebefore adoptiondd1808e
History of this fileTake it
Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 1–14why, 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 isMap,11 isEnumerable,12 DRAFT_STATE,13 NOTHING,14 freeze,TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 15–16why, one step back15 shallowCopy,16 setFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 17–19why, one step back17} from "./common"18import {isDraft, isDraftable} from "./index"19import {SetState} from "./set"TS type improvementsMichel Weststratebefore adoptiondd1808e
line 20why, one step back20import {generatePatches, PatchPath} from "./patches"Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 21–32why, one step back2122export function processResult(immer: Immer, result: any, scope: ImmerScope) {23 const baseDraft = scope.drafts![0]24 const isReplaced = result !== undefined && result !== baseDraft25 immer.willFinalize(scope, result, isReplaced)26 if (isReplaced) {27 if (baseDraft[DRAFT_STATE].modified) {28 scope.revoke()29 throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore30 }31 if (isDraftable(result)) {32 // Finalize the result in case it contains (or is) a subset of the draft.TS type improvementsMichel Weststratebefore adoptiondd1808e
line 33why, one step back33 result = finalize(immer, result, scope)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 34–49why, one step back34 maybeFreeze(immer, result)35 }36 if (scope.patches) {37 scope.patches.push({38 op: "replace",39 path: [],40 value: result41 })42 scope.inversePatches!.push({43 op: "replace",44 path: [],45 value: baseDraft[DRAFT_STATE].base46 })47 }48 } else {49 // Finalize the base draft.TS type improvementsMichel Weststratebefore adoptiondd1808e
line 50why, one step back50 result = finalize(immer, baseDraft, scope, [])Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 51–61why, one step back51 }52 scope.revoke()53 if (scope.patches) {54 scope.patchListener!(scope.patches, scope.inversePatches!)55 }56 return result !== NOTHING ? result : undefined57}5859function finalize(60 immer: Immer,61 draft: Drafted,TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 62–63why, one step back62 scope: ImmerScope,63 path?: PatchPathFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 64–67why, one step back64) {65 const state = draft[DRAFT_STATE]66 if (!state) {67 if (Object.isFrozen(draft)) return draftTS type improvementsMichel Weststratebefore adoptiondd1808e
line 68why, one step back68 return finalizeTree(immer, draft, scope)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 69–79why, one step back69 }70 // Never finalize drafts owned by another scope.71 if (state.scope !== scope) {72 return draft73 }74 if (!state.modified) {75 maybeFreeze(immer, state.base, true)76 return state.base77 }78 if (!state.finalized) {79 state.finalized = trueTS type improvementsMichel Weststratebefore adoptiondd1808e
line 80why, one step back80 finalizeTree(immer, state.draft, scope, path)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 81–116why, one step back8182 // We cannot really delete anything inside of a Set. We can only replace the whole Set.83 if (immer.onDelete && !isSet(state.base)) {84 // The `assigned` object is unreliable with ES5 drafts.85 if (immer.useProxies) {86 const {assigned} = state87 each(assigned, (prop, exists) => {88 if (!exists) immer.onDelete?.(state, prop as any)89 })90 } else {91 const {base, copy} = state92 each(base, prop => {93 if (!has(copy, prop)) immer.onDelete?.(state, prop as any)94 })95 }96 }97 if (immer.onCopy) {98 immer.onCopy(state)99 }100101 // At this point, all descendants of `state.copy` have been finalized,102 // so we can be sure that `scope.canAutoFreeze` is accurate.103 if (immer.autoFreeze && scope.canAutoFreeze) {104 freeze(state.copy, false)105 }106107 if (path && scope.patches) {108 generatePatches(state, path, scope.patches, scope.inversePatches!)109 }110 }111 return state.copy112}113114function finalizeTree(115 immer: Immer,116 root: Drafted,TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 117–118why, one step back117 scope: ImmerScope,118 rootPath?: PatchPathFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 119–121why, one step back119) {120 const state = root[DRAFT_STATE]121 if (state) {TS type improvementsMichel Weststratebefore adoptiondd1808e
line 122why, one step back122 if (state.type === "es5_object" || state.type === "es5_array") {Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 123why, one step back123 // Create the final copy, with added keys and without deleted keys.TS type improvementsMichel Weststratebefore adoptiondd1808e
line 124why, one step back124 state.copy = shallowCopy(state.draft, true)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 125–151why, one step back125 }126 root = state.copy127 }128 const needPatches = !!rootPath && !!scope.patches129 each(root, (key, value) =>130 finalizeProperty(131 immer,132 root,133 state,134 key,135 value,136 root,137 needPatches,138 scope,139 rootPath140 )141 )142 return root143}144145function finalizeProperty(146 // TODO: can do with less args?147 immer: Immer,148 root: ImmerState,149 state: ImmerState,150 prop: string | number,151 value: any,TS type improvementsMichel Weststratebefore adoptiondd1808e
line 152why, one step back152 parent: Drafted,Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 153–154why, one step back153 needPatches: boolean,154 scope: ImmerScope,TS type improvementsMichel Weststratebefore adoptiondd1808e
line 155why, one step back155 rootPath?: PatchPathFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 156–171why, one step back156) {157 if (value === parent) {158 throw Error("Immer forbids circular references")159 }160161 // In the `finalizeTree` method, only the `root` object may be a draft.162 const isDraftProp = !!state && parent === root163 const isSetMember = isSet(parent)164165 if (isDraft(value)) {166 const path =167 isDraftProp &&168 needPatches &&169 !isSetMember && // Set objects are atomic since they have no keys.170 !has((state as Exclude<ImmerState, SetState>).assigned!, prop) // Skip deep patches for assigned keys.171 ? rootPath!.concat(prop)TS type improvementsMichel Weststratebefore adoptiondd1808e
line 172why, one step back172 : undefinedFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 173–174why, one step back173174 // Drafts owned by `scope` are finalized here.TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 175–176why, one step back175 value = finalize(immer, value, scope, path)176 set(parent, prop, value)Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 177–217why, one step back177178 // Drafts from another scope must prevent auto-freezing.179 if (isDraft(value)) {180 scope.canAutoFreeze = false181 }182183 // Unchanged drafts are never passed to the `onAssign` hook.184 // if (isDraftProp && !isSet && value === get(state.base, prop)) return185 }186 // Unchanged draft properties are ignored.187 else if (isDraftProp && is(value, get(state.base, prop))) {188 return189 }190 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.191 else if (isDraftable(value) && !Object.isFrozen(value)) {192 each(value, (key, v) =>193 finalizeProperty(194 immer,195 root,196 state,197 key,198 v,199 value,200 needPatches,201 scope,202 rootPath203 )204 )205 maybeFreeze(immer, value)206 }207208 if (isDraftProp && immer.onAssign && !isSetMember) {209 immer.onAssign(state, prop, value)210 }211}212213export function maybeFreeze(immer: Immer, value: any, deep = false) {214 if (immer.autoFreeze && !isDraft(value)) {215 freeze(value, deep)216 }217}