finalize.ts
This file
Path
src/finalize.ts
159 lines · 4.4 KiB · TypeScript
Last changed by
Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
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"Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 25why, one step back25import {Archtype} from "./types-internal"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 26why, one step back26Small improvementsMichel Weststratebefore adoption712af5c
line 27why, one step back27export function processResult(result: any, scope: ImmerScope) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 28why, one step back28 const baseDraft = scope.drafts_![0]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 29why, one step back29 const isReplaced = result !== undefined && result !== baseDraftSmall improvementsMichel Weststratebefore adoption712af5c
line 30why, one step back30 willFinalize(scope, result, isReplaced)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 31why, one step back31 if (isReplaced) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 32–33why, one step back32 if (baseDraft[DRAFT_STATE].modified_) {33 scope.revoke_()use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 34why, one step back34 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 35–37why, one step back35 }36 if (isDraftable(result)) {37 // Finalize the result in case it contains (or is) a subset of the draft.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 38why, one step back38 result = finalize(scope, result)Small improvementsMichel Weststratebefore adoption712af5c
line 39why, one step back39 if (!scope.parent_) maybeFreeze(scope, result)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 40why, one step back40 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 41–42why, one step back41 if (scope.patches_) {42 scope.patches_.push({feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 43–46why, one step back43 op: "replace",44 path: [],45 value: result46 })Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 47why, one step back47 scope.inversePatches_!.push({feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 48–49why, one step back48 op: "replace",49 path: [],Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 50why, one step back50 value: (baseDraft[DRAFT_STATE] as ImmerState).base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 51–54why, one step back51 })52 }53 } else {54 // Finalize the base draft.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 55why, one step back55 result = finalize(scope, baseDraft, [])feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 56why, one step back56 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 57–59why, one step back57 scope.revoke_()58 if (scope.patches_) {59 scope.patchListener_!(scope.patches_, scope.inversePatches_!)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 60–63why, one step back60 }61 return result !== NOTHING ? result : undefined62}63Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 64why, one step back64function finalize(scope: ImmerScope, draft: Drafted, path?: PatchPath) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 65why, one step back65 const state: ImmerState = draft[DRAFT_STATE]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 66–67why, one step back66 if (!state) {67 if (Object.isFrozen(draft)) return draftInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 68why, one step back68 return finalizeTree(scope, draft)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 69–70why, one step back69 }70 // Never finalize drafts owned by another scope.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 71why, one step back71 if (state.scope_ !== scope) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 72–73why, one step back72 return draft73 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 74why, one step back74 if (!state.modified_) {Small improvementsMichel Weststratebefore adoption712af5c
line 75why, one step back75 maybeFreeze(scope, state.base_, true)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 76why, one step back76 return state.base_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 77why, one step back77 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 78–79why, one step back78 if (!state.finalized_) {79 state.finalized_ = trueInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 80why, one step back80 finalizeTree(scope, draft, path)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 81–83why, one step back8182 // At this point, all descendants of `state.copy` have been finalized,83 // so we can be sure that `scope.canAutoFreeze` is accurate.Small improvementsMichel Weststratebefore adoption712af5c
line 84why, one step back84 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 85why, one step back85 freeze(state.copy_, false)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 86–87why, one step back86 }87Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 88–89why, one step back88 if (path && scope.patches_) {89 generatePatches(state, path, scope.patches_, scope.inversePatches_!)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 90–91why, one step back90 }91 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 92why, one step back92 return state.copy_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 93–94why, one step back93}94Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 95why, one step back95function finalizeTree(rootScope: ImmerScope, value: any, rootPath?: PatchPath) {Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
lines 96–97why, one step back96 // Don't recurse in tho recursive data structures97 if (Object.isFrozen(value)) return valueInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 98why, one step back98 const state: ImmerState = value[DRAFT_STATE]feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 99–100why, one step back99 if (state) {100 if (Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 101–102why, one step back101 state.type_ === ProxyType.ES5Object ||102 state.type_ === ProxyType.ES5Arrayfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 103–104why, one step back103 ) {104 // Create the final copy, with added keys and without deleted keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 105why, one step back105 state.copy_ = shallowCopy(state.draft_, true)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 106why, one step back106 }Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 107why, one step back107 value = state.copy_feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 108why, one step back108 }Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 109–110why, one step back109 each(value, (key, childValue) =>110 finalizeProperty(rootScope, state, value, key, childValue, rootPath)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 111why, one step back111 )Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 112why, one step back112 return valuefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 113–115why, one step back113}114115function finalizeProperty(Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 116–118why, one step back116 rootScope: ImmerScope,117 parentState: undefined | ImmerState,118 targetObject: any,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 119–122why, one step back119 prop: string | number,120 childValue: any,121 rootPath?: PatchPath122) {Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 123why, one step back123 invariant(childValue !== targetObject, "Immer forbids circular references")feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 124–126why, one step back124 if (isDraft(childValue)) {125 const path =126 rootPath &&Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
lines 127–128why, one step back127 parentState &&128 parentState!.type_ !== ProxyType.Set && // Set objects are atomic since they have no keys.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 129why, one step back129 !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 130–132why, one step back130 ? rootPath!.concat(prop)131 : undefined132 // Drafts owned by `scope` are finalized here.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 133–134why, one step back133 const res = finalize(rootScope, childValue, path)134 set(targetObject, prop, res)Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
lines 135–136why, one step back135 // Drafts from another scope must prevented to be frozen136 // if we got a draft back from finalize, we're in a nested produce and shouldn't freezeInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 137–138why, one step back137 if (isDraft(res)) {138 rootScope.canAutoFreeze_ = falsefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 139why, one step back139 }Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
line 140why, one step back140 returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 141–142why, one step back141 }142 // Unchanged draft properties are ignored.Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 143why, one step back143 if (parentState && is(childValue, get(parentState!.base_, prop))) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 144–146why, one step back144 return145 }146 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.Initial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 147–148why, one step back147 if (isDraftable(childValue)) {148 finalizeTree(rootScope, childValue)Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97
line 149why, one step back149 // immer deep freezes plain objects, so if there is no parent state, we freeze as wellInitial finalize cleanupMichel Weststratebefore adoption67ab3ab
lines 150–151why, one step back150 if (!parentState || !parentState.scope_.parent_)151 maybeFreeze(rootScope, childValue)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 152–154why, one step back152 }153}154Small improvementsMichel Weststratebefore adoption712af5c
lines 155–156why, one step back155export function maybeFreeze(scope: {immer_: Immer}, value: any, deep = false) {156 if (scope.immer_.autoFreeze_ && !isDraft(value)) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 157–159why, one step back157 freeze(value, deep)158 }159}