Skip to content
Myr

proxy.ts

This file

Path

src/proxy.ts

255 lines · 6.9 KiB · TypeScript

Last changed by

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

lines 22–31why, one step back
22interface ProxyBaseState extends ImmerBaseState {
23 assigned: {24 [property: string]: boolean25 }26 parent?: ImmerState27 drafts?: {28 [property: string]: Drafted<any, any>29 }30 revoke(): void31}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 32why, one step back
32

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

lines 33–38why, one step back
33export interface ProxyObjectState extends ProxyBaseState {
34 type: ProxyType.ProxyObject35 base: AnyObject36 copy: AnyObject | null37 draft: Drafted<AnyObject, ProxyObjectState>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

lines 40–44why, one step back
40export interface ProxyArrayState extends ProxyBaseState {
41 type: ProxyType.ProxyArray42 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–60why, one step back
56 parent?: ImmerState
57): Drafted<T, ProxyState> {58 const isArray = Array.isArray(base)59 const state: ProxyState = {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.

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

line 62why, one step back
62 scope: parent ? parent.scope : ImmerScope.current!,

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 63–73why, one step back
63 // True for both shallow and deep changes.
64 modified: false,65 // Used during finalization.66 finalized: false,67 // Track which properties have been assigned (true) or deleted (false).68 assigned: {},69 // The parent draft state.70 parent,71 // The base state.72 base,73 // The base proxy.

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 75–79why, one step back
75 // Any property proxies.
76 drafts: {},77 // The base copy with any updated values.78 copy: null,79 // Called by the `produce` function.

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

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)

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

line 100why, one step back
100 state.draft = proxy as any

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 101why, one step back
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–114why, one step back
109 get(state, prop) {
110 if (prop === DRAFT_STATE) return state111 let {drafts} = state112113 // Check for existing draft in unmodified state.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–127why, one step back
116 }
117118 const value = latest(state)[prop]119 if (state.finalized || !isDraftable(value)) {120 return value121 }122123 // Check for existing draft in modified state.124 if (state.modified) {125 // Assigned values are never drafted. This catches any drafts we created, too.126 if (value !== peek(state.base, prop)) return value127 // 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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 129–131why, one step back
129 drafts = state.copy
130 }131

Small code cleanupMichel Weststratebefore adoption9217809

line 132why, one step back
132 return (drafts![prop as any] = createProxy(state.scope.immer, value, state))

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 133–139why, one step back
133 },
134 has(state, prop) {135 return prop in latest(state)136 },137 ownKeys(state) {138 return Reflect.ownKeys(latest(state))139 },

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 141–146why, one step back
141 if (!state.modified) {
142 const baseValue = peek(state.base, prop)143 // Optimize based on value's truthiness. Truthy values are guaranteed to144 // never be undefined, so we can avoid the `in` operator. Lastly, truthy145 // values may be drafts, but falsy values are never drafts.146 const isUnchanged = value

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

line 147why, one step back
147 ? is(baseValue, value) || value === state.drafts![prop]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 148–149why, one step back
148 : is(baseValue, value) && prop in state.base
149 if (isUnchanged) return true

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

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

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 152–153why, one step back
152 }
153 state.assigned[prop] = true

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

lines 154–155why, one step back
154 // @ts-ignore
155 state.copy![prop] = value

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 156–157why, one step back
156 return true
157 },

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 159–161why, one step back
159 // The `undefined` check is a fast path for pre-existing keys.
160 if (peek(state.base, prop) !== undefined || prop in state.base) {161 state.assigned[prop] = false

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

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

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 164–167why, one step back
164 } else if (state.assigned[prop]) {
165 // if an originally not assigned property was deleted166 delete state.assigned[prop]167 }

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 169–178why, one step back
169 if (state.copy) delete state.copy[prop]
170 return true171 },172 // Note: We never coerce `desc.value` into an Immer draft, because we can't make173 // the same guarantee in ES5 mode.174 getOwnPropertyDescriptor(state, prop) {175 const owner = latest(state)176 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)177 if (desc) {178 desc.writable = true

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

lines 179–180why, one step back
179 desc.configurable =
180 state.type !== ProxyType.ProxyArray || prop !== "length"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 181–184why, one step back
181 }
182 return desc183 },184 defineProperty() {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 186–190why, one step back
186 },
187 getPrototypeOf(state) {188 return Object.getPrototypeOf(state.base)189 },190 setPrototypeOf() {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 192–198why, one step back
192 }
193}194195/**196 * Array drafts197 */198

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 202–207why, one step back
202 arrayTraps[key] = function() {
203 arguments[0] = arguments[0][0]204 return fn.apply(this, arguments)205 }206})207arrayTraps.deleteProperty = function(state, prop) {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 212why, one step back
212 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 213–220why, one step back
213 return objectTraps.set!.call(this, state[0], prop, value, state[0])
214}215216/**217 * Map drafts218 */219220// Access a property without creating an Immer draft.

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 222–229why, one step back
222 const state = draft[DRAFT_STATE]
223 const desc = Reflect.getOwnPropertyDescriptor(224 state ? latest(state) : draft,225 prop226 )227 return desc && desc.value228}229

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 231–232why, one step back
231 if (!state.modified) {
232 state.modified = true

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

lines 233–242why, one step back
233 if (
234 state.type === ProxyType.ProxyObject ||235 state.type === ProxyType.ProxyArray236 ) {237 const copy = (state.copy = shallowCopy(state.base))238 each(state.drafts!, (key, value) => {239 // @ts-ignore240 copy[key] = value241 })242 state.drafts = undefined

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 243–244why, one step back
243 }
244

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

line 245why, one step back
245 if (state.parent) {

Small code cleanupMichel Weststratebefore adoption9217809

line 246why, one step back
246 markChangedProxy(state.parent)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

lines 251–253why, one step back
251function prepareCopy(state: ProxyState) {
252 if (!state.copy) {253 state.copy = shallowCopy(state.base)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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