Skip to content
Myr

proxy.ts

This file

Path

src/proxy.ts

260 lines · 7.0 KiB · TypeScript

Last changed by

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

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–12why, one step back
11 Drafted,
12 AnyObject,

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

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

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

line 15why, one step back
15 ImmerScope,

Small code cleanupMichel Weststratebefore adoption9217809

line 16why, one step back
16 DRAFT_STATE,

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 17–19why, one step back
17 createProxy,
18 ProxyTypeProxyObject,19 ProxyTypeProxyArray

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

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 22why, one step back
22

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 24why, one step back
24 assigned_: {

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 32why, one step back
32}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 33why, one step back
33

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

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 35why, one step back
35 type_: typeof ProxyTypeProxyObject

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 36–38why, one step back
36 base_: AnyObject
37 copy_: AnyObject | null38 draft_: Drafted<AnyObject, ProxyObjectState>

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

line 39why, one step back
39}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 40why, one step back
40

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

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 42why, one step back
42 type_: typeof ProxyTypeProxyArray

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 43–45why, one step back
43 base_: AnyArray
44 copy_: AnyArray | null45 draft_: Drafted<AnyArray, ProxyArrayState>

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 61why, one step back
61 type_: isArray ? ProxyTypeProxyArray : (ProxyTypeProxyObject as any),

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 94–97why, one step back
94 target = [state] as any
95 traps = arrayTraps96 }97

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 104–108why, one step back
104}
105106/**107 * Object drafts108 */

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 117–119why, one step back
117 }
118119 const value = latest(state)[prop]

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 121–124why, one step back
121 return value
122 }123124 // Check for existing draft in modified state.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 157why, one step back
157 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Small code cleanupMichel Weststratebefore adoption9217809

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 172why, one step back
172 }

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 185why, one step back
185 state.type_ !== ProxyTypeProxyArray || prop !== "length"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 186–189why, one step back
186 }
187 return desc188 },189 defineProperty() {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 197–203why, one step back
197 }
198}199200/**201 * Array drafts202 */203

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 217why, one step back
217 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 218–225why, one step back
218 return objectTraps.set!.call(this, state[0], prop, value, state[0])
219}220221/**222 * Map drafts223 */224225// Access a property without creating an Immer draft.

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 227–234why, one step back
227 const state = draft[DRAFT_STATE]
228 const desc = Reflect.getOwnPropertyDescriptor(229 state ? latest(state) : draft,230 prop231 )232 return desc && desc.value233}234

Small code cleanupMichel Weststratebefore adoption9217809

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 238why, one step back
238 if (

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 239–240why, one step back
239 state.type_ === ProxyTypeProxyObject ||
240 state.type_ === ProxyTypeProxyArray

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

line 241why, one step back
241 ) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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