finalize.ts
This file
Path
src/finalize.ts
172 lines · 4.5 KiB · TypeScript
Last changed by
Small improvementsMichel Weststratebefore adoption712af5c
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
line 25why, one step back25Small improvementsMichel Weststratebefore adoption712af5c
line 26why, one step back26export function processResult(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 improvementsMichel Weststratebefore adoption712af5c
line 29why, one step back29 willFinalize(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–36why, one step back34 }35 if (isDraftable(result)) {36 // Finalize the result in case it contains (or is) a subset of the draft.Small improvementsMichel Weststratebefore adoption712af5c
lines 37–38why, one step back37 result = finalize(result, scope)38 if (!scope.parent_) maybeFreeze(scope, 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–53why, one step back50 })51 }52 } else {53 // Finalize the base draft.Small improvementsMichel Weststratebefore adoption712af5c
line 54why, one step back54 result = finalize(baseDraft, scope, [])feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 55why, one step back55 }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–62why, one step back59 }60 return result !== NOTHING ? result : undefined61}62Small improvementsMichel Weststratebefore adoption712af5c
line 63why, one step back63function finalize(draft: Drafted, scope: ImmerScope, path?: PatchPath) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 64why, one step back64 const state: ImmerState = draft[DRAFT_STATE]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 65–66why, one step back65 if (!state) {66 if (Object.isFrozen(draft)) return draftSmall improvementsMichel Weststratebefore adoption712af5c
line 67why, one step back67 return finalizeTree(draft, scope)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 68–69why, one step back68 }69 // Never finalize drafts owned by another scope.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 70why, one step back70 if (state.scope_ !== scope) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 71–72why, one step back71 return draft72 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 73why, one step back73 if (!state.modified_) {Small improvementsMichel Weststratebefore adoption712af5c
line 74why, one step back74 maybeFreeze(scope, state.base_, true)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 75why, one step back75 return state.base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 76why, one step back76 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 77–78why, one step back77 if (!state.finalized_) {78 state.finalized_ = trueSmall improvementsMichel Weststratebefore adoption712af5c
line 79why, one step back79 finalizeTree(draft, scope, path)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 80–82why, one step back8081 // At this point, all descendants of `state.copy` have been finalized,82 // so we can be sure that `scope.canAutoFreeze` is accurate.Small improvementsMichel Weststratebefore adoption712af5c
line 83why, one step back83 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 84why, one step back84 freeze(state.copy_, false)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 85–86why, one step back85 }86Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 87–88why, one step back87 if (path && scope.patches_) {88 generatePatches(state, path, scope.patches_, scope.inversePatches_!)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 89–90why, one step back89 }90 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 91why, one step back91 return state.copy_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 92–93why, one step back92}93Small improvementsMichel Weststratebefore adoption712af5c
line 94why, one step back94function finalizeTree(root: Drafted, scope: ImmerScope, rootPath?: PatchPath) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 95why, one step back95 const state: ImmerState = root[DRAFT_STATE]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 96–97why, one step back96 if (state) {97 if (Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 98–99why, one step back98 state.type_ === ProxyType.ES5Object ||99 state.type_ === ProxyType.ES5Arrayfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 100–101why, one step back100 ) {101 // Create the final copy, with added keys and without deleted keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 102why, one step back102 state.copy_ = shallowCopy(state.draft_, true)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 103why, one step back103 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 104why, one step back104 root = state.copy_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 105–106why, one step back105 }106 each(root, (key, value) =>Small improvementsMichel Weststratebefore adoption712af5c
line 107why, one step back107 finalizeProperty(scope, root, state, root, key, value, rootPath)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 108–120why, one step back108 )109 return root110}111112function finalizeProperty(113 scope: ImmerScope,114 root: Drafted,115 rootState: ImmerState,116 parentValue: Drafted,117 prop: string | number,118 childValue: any,119 rootPath?: PatchPath120) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 121why, one step back121 invariant(childValue !== parentValue, "Immer forbids circular references")feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 122–131why, one step back122123 // In the `finalizeTree` method, only the `root` object may be a draft.124 const isDraftProp = !!rootState && parentValue === root125 const isSetMember = isSet(parentValue)126127 if (isDraft(childValue)) {128 const path =129 rootPath &&130 isDraftProp &&131 !isSetMember && // Set objects are atomic since they have no keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 132why, one step back132 !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 133–136why, one step back133 ? rootPath!.concat(prop)134 : undefined135136 // Drafts owned by `scope` are finalized here.Small improvementsMichel Weststratebefore adoption712af5c
line 137why, one step back137 childValue = finalize(childValue, scope, path)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 138–141why, one step back138 set(parentValue, prop, childValue)139140 // Drafts from another scope must prevent auto-freezing.141 if (isDraft(childValue)) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 142why, one step back142 scope.canAutoFreeze_ = falsefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 143–145why, one step back143 }144 }145 // Unchanged draft properties are ignored.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 146why, one step back146 else if (isDraftProp && is(childValue, get(rootState.base_, prop))) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 147–151why, one step back147 return148 }149 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.150 // TODO: the recursion over here looks weird, shouldn't non-draft stuff have it's own recursion?151 // 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 152why, one step back152 else if (isDraftable(childValue)) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 153–163why, one step back153 each(childValue, (key, grandChild) =>154 finalizeProperty(155 scope,156 root,157 rootState,158 childValue,159 key,160 grandChild,161 rootPath162 )163 )Small improvementsMichel Weststratebefore adoption712af5c
line 164why, one step back164 if (!scope.parent_) maybeFreeze(scope, childValue)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 165–167why, one step back165 }166}167Small improvementsMichel Weststratebefore adoption712af5c
lines 168–169why, one step back168export function maybeFreeze(scope: {immer_: Immer}, value: any, deep = false) {169 if (scope.immer_.autoFreeze_ && !isDraft(value)) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 170–172why, one step back170 freeze(value, deep)171 }172}