Skip to content
Myr

finalize.ts

This file

Path

src/finalize.ts

207 lines · 5.2 KiB · TypeScript

Last changed by

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

lines 25–26why, one step back
25
26export function processResult(immer: Immer, 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 code cleanupMichel Weststratebefore adoption9217809

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 38why, one step back
38 if (!scope.parent_) maybeFreeze(immer, 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–55why, one step back
50 })
51 }52 } else {53 // Finalize the base draft.54 result = finalize(immer, baseDraft, scope, [])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–68why, one step back
59 }
60 return result !== NOTHING ? result : undefined61}6263function finalize(64 immer: Immer,65 draft: Drafted,66 scope: ImmerScope,67 path?: PatchPath68) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 70–74why, one step back
70 if (!state) {
71 if (Object.isFrozen(draft)) return draft72 return finalizeTree(immer, draft, scope)73 }74 // Never finalize drafts owned by another scope.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 76–77why, one step back
76 return draft
77 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 78–80why, one step back
78 if (!state.modified_) {
79 maybeFreeze(immer, state.base_, true)80 return state.base_

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

line 81why, one step back
81 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 82–84why, one step back
82 if (!state.finalized_) {
83 state.finalized_ = true84 finalizeTree(immer, state.draft_, scope, path)

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

lines 85–86why, one step back
85
86 // We cannot really delete anything inside of a Set. We can only replace the whole Set.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 87why, one step back
87 if (immer.onDelete && state.type_ !== ProxyType.Set) {

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

lines 88–89why, one step back
88 // The `assigned` object is unreliable with ES5 drafts.
89 if (immer.useProxies) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 90–91why, one step back
90 const {assigned_} = state
91 each(assigned_ as any, (prop, exists) => {

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

lines 92–94why, one step back
92 if (!exists) immer.onDelete!(state, prop as any)
93 })94 } else {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 95–97why, one step back
95 const {base_, copy_} = state
96 each(base_, prop => {97 if (!has(copy_, prop)) immer.onDelete!(state, prop as any)

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

lines 98–106why, one step back
98 })
99 }100 }101 if (immer.onCopy) {102 immer.onCopy(state)103 }104105 // At this point, all descendants of `state.copy` have been finalized,106 // so we can be sure that `scope.canAutoFreeze` is accurate.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 107–108why, one step back
107 if (immer.autoFreeze && scope.canAutoFreeze_) {
108 freeze(state.copy_, false)

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

lines 109–110why, one step back
109 }
110

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 113–114why, one step back
113 }
114 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 116–123why, one step back
116}
117118function finalizeTree(119 immer: Immer,120 root: Drafted,121 scope: ImmerScope,122 rootPath?: PatchPath123) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 124why, one step back
124 const state: ImmerState = root[DRAFT_STATE]

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

lines 125–126why, one step back
125 if (state) {
126 if (

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 127–128why, one step back
127 state.type_ === ProxyType.ES5Object ||
128 state.type_ === ProxyType.ES5Array

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 132why, one step back
132 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 133why, one step back
133 root = state.copy_

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

lines 134–150why, one step back
134 }
135 each(root, (key, value) =>136 finalizeProperty(immer, scope, root, state, root, key, value, rootPath)137 )138 return root139}140141function finalizeProperty(142 immer: Immer,143 scope: ImmerScope,144 root: Drafted,145 rootState: ImmerState,146 parentValue: Drafted,147 prop: string | number,148 childValue: any,149 rootPath?: PatchPath150) {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

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

lines 152–161why, one step back
152
153 // In the `finalizeTree` method, only the `root` object may be a draft.154 const isDraftProp = !!rootState && parentValue === root155 const isSetMember = isSet(parentValue)156157 if (isDraft(childValue)) {158 const path =159 rootPath &&160 isDraftProp &&161 !isSetMember && // Set objects are atomic since they have no keys.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 162why, one step back
162 !has((rootState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.

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

lines 163–171why, one step back
163 ? rootPath!.concat(prop)
164 : undefined165166 // Drafts owned by `scope` are finalized here.167 childValue = finalize(immer, childValue, scope, path)168 set(parentValue, prop, childValue)169170 // Drafts from another scope must prevent auto-freezing.171 if (isDraft(childValue)) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 172why, one step back
172 scope.canAutoFreeze_ = false

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

lines 173–175why, one step back
173 }
174 }175 // Unchanged draft properties are ignored.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 176why, one step back
176 else if (isDraftProp && is(childValue, get(rootState.base_, prop))) {

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

lines 177–181why, one step back
177 return
178 }179 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.180 // TODO: the recursion over here looks weird, shouldn't non-draft stuff have it's own recursion?181 // especially the passing on of root and rootState doesn't make sense...

fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524Michel Weststratebefore adoptionacc5e54

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

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

lines 183–194why, one step back
183 each(childValue, (key, grandChild) =>
184 finalizeProperty(185 immer,186 scope,187 root,188 rootState,189 childValue,190 key,191 grandChild,192 rootPath193 )194 )

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 195why, one step back
195 if (!scope.parent_) maybeFreeze(immer, childValue)

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

lines 196–207why, one step back
196 }
197198 if (isDraftProp && immer.onAssign && !isSetMember) {199 immer.onAssign(rootState, prop, childValue)200 }201}202203export function maybeFreeze(immer: Immer, value: any, deep = false) {204 if (immer.autoFreeze && !isDraft(value)) {205 freeze(value, deep)206 }207}