Skip to content
Myr

finalize.ts

This file

Path

src/finalize.ts

162 lines · 5.0 KiB · TypeScript

Last changed by

Optimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8

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

lines 9–10why, one step back
9 freeze,
10 shallowCopy,

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

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

Small code cleanupMichel Weststratebefore adoption9217809

line 16why, one step back
16 get,

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 17–18why, one step back
17 ProxyTypeES5Object,
18 ProxyTypeES5Array,

scramble plugin names, -56Michel Weststratebefore adoption037b474

lines 19–20why, one step back
19 ProxyTypeSet,
20 getPlugin

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

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

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

line 23why, one step back
23

Small improvementsMichel Weststratebefore adoption712af5c

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

Optimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8

line 25why, one step back
25 scope.unfinalizedDrafts_ = scope.drafts_.length

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

scramble plugin names, -56Michel Weststratebefore adoption037b474

lines 28–29why, one step back
28 if (!scope.immer_.useProxies_)
29 getPlugin("es5").willFinalizeES5_(scope, result, isReplaced)

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 33why, one step back
33 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 34–36why, one step back
34 }
35 if (isDraftable(result)) {36 // Finalize the result in case it contains (or is) a subset of the draft.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 37why, one step back
37 result = finalize(scope, result)

Small improvementsMichel Weststratebefore adoption712af5c

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

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

line 39why, one step back
39 }

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 40why, one step back
40 // TODO: move to plugin?

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 41–42why, one step back
41 if (scope.patches_) {
42 scope.patches_.push({

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

lines 43–46why, one step back
43 op: "replace",
44 path: [],45 value: result46 })

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 48–49why, one step back
48 op: "replace",
49 path: [],

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

line 56why, one step back
56 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 64–69why, one step back
64function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {
65 // Don't recurse in tho recursive data structures66 if (Object.isFrozen(value)) return value6768 const state: ImmerState = value[DRAFT_STATE]69 // A plain object, might need freezing, might contain drafts

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

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

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

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 82why, one step back
82 }

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Optimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8

line 86why, one step back
86 state.scope_.unfinalizedDrafts_--

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 87–88why, one step back
87 const result =
88 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 89why, one step back
89 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 90–99why, one step back
90 ? (state.copy_ = shallowCopy(state.draft_, true))
91 : state.copy_92 // finalize all children of the copy93 each(result as any, (key, childValue) =>94 finalizeProperty(rootScope, state, result, key, childValue, path)95 )96 // everything inside is frozen, we can freeze here97 maybeFreeze(rootScope, result, false)98 // first time finalizing, let's create those patches99 if (path && rootScope.patches_) {

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 100why, one step back
100 getPlugin("patches").generatePatches_(

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 101–105why, one step back
101 state,
102 path,103 rootScope.patches_,104 rootScope.inversePatches_!105 )

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

lines 106–107why, one step back
106 }
107 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 109–111why, one step back
109}
110111function finalizeProperty(

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 112–114why, one step back
112 rootScope: ImmerScope,
113 parentState: undefined | ImmerState,114 targetObject: any,

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

lines 115–118why, one step back
115 prop: string | number,
116 childValue: any,117 rootPath?: PatchPath118) {

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

lines 120–122why, one step back
120 if (isDraft(childValue)) {
121 const path =122 rootPath &&

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

line 123why, one step back
123 parentState &&

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 125why, one step back
125 !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 126–128why, one step back
126 ? rootPath!.concat(prop)
127 : undefined128 // Drafts owned by `scope` are finalized here.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 129–130why, one step back
129 const res = finalize(rootScope, childValue, path)
130 set(targetObject, prop, res)

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

lines 131–132why, one step back
131 // Drafts from another scope must prevented to be frozen
132 // if we got a draft back from finalize, we're in a nested produce and shouldn't freeze

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

line 135why, one step back
135 } else return

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

lines 136–137why, one step back
136 }
137 // Unchanged draft properties are ignored.

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

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

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

lines 139–141why, one step back
139 return
140 }141 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Optimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8

lines 143–150why, one step back
143 if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
144 // optimization: if an object is not a draft, and we don't have to145 // deepfreeze everything, and we are sure that no drafts are left in the remaining object146 // cause we saw and finalized all drafts already; we can stop visiting the rest of the tree.147 // This benefits especially adding large data tree's without further processing.148 // See add-data.js perf test149 return150 }

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 153–154why, one step back
153 if (!parentState || !parentState.scope_.parent_)
154 maybeFreeze(rootScope, childValue)

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

lines 155–157why, one step back
155 }
156}157

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

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

lines 160–162why, one step back
160 freeze(value, deep)
161 }162}