finalize.ts
This file
Path
src/finalize.ts
151 lines · 4.4 KiB · TypeScript
Last changed by
Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
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
line 2why, one step back2 ImmerScope,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 3why, one step back3 DRAFT_STATE,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 4why, one step back4 isDraftable,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 5why, one step back5 NOTHING,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 6–8why, one step back6 PatchPath,7 each,8 has,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 9why, one step back9 freeze,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 10why, one step back10 generatePatches,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 11why, one step back11 shallowCopy,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 12–16why, one step back12 ImmerState,13 isDraft,14 SetState,15 set,16 is,Small code cleanupMichel Weststratebefore adoption9217809
line 17why, one step back17 get,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 18–21why, one step back18 willFinalize,19 ProxyTypeES5Object,20 ProxyTypeES5Array,21 ProxyTypeSetfix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 22why, one step back22} from "./internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 23why, one step back23import invariant from "tiny-invariant"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 24why, one step back24Small improvementsMichel Weststratebefore adoption712af5c
line 25why, one step back25export function processResult(result: any, scope: ImmerScope) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 26why, one step back26 const baseDraft = scope.drafts_![0]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 27why, one step back27 const isReplaced = result !== undefined && result !== baseDraftSmall improvementsMichel Weststratebefore adoption712af5c
line 28why, one step back28 willFinalize(scope, result, isReplaced)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 29why, one step back29 if (isReplaced) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 30–31why, one step back30 if (baseDraft[DRAFT_STATE].modified_) {31 scope.revoke_()use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 32why, one step back32 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 33–35why, one step back33 }34 if (isDraftable(result)) {35 // Finalize the result in case it contains (or is) a subset of the draft.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 36why, one step back36 result = finalize(scope, result)Small improvementsMichel Weststratebefore adoption712af5c
line 37why, one step back37 if (!scope.parent_) maybeFreeze(scope, result)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 38why, one step back38 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 39–40why, one step back39 if (scope.patches_) {40 scope.patches_.push({feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 41–44why, one step back41 op: "replace",42 path: [],43 value: result44 })Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 45why, one step back45 scope.inversePatches_!.push({feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 46–47why, one step back46 op: "replace",47 path: [],Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 48why, one step back48 value: (baseDraft[DRAFT_STATE] as ImmerState).base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 49–52why, one step back49 })50 }51 } else {52 // Finalize the base draft.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 53why, one step back53 result = finalize(scope, baseDraft, [])feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 54why, one step back54 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 55–57why, one step back55 scope.revoke_()56 if (scope.patches_) {57 scope.patchListener_!(scope.patches_, scope.inversePatches_!)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 58–61why, one step back58 }59 return result !== NOTHING ? result : undefined60}61Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 62–67why, one step back62function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {63 // Don't recurse in tho recursive data structures64 if (Object.isFrozen(value)) return value6566 const state: ImmerState = value[DRAFT_STATE]67 // A plain object, might need freezing, might contain draftsfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 68why, one step back68 if (!state) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 69–72why, one step back69 each(value, (key, childValue) =>70 finalizeProperty(rootScope, state, value, key, childValue, path)71 )72 return valuefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 73–74why, one step back73 }74 // Never finalize drafts owned by another scope.Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 75–76why, one step back75 if (state.scope_ !== rootScope) return value76 // Unmodified draft, return the (frozen) originalMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 77why, one step back77 if (!state.modified_) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 78why, one step back78 maybeFreeze(rootScope, state.base_, true)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 79why, one step back79 return state.base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 80why, one step back80 }Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 81why, one step back81 // Not finalized yet, let's do that nowMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 82–83why, one step back82 if (!state.finalized_) {83 state.finalized_ = trueCleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 84–85why, one step back84 const result =85 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 86why, one step back86 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5ArrayCleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 87–102why, one step back87 ? (state.copy_ = shallowCopy(state.draft_, true))88 : state.copy_89 // finalize all children of the copy90 each(result as any, (key, childValue) =>91 finalizeProperty(rootScope, state, result, key, childValue, path)92 )93 // everything inside is frozen, we can freeze here94 maybeFreeze(rootScope, result, false)95 // first time finalizing, let's create those patches96 if (path && rootScope.patches_) {97 generatePatches(98 state,99 path,100 rootScope.patches_,101 rootScope.inversePatches_!102 )feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 103–104why, one step back103 }104 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 105why, one step back105 return state.copy_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 106–108why, one step back106}107108function finalizeProperty(Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 109–111why, one step back109 rootScope: ImmerScope,110 parentState: undefined | ImmerState,111 targetObject: any,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 112–115why, one step back112 prop: string | number,113 childValue: any,114 rootPath?: PatchPath115) {Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 116why, one step back116 invariant(childValue !== targetObject, "Immer forbids circular references")feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 117–119why, one step back117 if (isDraft(childValue)) {118 const path =119 rootPath &&Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 120why, one step back120 parentState &&Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 121why, one step back121 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 122why, one step back122 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 123–125why, one step back123 ? rootPath!.concat(prop)124 : undefined125 // Drafts owned by `scope` are finalized here.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 126–127why, one step back126 const res = finalize(rootScope, childValue, path)127 set(targetObject, prop, res)Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
lines 128–129why, one step back128 // Drafts from another scope must prevented to be frozen129 // if we got a draft back from finalize, we're in a nested produce and shouldn't freezeInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 130–131why, one step back130 if (isDraft(res)) {131 rootScope.canAutoFreeze_ = falseCleanup finalize, -57Michel Weststratebefore adoption530d81b
line 132why, one step back132 } else returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 133–134why, one step back133 }134 // Unchanged draft properties are ignored.Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 135why, one step back135 if (parentState && is(childValue, get(parentState!.base_, prop))) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 136–138why, one step back136 return137 }138 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 139why, one step back139 if (isDraftable(childValue)) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 140why, one step back140 finalize(rootScope, childValue)Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 141why, one step back141 // immer deep freezes plain objects, so if there is no parent state, we freeze as wellInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 142–143why, one step back142 if (!parentState || !parentState.scope_.parent_)143 maybeFreeze(rootScope, childValue)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 144–146why, one step back144 }145}146Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 147why, one step back147function maybeFreeze(scope: ImmerScope, value: any, deep = false) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 148why, one step back148 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 149–151why, one step back149 freeze(value, deep)150 }151}