finalize.ts
This file
Path
src/finalize.ts
152 lines · 4.4 KiB · TypeScript
Last changed by
scramble plugin names, -56Michel Weststratebefore adoption037b474
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
lines 9–10why, one step back9 freeze,10 shallowCopy,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 11–15why, one step back11 ImmerState,12 isDraft,13 SetState,14 set,15 is,Small code cleanupMichel Weststratebefore adoption9217809
line 16why, one step back16 get,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 17–18why, one step back17 ProxyTypeES5Object,18 ProxyTypeES5Array,scramble plugin names, -56Michel Weststratebefore adoption037b474
lines 19–20why, one step back19 ProxyTypeSet,20 getPluginfix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 21why, one step back21} from "./internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 22why, one step back22import invariant from "tiny-invariant"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 23why, one step back23Small improvementsMichel Weststratebefore adoption712af5c
line 24why, one step back24export function processResult(result: any, scope: ImmerScope) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 25why, one step back25 const baseDraft = scope.drafts_![0]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 26why, one step back26 const isReplaced = result !== undefined && result !== baseDraftscramble plugin names, -56Michel Weststratebefore adoption037b474
lines 27–28why, one step back27 if (!scope.immer_.useProxies_)28 getPlugin("es5").willFinalizeES5_(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 }scramble plugin names, -56Michel Weststratebefore adoption037b474
line 39why, one step back39 // TODO: move to plugin?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.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 54why, one step back54 result = finalize(scope, baseDraft, [])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}62Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 63–68why, one step back63function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {64 // Don't recurse in tho recursive data structures65 if (Object.isFrozen(value)) return value6667 const state: ImmerState = value[DRAFT_STATE]68 // A plain object, might need freezing, might contain draftsfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 69why, one step back69 if (!state) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 70–73why, one step back70 each(value, (key, childValue) =>71 finalizeProperty(rootScope, state, value, key, childValue, path)72 )73 return valuefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 74–75why, one step back74 }75 // Never finalize drafts owned by another scope.Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 76–77why, one step back76 if (state.scope_ !== rootScope) return value77 // Unmodified draft, return the (frozen) originalMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 78why, one step back78 if (!state.modified_) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 79why, one step back79 maybeFreeze(rootScope, state.base_, true)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 80why, one step back80 return state.base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 81why, one step back81 }Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 82why, one step back82 // Not finalized yet, let's do that nowMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 83–84why, one step back83 if (!state.finalized_) {84 state.finalized_ = trueCleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 85–86why, one step back85 const result =86 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 87why, one step back87 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5ArrayCleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 88–97why, one step back88 ? (state.copy_ = shallowCopy(state.draft_, true))89 : state.copy_90 // finalize all children of the copy91 each(result as any, (key, childValue) =>92 finalizeProperty(rootScope, state, result, key, childValue, path)93 )94 // everything inside is frozen, we can freeze here95 maybeFreeze(rootScope, result, false)96 // first time finalizing, let's create those patches97 if (path && rootScope.patches_) {scramble plugin names, -56Michel Weststratebefore adoption037b474
line 98why, one step back98 getPlugin("patches").generatePatches_(Cleanup finalize, -57Michel Weststratebefore adoption530d81b
lines 99–103why, one step back99 state,100 path,101 rootScope.patches_,102 rootScope.inversePatches_!103 )feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 104–105why, one step back104 }105 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 106why, one step back106 return state.copy_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 107–109why, one step back107}108109function finalizeProperty(Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 110–112why, one step back110 rootScope: ImmerScope,111 parentState: undefined | ImmerState,112 targetObject: any,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 113–116why, one step back113 prop: string | number,114 childValue: any,115 rootPath?: PatchPath116) {Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 117why, one step back117 invariant(childValue !== targetObject, "Immer forbids circular references")feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 118–120why, one step back118 if (isDraft(childValue)) {119 const path =120 rootPath &&Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 121why, one step back121 parentState &&Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 122why, one step back122 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 123why, one step back123 !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 124–126why, one step back124 ? rootPath!.concat(prop)125 : undefined126 // Drafts owned by `scope` are finalized here.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 127–128why, one step back127 const res = finalize(rootScope, childValue, path)128 set(targetObject, prop, res)Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
lines 129–130why, one step back129 // Drafts from another scope must prevented to be frozen130 // if we got a draft back from finalize, we're in a nested produce and shouldn't freezeInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 131–132why, one step back131 if (isDraft(res)) {132 rootScope.canAutoFreeze_ = falseCleanup finalize, -57Michel Weststratebefore adoption530d81b
line 133why, one step back133 } else returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 134–135why, one step back134 }135 // Unchanged draft properties are ignored.Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 136why, one step back136 if (parentState && is(childValue, get(parentState!.base_, prop))) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 137–139why, one step back137 return138 }139 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 140why, one step back140 if (isDraftable(childValue)) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 141why, one step back141 finalize(rootScope, childValue)Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 142why, one step back142 // immer deep freezes plain objects, so if there is no parent state, we freeze as wellInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 143–144why, one step back143 if (!parentState || !parentState.scope_.parent_)144 maybeFreeze(rootScope, childValue)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 145–147why, one step back145 }146}147Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 148why, one step back148function maybeFreeze(scope: ImmerScope, value: any, deep = false) {Cleanup finalize, -57Michel Weststratebefore adoption530d81b
line 149why, one step back149 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 150–152why, one step back150 freeze(value, deep)151 }152}