Skip to content
Myr

finalize.ts

This file

Path

src/finalize.ts

184 lines · 4.6 KiB · TypeScript

Last changed by

killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb

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–87why, one step back
85
86 // At this point, all descendants of `state.copy` have been finalized,87 // so we can be sure that `scope.canAutoFreeze` is accurate.

killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb

line 88why, one step back
88 if (immer.autoFreeze_ && scope.canAutoFreeze_) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 94–95why, one step back
94 }
95 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 97–104why, one step back
97}
9899function finalizeTree(100 immer: Immer,101 root: Drafted,102 scope: ImmerScope,103 rootPath?: PatchPath104) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 106–107why, one step back
106 if (state) {
107 if (

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 108–109why, one step back
108 state.type_ === ProxyType.ES5Object ||
109 state.type_ === ProxyType.ES5Array

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 113why, one step back
113 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 115–131why, one step back
115 }
116 each(root, (key, value) =>117 finalizeProperty(immer, scope, root, state, root, key, value, rootPath)118 )119 return root120}121122function finalizeProperty(123 immer: Immer,124 scope: ImmerScope,125 root: Drafted,126 rootState: ImmerState,127 parentValue: Drafted,128 prop: string | number,129 childValue: any,130 rootPath?: PatchPath131) {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

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

lines 133–142why, one step back
133
134 // In the `finalizeTree` method, only the `root` object may be a draft.135 const isDraftProp = !!rootState && parentValue === root136 const isSetMember = isSet(parentValue)137138 if (isDraft(childValue)) {139 const path =140 rootPath &&141 isDraftProp &&142 !isSetMember && // Set objects are atomic since they have no keys.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 143why, one step back
143 !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 144–152why, one step back
144 ? rootPath!.concat(prop)
145 : undefined146147 // Drafts owned by `scope` are finalized here.148 childValue = finalize(immer, childValue, scope, path)149 set(parentValue, prop, childValue)150151 // Drafts from another scope must prevent auto-freezing.152 if (isDraft(childValue)) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 158–162why, one step back
158 return
159 }160 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.161 // TODO: the recursion over here looks weird, shouldn't non-draft stuff have it's own recursion?162 // 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 163why, one step back
163 else if (isDraftable(childValue)) {

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

lines 164–175why, one step back
164 each(childValue, (key, grandChild) =>
165 finalizeProperty(166 immer,167 scope,168 root,169 rootState,170 childValue,171 key,172 grandChild,173 rootPath174 )175 )

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

lines 177–180why, one step back
177 }
178}179180export function maybeFreeze(immer: Immer, value: any, deep = false) {

killed hooks, as they leaked mangled properties. Also -145 bytesMichel Weststratebefore adoptionf1918cb

line 181why, one step back
181 if (immer.autoFreeze_ && !isDraft(value)) {

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

lines 182–184why, one step back
182 freeze(value, deep)
183 }184}