Skip to content
Myr

finalize.ts

This file

Path

src/finalize.ts

159 lines · 4.4 KiB · TypeScript

Last changed by

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

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"

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

line 25why, one step back
25import {Archtype} from "./types-internal"

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

line 26why, one step back
26

Small improvementsMichel Weststratebefore adoption712af5c

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Small improvementsMichel Weststratebefore adoption712af5c

line 30why, one step back
30 willFinalize(scope, result, isReplaced)

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Small improvementsMichel Weststratebefore adoption712af5c

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

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

line 40why, one step back
40 }

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Small improvementsMichel Weststratebefore adoption712af5c

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 77why, one step back
77 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

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

Small improvementsMichel Weststratebefore adoption712af5c

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 95why, one step back
95function finalizeTree(rootScope: ImmerScope, value: any, rootPath?: PatchPath) {

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

lines 96–97why, one step back
96 // Don't recurse in tho recursive data structures
97 if (Object.isFrozen(value)) return value

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 98why, one step back
98 const state: ImmerState = value[DRAFT_STATE]

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

lines 99–100why, one step back
99 if (state) {
100 if (

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 101–102why, one step back
101 state.type_ === ProxyType.ES5Object ||
102 state.type_ === ProxyType.ES5Array

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 106why, one step back
106 }

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

line 108why, one step back
108 }

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

line 111why, one step back
111 )

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 112why, one step back
112 return value

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

lines 113–115why, one step back
113}
114115function finalizeProperty(

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 116–118why, one step back
116 rootScope: ImmerScope,
117 parentState: undefined | ImmerState,118 targetObject: any,

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

lines 119–122why, one step back
119 prop: string | number,
120 childValue: any,121 rootPath?: PatchPath122) {

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

lines 124–126why, one step back
124 if (isDraft(childValue)) {
125 const path =126 rootPath &&

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

lines 127–128why, one step back
127 parentState &&
128 parentState!.type_ !== ProxyType.Set && // Set objects are atomic since they have no keys.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 129why, one step back
129 !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 130–132why, one step back
130 ? rootPath!.concat(prop)
131 : undefined132 // Drafts owned by `scope` are finalized here.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

line 139why, one step back
139 }

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

line 140why, one step back
140 return

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

lines 141–142why, one step back
141 }
142 // Unchanged draft properties are ignored.

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

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

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

lines 144–146why, one step back
144 return
145 }146 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

lines 147–148why, one step back
147 if (isDraftable(childValue)) {
148 finalizeTree(rootScope, childValue)

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

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

Initial finalize cleanupMichel Weststratebefore adoption67ab3ab

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

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

lines 152–154why, one step back
152 }
153}154

Small improvementsMichel Weststratebefore adoption712af5c

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

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

lines 157–159why, one step back
157 freeze(value, deep)
158 }159}