Skip to content
Myr

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 file

Take it

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 1why, one step back
1import {

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 2why, one step back
2 ImmerScope,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 3why, one step back
3 DRAFT_STATE,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 4why, one step back
4 isDraftable,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 5why, one step back
5 NOTHING,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 6–9why, one step back
6 PatchPath,
7 ProxyType,8 each,9 has,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 10why, one step back
10 freeze,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 11why, one step back
11 generatePatches,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 12why, one step back
12 shallowCopy,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 13–17why, one step back
13 ImmerState,
14 isDraft,15 SetState,16 set,17 is,

Small code cleanupMichel Weststratebefore adoption9217809

lines 18–19why, one step back
18 get,
19 willFinalize

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 20why, one step back
20} from "./internal"

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 21why, one step back
21import invariant from "tiny-invariant"

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 22why, one step back
22

Small improvementsMichel Weststratebefore adoption712af5c

line 23why, one step back
23export function processResult(result: any, scope: ImmerScope) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 24why, one step back
24 const baseDraft = scope.drafts_![0]

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 25why, one step back
25 const isReplaced = result !== undefined && result !== baseDraft

Small improvementsMichel Weststratebefore adoption712af5c

line 26why, one step back
26 willFinalize(scope, result, isReplaced)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 27why, one step back
27 if (isReplaced) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 28–29why, one step back
28 if (baseDraft[DRAFT_STATE].modified_) {
29 scope.revoke_()

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 30why, one step back
30 invariant(false, "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 31–33why, one step back
31 }
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 back
34 result = finalize(scope, result)

Small improvementsMichel Weststratebefore adoption712af5c

line 35why, one step back
35 if (!scope.parent_) maybeFreeze(scope, result)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 36why, one step back
36 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 37–38why, one step back
37 if (scope.patches_) {
38 scope.patches_.push({

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 39–42why, one step back
39 op: "replace",
40 path: [],41 value: result42 })

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 43why, one step back
43 scope.inversePatches_!.push({

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 44–45why, one step back
44 op: "replace",
45 path: [],

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 46why, one step back
46 value: (baseDraft[DRAFT_STATE] as ImmerState).base_

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 47–50why, one step back
47 })
48 }49 } else {50 // Finalize the base draft.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 51why, one step back
51 result = finalize(scope, baseDraft, [])

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 52why, one step back
52 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 53–55why, one step back
53 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 back
56 }
57 return result !== NOTHING ? result : undefined58}59

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 60–65why, one step back
60function 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 drafts

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 66why, one step back
66 if (!state) {

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 67–70why, one step back
67 each(value, (key, childValue) =>
68 finalizeProperty(rootScope, state, value, key, childValue, path)69 )70 return value

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 71–72why, one step back
71 }
72 // Never finalize drafts owned by another scope.

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 73–74why, one step back
73 if (state.scope_ !== rootScope) return value
74 // Unmodified draft, return the (frozen) original

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 75why, one step back
75 if (!state.modified_) {

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

line 76why, one step back
76 maybeFreeze(rootScope, state.base_, true)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 77why, one step back
77 return state.base_

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 78why, one step back
78 }

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

line 79why, one step back
79 // Not finalized yet, let's do that now

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 80–81why, one step back
80 if (!state.finalized_) {
81 state.finalized_ = true

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 82–100why, one step back
82 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 back
101 }
102 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 103why, one step back
103 return state.copy_

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 104–106why, one step back
104}
105106function finalizeProperty(

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 107–109why, one step back
107 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 back
110 prop: string | number,
111 childValue: any,112 rootPath?: PatchPath113) {

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 114why, one step back
114 invariant(childValue !== targetObject, "Immer forbids circular references")

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 115–117why, one step back
115 if (isDraft(childValue)) {
116 const path =117 rootPath &&

Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97

lines 118–119why, one step back
118 parentState &&
119 parentState!.type_ !== ProxyType.Set && // Set objects are atomic since they have no keys.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 120why, one step back
120 !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 back
121 ? rootPath!.concat(prop)
122 : undefined123 // Drafts owned by `scope` are finalized here.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 124–125why, one step back
124 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 back
126 // Drafts from another scope must prevented to be frozen
127 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 128–129why, one step back
128 if (isDraft(res)) {
129 rootScope.canAutoFreeze_ = false

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

line 130why, one step back
130 } else return

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 131–132why, one step back
131 }
132 // Unchanged draft properties are ignored.

Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97

line 133why, one step back
133 if (parentState && is(childValue, get(parentState!.base_, prop))) {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 134–136why, one step back
134 return
135 }136 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 137why, one step back
137 if (isDraftable(childValue)) {

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

line 138why, one step back
138 finalize(rootScope, childValue)

Cleanup. Don't recurse into frozen data structuresMichel Weststratebefore adoptiond32cb97

line 139why, one step back
139 // immer deep freezes plain objects, so if there is no parent state, we freeze as well

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 140–141why, one step back
140 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 back
142 }
143}144

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 145–146why, one step back
145export 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 back
147 freeze(value, deep)
148 }149}