finalize.ts
This file
Path
src/finalize.ts
207 lines · 5.1 KiB · TypeScript
Last changed by
use invariant -321 bytesMichel Weststratebefore adoptionac57da5
History of this fileTake it
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 1why, one step back1import {fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 2–3why, one step back2 Immer,3 ImmerScope,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 4why, one step back4 DRAFT_STATE,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 5why, one step back5 isDraftable,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 6why, one step back6 NOTHING,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 7–11why, one step back7 Drafted,8 PatchPath,9 ProxyType,10 each,11 has,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 12why, one step back12 freeze,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 13why, one step back13 generatePatches,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 14why, one step back14 shallowCopy,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 15–20why, one step back15 ImmerState,16 isSet,17 isDraft,18 SetState,19 set,20 is,Small code cleanupMichel Weststratebefore adoption9217809
lines 21–22why, one step back21 get,22 willFinalizefix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 23why, one step back23} from "./internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 24why, one step back24import invariant from "tiny-invariant"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 25–28why, one step back2526export function processResult(immer: Immer, result: any, scope: ImmerScope) {27 const baseDraft = scope.drafts![0]28 const isReplaced = result !== undefined && result !== baseDraftSmall code cleanupMichel Weststratebefore adoption9217809
line 29why, one step back29 willFinalize(immer, scope, result, isReplaced)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 30–32why, one step back30 if (isReplaced) {31 if (baseDraft[DRAFT_STATE].modified) {32 scope.revoke()use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 33why, one step back33 invariant(false, "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignorefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 34–37why, one step back34 }35 if (isDraftable(result)) {36 // Finalize the result in case it contains (or is) a subset of the draft.37 result = finalize(immer, result, scope)fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524Michel Weststratebefore adoptionacc5e54
line 38why, one step back38 if (!scope.parent) maybeFreeze(immer, result)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 39–150why, one step back39 }40 if (scope.patches) {41 scope.patches.push({42 op: "replace",43 path: [],44 value: result45 })46 scope.inversePatches!.push({47 op: "replace",48 path: [],49 value: baseDraft[DRAFT_STATE].base50 })51 }52 } else {53 // Finalize the base draft.54 result = finalize(immer, baseDraft, scope, [])55 }56 scope.revoke()57 if (scope.patches) {58 scope.patchListener!(scope.patches, scope.inversePatches!)59 }60 return result !== NOTHING ? result : undefined61}6263function finalize(64 immer: Immer,65 draft: Drafted,66 scope: ImmerScope,67 path?: PatchPath68) {69 const state = draft[DRAFT_STATE]70 if (!state) {71 if (Object.isFrozen(draft)) return draft72 return finalizeTree(immer, draft, scope)73 }74 // Never finalize drafts owned by another scope.75 if (state.scope !== scope) {76 return draft77 }78 if (!state.modified) {79 maybeFreeze(immer, state.base, true)80 return state.base81 }82 if (!state.finalized) {83 state.finalized = true84 finalizeTree(immer, state.draft, scope, path)8586 // We cannot really delete anything inside of a Set. We can only replace the whole Set.87 if (immer.onDelete && state.type !== ProxyType.Set) {88 // The `assigned` object is unreliable with ES5 drafts.89 if (immer.useProxies) {90 const {assigned} = state91 each(assigned, (prop, exists) => {92 if (!exists) immer.onDelete!(state, prop as any)93 })94 } else {95 const {base, copy} = state96 each(base, prop => {97 if (!has(copy, prop)) immer.onDelete!(state, prop as any)98 })99 }100 }101 if (immer.onCopy) {102 immer.onCopy(state)103 }104105 // At this point, all descendants of `state.copy` have been finalized,106 // so we can be sure that `scope.canAutoFreeze` is accurate.107 if (immer.autoFreeze && scope.canAutoFreeze) {108 freeze(state.copy, false)109 }110111 if (path && scope.patches) {112 generatePatches(state, path, scope.patches, scope.inversePatches!)113 }114 }115 return state.copy116}117118function finalizeTree(119 immer: Immer,120 root: Drafted,121 scope: ImmerScope,122 rootPath?: PatchPath123) {124 const state = root[DRAFT_STATE]125 if (state) {126 if (127 state.type === ProxyType.ES5Object ||128 state.type === ProxyType.ES5Array129 ) {130 // Create the final copy, with added keys and without deleted keys.131 state.copy = shallowCopy(state.draft, true)132 }133 root = state.copy134 }135 each(root, (key, value) =>136 finalizeProperty(immer, scope, root, state, root, key, value, rootPath)137 )138 return root139}140141function finalizeProperty(142 immer: Immer,143 scope: ImmerScope,144 root: Drafted,145 rootState: ImmerState,146 parentValue: Drafted,147 prop: string | number,148 childValue: any,149 rootPath?: PatchPath150) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 151why, one step back151 invariant(childValue !== parentValue, "Immer forbids circular references")feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 152–181why, one step back152153 // 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 back182 else if (isDraftable(childValue)) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 183–194why, one step back183 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 back195 if (!scope.parent) maybeFreeze(immer, childValue)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 196–207why, one step back196 }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}