Skip to content
Myr

finalize.ts

This file

Path

src/finalize.ts

176 lines · 4.7 KiB · TypeScript

Last changed by

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

lines 2–3why, one step back
2 Immer,
3 ImmerScope,

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

line 4why, one step back
4 DRAFT_STATE,

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

line 5why, one step back
5 isDraftable,

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

line 6why, one step back
6 NOTHING,

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

lines 7–11why, one step back
7 Drafted,
8 PatchPath,9 ProxyType,10 each,11 has,

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

line 12why, one step back
12 freeze,

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

line 13why, one step back
13 generatePatches,

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

line 14why, one step back
14 shallowCopy,

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

lines 15–20why, one step back
15 ImmerState,
16 isSet,17 isDraft,18 SetState,19 set,20 is,

Small code cleanupMichel Weststratebefore adoption9217809

lines 21–22why, one step back
21 get,
22 willFinalize

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

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

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

line 25why, one step back
25

Small improvementsMichel Weststratebefore adoption712af5c

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Small improvementsMichel Weststratebefore adoption712af5c

line 29why, one step back
29 willFinalize(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 }

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 63why, one step back
63function finalize(scope: ImmerScope, draft: Drafted, path?: PatchPath) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 64why, one step back
64 const state: ImmerState = draft[DRAFT_STATE]

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

lines 65–66why, one step back
65 if (!state) {
66 if (Object.isFrozen(draft)) return draft

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 67why, one step back
67 return finalizeTree(scope, draft)

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 70why, one step back
70 if (state.scope_ !== scope) {

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

lines 71–72why, one step back
71 return draft
72 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Small improvementsMichel Weststratebefore adoption712af5c

line 74why, one step back
74 maybeFreeze(scope, state.base_, true)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 76why, one step back
76 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 79why, one step back
79 finalizeTree(scope, draft, path)

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

lines 80–82why, one step back
80
81 // At this point, all descendants of `state.copy` have been finalized,82 // so we can be sure that `scope.canAutoFreeze` is accurate.

Small improvementsMichel Weststratebefore adoption712af5c

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 84why, one step back
84 freeze(state.copy_, false)

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

lines 85–86why, one step back
85 }
86

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 87–88why, one step back
87 if (path && scope.patches_) {
88 generatePatches(state, path, scope.patches_, scope.inversePatches_!)

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

lines 89–90why, one step back
89 }
90 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 92–93why, one step back
92}
93

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 94–98why, one step back
94function finalizeTree(rootScope: ImmerScope, value: any, rootPath?: PatchPath) {
95 // TODO: enable96 // if (Object.isFrozen(value))97 // return value;98 const state: ImmerState = value[DRAFT_STATE]

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

line 99why, one step back
99 if (state) {

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 100–102why, one step back
100 // if (rootScope !== state.scope_)
101 // // Drafts from another scope must prevent auto-freezing.102 // state.scope_.canAutoFreeze_ = false;

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

line 103why, one step back
103 if (

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 104–105why, one step back
104 state.type_ === ProxyType.ES5Object ||
105 state.type_ === ProxyType.ES5Array

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

lines 106–107why, one step back
106 ) {
107 // Create the final copy, with added keys and without deleted keys.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 108why, one step back
108 state.copy_ = shallowCopy(state.draft_, true)

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

line 109why, one step back
109 }

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 110why, one step back
110 value = state.copy_

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

line 111why, one step back
111 }

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 112–113why, one step back
112 each(value, (key, childValue) =>
113 finalizeProperty(rootScope, state, value, key, childValue, rootPath)

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

line 114why, one step back
114 )

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 115why, one step back
115 return value

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

lines 116–118why, one step back
116}
117118function finalizeProperty(

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 119–121why, one step back
119 rootScope: ImmerScope,
120 parentState: undefined | ImmerState,121 targetObject: any,

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

lines 122–125why, one step back
122 prop: string | number,
123 childValue: any,124 rootPath?: PatchPath125) {

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

lines 127–128why, one step back
127
128 // In the `finalizeTree` method, only the `root` object may be a draft.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 129–130why, one step back
129 const isDraftProp = !!parentState
130 const isSetMember = isSet(targetObject) // TODO: move inside if

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

line 131why, one step back
131

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 132why, one step back
132 // if the child is a draft, we can build a more precise patch

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

lines 133–137why, one step back
133 if (isDraft(childValue)) {
134 const path =135 rootPath &&136 isDraftProp &&137 !isSetMember && // Set objects are atomic since they have no keys.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 138why, one step back
138 !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 139–142why, one step back
139 ? rootPath!.concat(prop)
140 : undefined141142 // Drafts owned by `scope` are finalized here.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

lines 145–146why, one step back
145
146 // Drafts from another scope must prevent auto-freezing.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 147–150why, one step back
147 // TODO: weird place for this condition
148 // if we got a draft back, we're in a nested thing and shouldn't freese149 if (isDraft(res)) {150 rootScope.canAutoFreeze_ = false

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

line 151why, one step back
151 }

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 152–153why, one step back
152
153 return

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

lines 154–155why, one step back
154 }
155 // Unchanged draft properties are ignored.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

lines 157–158why, one step back
157 return
158 }

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 159why, one step back
159 // TODO: cleanup conditions for reuse?

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

line 160why, one step back
160 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 161–168why, one step back
161 if (isDraftable(childValue)) {
162 finalizeTree(rootScope, childValue)163 // TODO: needed?164 // set(targetObject, prop, res)165 // TODO: drop first condition?166 // TODO: needed?167 if (!parentState || !parentState.scope_.parent_)168 maybeFreeze(rootScope, childValue)

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

lines 169–171why, one step back
169 }
170}171

Small improvementsMichel Weststratebefore adoption712af5c

lines 172–173why, one step back
172export function maybeFreeze(scope: {immer_: Immer}, value: any, deep = false) {
173 if (scope.immer_.autoFreeze_ && !isDraft(value)) {

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

lines 174–176why, one step back
174 freeze(value, deep)
175 }176}