Skip to content
Myr

proxy.ts

This file

Path

src/proxy.ts

259 lines · 7.0 KiB · TypeScript

Last changed by

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

History of this file

Take it

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 1–7why, one step back
1"use strict"
2import {3 each,4 has,5 is,6 isDraftable,7 shallowCopy,

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

lines 8–9why, one step back
8 latest,
9 ImmerBaseState,

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

line 10why, one step back
10 ImmerState,

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

lines 11–13why, one step back
11 Drafted,
12 ProxyType,13 AnyObject,

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

lines 14–15why, one step back
14 AnyArray,
15 Objectish,

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

line 16why, one step back
16 ImmerScope,

Small code cleanupMichel Weststratebefore adoption9217809

lines 17–18why, one step back
17 DRAFT_STATE,
18 createProxy

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

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 21why, one step back
21

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

line 22why, one step back
22interface ProxyBaseState extends ImmerBaseState {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 23why, one step back
23 assigned_: {

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

lines 24–25why, one step back
24 [property: string]: boolean
25 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 26–27why, one step back
26 parent_?: ImmerState
27 drafts_?: {

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

lines 28–29why, one step back
28 [property: string]: Drafted<any, any>
29 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 30why, one step back
30 revoke_(): void

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

line 31why, one step back
31}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 32why, one step back
32

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

line 33why, one step back
33export interface ProxyObjectState extends ProxyBaseState {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 34–37why, one step back
34 type_: ProxyType.ProxyObject
35 base_: AnyObject36 copy_: AnyObject | null37 draft_: Drafted<AnyObject, ProxyObjectState>

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

line 38why, one step back
38}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 39why, one step back
39

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

line 40why, one step back
40export interface ProxyArrayState extends ProxyBaseState {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 41–44why, one step back
41 type_: ProxyType.ProxyArray
42 base_: AnyArray43 copy_: AnyArray | null44 draft_: Drafted<AnyArray, ProxyArrayState>

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 45–46why, one step back
45}
46

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

lines 47–48why, one step back
47type ProxyState = ProxyObjectState | ProxyArrayState
48

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 49–53why, one step back
49/**
50 * Returns a new draft of the `base` object.51 *52 * The second argument is the parent draft-state (used internally).53 */

Small code cleanupMichel Weststratebefore adoption9217809

line 54why, one step back
54export function createProxyProxy<T extends Objectish>(

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 55why, one step back
55 base: T,

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

lines 56–59why, one step back
56 parent?: ImmerState
57): Drafted<T, ProxyState> {58 const isArray = Array.isArray(base)59 const state: ProxyState = {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 60why, one step back
60 type_: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 61why, one step back
61 // Track which produce call this is associated with.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 62why, one step back
62 scope_: parent ? parent.scope_ : ImmerScope.current_!,

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 63why, one step back
63 // True for both shallow and deep changes.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 64why, one step back
64 modified_: false,

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 65why, one step back
65 // Used during finalization.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 66why, one step back
66 finalized_: false,

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 67why, one step back
67 // Track which properties have been assigned (true) or deleted (false).

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 68why, one step back
68 assigned_: {},

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 69why, one step back
69 // The parent draft state.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 70why, one step back
70 parent_: parent,

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 71why, one step back
71 // The base state.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 72why, one step back
72 base_: base,

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 73why, one step back
73 // The base proxy.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 74why, one step back
74 draft_: null as any, // set below

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 75why, one step back
75 // Any property proxies.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 76why, one step back
76 drafts_: {},

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 77why, one step back
77 // The base copy with any updated values.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 78why, one step back
78 copy_: null,

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 79why, one step back
79 // Called by the `produce` function.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 80–81why, one step back
80 revoke_: null as any,
81 isManual_: false

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 82–91why, one step back
82 }
8384 // the traps must target something, a bit like the 'real' base.85 // but also, we need to be able to determine from the target what the relevant state is86 // (to avoid creating traps per instance to capture the state in closure,87 // and to avoid creating weird hidden properties as well)88 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)89 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb90 let target: T = state as any91 let traps: ProxyHandler<object | Array<any>> = objectTraps

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

line 92why, one step back
92 if (isArray) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 93–96why, one step back
93 target = [state] as any
94 traps = arrayTraps95 }96

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

lines 97–98why, one step back
97 // TODO: optimization: might be faster, cheaper if we created a non-revocable proxy
98 // and administrate revoking ourselves

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 99why, one step back
99 const {revoke, proxy} = Proxy.revocable(target, traps)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 100–101why, one step back
100 state.draft_ = proxy as any
101 state.revoke_ = revoke

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

line 102why, one step back
102 return proxy as any

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 103–107why, one step back
103}
104105/**106 * Object drafts107 */

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

line 108why, one step back
108const objectTraps: ProxyHandler<ProxyState> = {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 109–110why, one step back
109 get(state, prop) {
110 if (prop === DRAFT_STATE) return state

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 111why, one step back
111 let {drafts_: drafts} = state

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 112–113why, one step back
112
113 // Check for existing draft in unmodified state.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 114why, one step back
114 if (!state.modified_ && has(drafts, prop)) {

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

line 115why, one step back
115 return drafts![prop as any]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 116–118why, one step back
116 }
117118 const value = latest(state)[prop]

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 119why, one step back
119 if (state.finalized_ || !isDraftable(value)) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 120–123why, one step back
120 return value
121 }122123 // Check for existing draft in modified state.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 125why, one step back
125 // Assigned values are never drafted. This catches any drafts we created, too.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 126why, one step back
126 if (value !== peek(state.base_, prop)) return value

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 127why, one step back
127 // Store drafts on the copy (when one exists).

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

line 128why, one step back
128 // @ts-ignore

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 129why, one step back
129 drafts = state.copy_

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 130–131why, one step back
130 }
131

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 132–136why, one step back
132 return (drafts![prop as any] = createProxy(
133 state.scope_.immer_,134 value,135 state136 ))

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 137–143why, one step back
137 },
138 has(state, prop) {139 return prop in latest(state)140 },141 ownKeys(state) {142 return Reflect.ownKeys(latest(state))143 },

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

line 144why, one step back
144 set(state, prop: string /* strictly not, but helps TS */, value) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 145–146why, one step back
145 if (!state.modified_) {
146 const baseValue = peek(state.base_, prop)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 147–150why, one step back
147 // Optimize based on value's truthiness. Truthy values are guaranteed to
148 // never be undefined, so we can avoid the `in` operator. Lastly, truthy149 // values may be drafts, but falsy values are never drafts.150 const isUnchanged = value

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 151–152why, one step back
151 ? is(baseValue, value) || value === state.drafts_![prop]
152 : is(baseValue, value) && prop in state.base_

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 153why, one step back
153 if (isUnchanged) return true

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

line 154why, one step back
154 prepareCopy(state)

Small code cleanupMichel Weststratebefore adoption9217809

line 155why, one step back
155 markChangedProxy(state)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 156why, one step back
156 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 157why, one step back
157 state.assigned_[prop] = true

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

line 158why, one step back
158 // @ts-ignore

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 159why, one step back
159 state.copy_![prop] = value

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 160–161why, one step back
160 return true
161 },

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

line 162why, one step back
162 deleteProperty(state, prop: string) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 163why, one step back
163 // The `undefined` check is a fast path for pre-existing keys.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 164–165why, one step back
164 if (peek(state.base_, prop) !== undefined || prop in state.base_) {
165 state.assigned_[prop] = false

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

line 166why, one step back
166 prepareCopy(state)

Small code cleanupMichel Weststratebefore adoption9217809

line 167why, one step back
167 markChangedProxy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 168why, one step back
168 } else if (state.assigned_[prop]) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 169why, one step back
169 // if an originally not assigned property was deleted

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 170why, one step back
170 delete state.assigned_[prop]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 171why, one step back
171 }

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

line 172why, one step back
172 // @ts-ignore

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 173why, one step back
173 if (state.copy_) delete state.copy_[prop]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 174–182why, one step back
174 return true
175 },176 // Note: We never coerce `desc.value` into an Immer draft, because we can't make177 // the same guarantee in ES5 mode.178 getOwnPropertyDescriptor(state, prop) {179 const owner = latest(state)180 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)181 if (desc) {182 desc.writable = true

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

line 183why, one step back
183 desc.configurable =

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 184why, one step back
184 state.type_ !== ProxyType.ProxyArray || prop !== "length"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 185–188why, one step back
185 }
186 return desc187 },188 defineProperty() {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 189why, one step back
189 invariant(false, "Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 190–191why, one step back
190 },
191 getPrototypeOf(state) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 192why, one step back
192 return Object.getPrototypeOf(state.base_)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 193–194why, one step back
193 },
194 setPrototypeOf() {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 195why, one step back
195 invariant(false, "Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 196–202why, one step back
196 }
197}198199/**200 * Array drafts201 */202

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

line 203why, one step back
203const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 204why, one step back
204each(objectTraps, (key, fn) => {

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

line 205why, one step back
205 // @ts-ignore

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 206–211why, one step back
206 arrayTraps[key] = function() {
207 arguments[0] = arguments[0][0]208 return fn.apply(this, arguments)209 }210})211arrayTraps.deleteProperty = function(state, prop) {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 212why, one step back
212 invariant(!isNaN(parseInt(prop as any)), "Immer only supports deleting array indices") // prettier-ignore

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 213–215why, one step back
213 return objectTraps.deleteProperty!.call(this, state[0], prop)
214}215arrayTraps.set = function(state, prop, value) {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 216why, one step back
216 invariant(prop === "length" || !isNaN(parseInt(prop as any)), "Immer only supports setting array indices and the 'length' property") // prettier-ignore

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 217–224why, one step back
217 return objectTraps.set!.call(this, state[0], prop, value, state[0])
218}219220/**221 * Map drafts222 */223224// Access a property without creating an Immer draft.

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

line 225why, one step back
225function peek(draft: Drafted, prop: PropertyKey): any {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 226–233why, one step back
226 const state = draft[DRAFT_STATE]
227 const desc = Reflect.getOwnPropertyDescriptor(228 state ? latest(state) : draft,229 prop230 )231 return desc && desc.value232}233

Small code cleanupMichel Weststratebefore adoption9217809

line 234why, one step back
234export function markChangedProxy(state: ImmerState) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 235–236why, one step back
235 if (!state.modified_) {
236 state.modified_ = true

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

line 237why, one step back
237 if (

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 238–239why, one step back
238 state.type_ === ProxyType.ProxyObject ||
239 state.type_ === ProxyType.ProxyArray

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

line 240why, one step back
240 ) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 241–242why, one step back
241 const copy = (state.copy_ = shallowCopy(state.base_))
242 each(state.drafts_!, (key, value) => {

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

lines 243–245why, one step back
243 // @ts-ignore
244 copy[key] = value245 })

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 246why, one step back
246 state.drafts_ = undefined

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 247–248why, one step back
247 }
248

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 249–250why, one step back
249 if (state.parent_) {
250 markChangedProxy(state.parent_)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 251–254why, one step back
251 }
252 }253}254

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

line 255why, one step back
255function prepareCopy(state: ProxyState) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 256–257why, one step back
256 if (!state.copy_) {
257 state.copy_ = shallowCopy(state.base_)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 258–259why, one step back
258 }
259}