Skip to content
Myr

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 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–8why, one step back
6 PatchPath,
7 each,8 has,

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

line 9why, one step back
9 freeze,

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

line 10why, one step back
10 generatePatches,

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

line 11why, one step back
11 shallowCopy,

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

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

Small code cleanupMichel Weststratebefore adoption9217809

line 17why, one step back
17 get,

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 18–21why, one step back
18 willFinalize,
19 ProxyTypeES5Object,20 ProxyTypeES5Array,21 ProxyTypeSet

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

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

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

line 24why, one step back
24

Small improvementsMichel Weststratebefore adoption712af5c

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Small improvementsMichel Weststratebefore adoption712af5c

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 32why, one step back
32 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 33–35why, one step back
33 }
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 back
36 result = finalize(scope, result)

Small improvementsMichel Weststratebefore adoption712af5c

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

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

line 38why, one step back
38 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 39–40why, one step back
39 if (scope.patches_) {
40 scope.patches_.push({

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

lines 41–44why, one step back
41 op: "replace",
42 path: [],43 value: result44 })

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 46–47why, one step back
46 op: "replace",
47 path: [],

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

line 54why, one step back
54 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

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

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

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

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 80why, one step back
80 }

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 84–85why, one step back
84 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 back
86 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 87–102why, one step back
87 ? (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 back
103 }
104 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 106–108why, one step back
106}
107108function finalizeProperty(

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

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

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

line 120why, one step back
120 parentState &&

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 121why, one step back
121 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

line 132why, one step back
132 } else return

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

lines 133–134why, one step back
133 }
134 // Unchanged draft properties are ignored.

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

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 147why, one step back
147function maybeFreeze(scope: ImmerScope, value: any, deep = false) {

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

line 148why, one step back
148 if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {

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

lines 149–151why, one step back
149 freeze(value, deep)
150 }151}