Skip to content
Myr

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 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) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

scramble plugin names, -56Michel Weststratebefore adoption037b474

lines 27–28why, one step back
27 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 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 }

scramble plugin names, -56Michel Weststratebefore adoption037b474

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

line 55why, one step back
55 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 56–58why, one step back
56 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 back
59 }
60 return result !== NOTHING ? result : undefined61}62

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

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

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

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

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 81why, one step back
81 }

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 88–97why, one step back
88 ? (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 back
98 getPlugin("patches").generatePatches_(

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

lines 99–103why, one step back
99 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 back
104 }
105 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 107–109why, one step back
107}
108109function finalizeProperty(

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

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

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

line 121why, one step back
121 parentState &&

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

line 133why, one step back
133 } else return

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

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

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

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

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

Cleanup finalize, -57Michel Weststratebefore adoption530d81b

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

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

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