finalize.ts
This file
Path
src/finalize.ts
207 lines · 5.2 KiB · TypeScript
Last changed by
Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
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–26why, one step back2526export function processResult(immer: Immer, result: any, scope: ImmerScope) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 27why, one step back27 const baseDraft = scope.drafts_![0]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 28why, one step back28 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
line 30why, one step back30 if (isReplaced) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 31–32why, one step back31 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)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 38why, one step back38 if (!scope.parent_) maybeFreeze(immer, result)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 39why, one step back39 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 40–41why, one step back40 if (scope.patches_) {41 scope.patches_.push({feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 42–45why, one step back42 op: "replace",43 path: [],44 value: result45 })Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 46why, one step back46 scope.inversePatches_!.push({feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 47–48why, one step back47 op: "replace",48 path: [],Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 49why, one step back49 value: (baseDraft[DRAFT_STATE] as ImmerState).base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 50–55why, one step back50 })51 }52 } else {53 // Finalize the base draft.54 result = finalize(immer, baseDraft, scope, [])55 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 56–58why, one step back56 scope.revoke_()57 if (scope.patches_) {58 scope.patchListener_!(scope.patches_, scope.inversePatches_!)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 59–68why, one step back59 }60 return result !== NOTHING ? result : undefined61}6263function finalize(64 immer: Immer,65 draft: Drafted,66 scope: ImmerScope,67 path?: PatchPath68) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 69why, one step back69 const state: ImmerState = draft[DRAFT_STATE]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 70–74why, one step back70 if (!state) {71 if (Object.isFrozen(draft)) return draft72 return finalizeTree(immer, draft, scope)73 }74 // Never finalize drafts owned by another scope.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 75why, one step back75 if (state.scope_ !== scope) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 76–77why, one step back76 return draft77 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 78–80why, one step back78 if (!state.modified_) {79 maybeFreeze(immer, state.base_, true)80 return state.base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 81why, one step back81 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 82–84why, one step back82 if (!state.finalized_) {83 state.finalized_ = true84 finalizeTree(immer, state.draft_, scope, path)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 85–86why, one step back8586 // We cannot really delete anything inside of a Set. We can only replace the whole Set.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 87why, one step back87 if (immer.onDelete && state.type_ !== ProxyType.Set) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 88–89why, one step back88 // The `assigned` object is unreliable with ES5 drafts.89 if (immer.useProxies) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 90–91why, one step back90 const {assigned_} = state91 each(assigned_ as any, (prop, exists) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 92–94why, one step back92 if (!exists) immer.onDelete!(state, prop as any)93 })94 } else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 95–97why, one step back95 const {base_, copy_} = state96 each(base_, prop => {97 if (!has(copy_, prop)) immer.onDelete!(state, prop as any)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 98–106why, one step back98 })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.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 107–108why, one step back107 if (immer.autoFreeze && scope.canAutoFreeze_) {108 freeze(state.copy_, false)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 109–110why, one step back109 }110Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 111–112why, one step back111 if (path && scope.patches_) {112 generatePatches(state, path, scope.patches_, scope.inversePatches_!)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 113–114why, one step back113 }114 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 115why, one step back115 return state.copy_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 116–123why, one step back116}117118function finalizeTree(119 immer: Immer,120 root: Drafted,121 scope: ImmerScope,122 rootPath?: PatchPath123) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 124why, one step back124 const state: ImmerState = root[DRAFT_STATE]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 125–126why, one step back125 if (state) {126 if (Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 127–128why, one step back127 state.type_ === ProxyType.ES5Object ||128 state.type_ === ProxyType.ES5Arrayfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 129–130why, one step back129 ) {130 // Create the final copy, with added keys and without deleted keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 131why, one step back131 state.copy_ = shallowCopy(state.draft_, true)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 132why, one step back132 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 133why, one step back133 root = state.copy_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 134–150why, one step back134 }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–161why, 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.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 162why, one step back162 !has((rootState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 163–171why, one step back163 ? 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)) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 172why, one step back172 scope.canAutoFreeze_ = falsefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 173–175why, one step back173 }174 }175 // Unchanged draft properties are ignored.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 176why, one step back176 else if (isDraftProp && is(childValue, get(rootState.base_, prop))) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 177–181why, one step back177 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 )Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
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}