finalize.ts
This file
Path
src/finalize.ts
149 lines · 4.3 KiB · TypeScript
Last changed by
Cleanup finalize, -57Michel Weststratebefore adoption530d81b
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–9why, one step back6 PatchPath,7 ProxyType,8 each,9 has,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 10why, one step back10 freeze,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 11why, one step back11 generatePatches,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 12why, one step back12 shallowCopy,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 13–17why, one step back13 ImmerState,14 isDraft,15 SetState,16 set,17 is,Small code cleanupMichel Weststratebefore adoption9217809
lines 18–19why, one step back18 get,19 willFinalizefix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 20why, one step back20} from "./internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 21why, one step back21import invariant from "tiny-invariant"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 22why, one step back22Small improvementsMichel Weststratebefore adoption712af5c
line 23why, one step back23export function processResult(result: any, scope: ImmerScope) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 24why, one step back24 const baseDraft = scope.drafts_![0]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 25why, one step back25 const isReplaced = result !== undefined && result !== baseDraftSmall improvementsMichel Weststratebefore adoption712af5c
line 26why, one step back26 willFinalize(scope, result, isReplaced)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 27why, one step back27 if (isReplaced) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 28–29why, one step back28 if (baseDraft[DRAFT_STATE].modified_) {29 scope.revoke_()use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 30why, one step back30 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 31–33why, one step back31 }32 if (isDraftable(result)) {33 // Finalize the result in case it contains (or is) a subset of the draft.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 34why, one step back34 result = finalize(scope, result)Small improvementsMichel Weststratebefore adoption712af5c
line 35why, one step back35 if (!scope.parent_) maybeFreeze(scope, result)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 36why, one step back36 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 37–38why, one step back37 if (scope.patches_) {38 scope.patches_.push({feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 39–42why, one step back39 op: "replace",40 path: [],41 value: result42 })Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 43why, one step back43 scope.inversePatches_!.push({feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 44–45why, one step back44 op: "replace",45 path: [],Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 46why, one step back46 value: (baseDraft[DRAFT_STATE] as ImmerState).base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 47–50why, one step back47 })48 }49 } else {50 // Finalize the base draft.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 51why, one step back51 result = finalize(scope, baseDraft, [])feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 52why, one step back52 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 53–55why, one step back53 scope.revoke_()54 if (scope.patches_) {55 scope.patchListener_!(scope.patches_, scope.inversePatches_!)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 56–59why, one step back56 }57 return result !== NOTHING ? result : undefined58}59Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 60–65why, one step back60function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {61 // Don't recurse in tho recursive data structures62 if (Object.isFrozen(value)) return value6364 const state: ImmerState = value[DRAFT_STATE]65 // A plain object, might need freezing, might contain draftsfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 66why, one step back66 if (!state) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 67–70why, one step back67 each(value, (key, childValue) =>68 finalizeProperty(rootScope, state, value, key, childValue, path)69 )70 return valuefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 71–72why, one step back71 }72 // Never finalize drafts owned by another scope.Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 73–74why, one step back73 if (state.scope_ !== rootScope) return value74 // Unmodified draft, return the (frozen) originalMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 75why, one step back75 if (!state.modified_) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 76why, one step back76 maybeFreeze(rootScope, state.base_, true)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 77why, one step back77 return state.base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 78why, one step back78 }Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 79why, one step back79 // Not finalized yet, let's do that nowMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 80–81why, one step back80 if (!state.finalized_) {81 state.finalized_ = trueCleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 82–100why, one step back82 const result =83 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.84 state.type_ === ProxyType.ES5Object || state.type_ === ProxyType.ES5Array85 ? (state.copy_ = shallowCopy(state.draft_, true))86 : state.copy_87 // finalize all children of the copy88 each(result as any, (key, childValue) =>89 finalizeProperty(rootScope, state, result, key, childValue, path)90 )91 // everything inside is frozen, we can freeze here92 maybeFreeze(rootScope, result, false)93 // first time finalizing, let's create those patches94 if (path && rootScope.patches_) {95 generatePatches(96 state,97 path,98 rootScope.patches_,99 rootScope.inversePatches_!100 )feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 101–102why, one step back101 }102 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 103why, one step back103 return state.copy_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 104–106why, one step back104}105106function finalizeProperty(Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 107–109why, one step back107 rootScope: ImmerScope,108 parentState: undefined | ImmerState,109 targetObject: any,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 110–113why, one step back110 prop: string | number,111 childValue: any,112 rootPath?: PatchPath113) {Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 114why, one step back114 invariant(childValue !== targetObject, "Immer forbids circular references")feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 115–117why, one step back115 if (isDraft(childValue)) {116 const path =117 rootPath &&Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
lines 118–119why, one step back118 parentState &&119 parentState!.type_ !== ProxyType.Set && // Set objects are atomic since they have no keys.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 120why, one step back120 !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 121–123why, one step back121 ? rootPath!.concat(prop)122 : undefined123 // Drafts owned by `scope` are finalized here.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 124–125why, one step back124 const res = finalize(rootScope, childValue, path)125 set(targetObject, prop, res)Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
lines 126–127why, one step back126 // Drafts from another scope must prevented to be frozen127 // if we got a draft back from finalize, we're in a nested produce and shouldn't freezeInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 128–129why, one step back128 if (isDraft(res)) {129 rootScope.canAutoFreeze_ = falseCleanup finalize, -57Michel Weststratebefore adoption530d81b
line 130why, one step back130 } else returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 131–132why, one step back131 }132 // Unchanged draft properties are ignored.Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 133why, one step back133 if (parentState && is(childValue, get(parentState!.base_, prop))) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 134–136why, one step back134 return135 }136 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 137why, one step back137 if (isDraftable(childValue)) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 138why, one step back138 finalize(rootScope, childValue)Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 139why, one step back139 // immer deep freezes plain objects, so if there is no parent state, we freeze as wellInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 140–141why, one step back140 if (!parentState || !parentState.scope_.parent_)141 maybeFreeze(rootScope, childValue)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 142–144why, one step back142 }143}144Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 145–146why, one step back145export function maybeFreeze(scope: ImmerScope, value: any, deep = false) {146 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 147–149why, one step back147 freeze(value, deep)148 }149}