Skip to content
Myr

proxy.ts

This file

Path

src/proxy.ts

258 lines · 6.9 KiB · TypeScript

Last changed by

Small code cleanupMichel Weststratebefore adoption9217809

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"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 20why, one step back
20

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

lines 21–30why, one step back
21interface ProxyBaseState extends ImmerBaseState {
22 assigned: {23 [property: string]: boolean24 }25 parent?: ImmerState26 drafts?: {27 [property: string]: Drafted<any, any>28 }29 revoke(): void30}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 31why, one step back
31

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

lines 32–37why, one step back
32export interface ProxyObjectState extends ProxyBaseState {
33 type: ProxyType.ProxyObject34 base: AnyObject35 copy: AnyObject | null36 draft: Drafted<AnyObject, ProxyObjectState>37}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 38why, one step back
38

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

lines 39–43why, one step back
39export interface ProxyArrayState extends ProxyBaseState {
40 type: ProxyType.ProxyArray41 base: AnyArray42 copy: AnyArray | null43 draft: Drafted<AnyArray, ProxyArrayState>

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

lines 79–80why, one step back
79 revoke: null as any,
80 isManual: false

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 92–95why, one step back
92 target = [state] as any
93 traps = arrayTraps94 }95

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 100why, one step back
100 state.revoke = revoke

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 102–106why, one step back
102}
103104/**105 * Object drafts106 */

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 108–113why, one step back
108 get(state, prop) {
109 if (prop === DRAFT_STATE) return state110 let {drafts} = state111112 // Check for existing draft in unmodified state.113 if (!state.modified && has(drafts, prop)) {

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 115–126why, one step back
115 }
116117 const value = latest(state)[prop]118 if (state.finalized || !isDraftable(value)) {119 return value120 }121122 // Check for existing draft in modified state.123 if (state.modified) {124 // Assigned values are never drafted. This catches any drafts we created, too.125 if (value !== peek(state.base, prop)) return value126 // Store drafts on the copy (when one exists).

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 180–197why, one step back
180 }
181 return desc182 },183 defineProperty() {184 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore185 },186 getPrototypeOf(state) {187 return Object.getPrototypeOf(state.base)188 },189 setPrototypeOf() {190 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore191 }192}193194/**195 * Array drafts196 */197

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 201–223why, one step back
201 arrayTraps[key] = function() {
202 arguments[0] = arguments[0][0]203 return fn.apply(this, arguments)204 }205})206arrayTraps.deleteProperty = function(state, prop) {207 if (isNaN(parseInt(prop as any))) {208 throw new Error("Immer only supports deleting array indices") // prettier-ignore209 }210 return objectTraps.deleteProperty!.call(this, state[0], prop)211}212arrayTraps.set = function(state, prop, value) {213 if (prop !== "length" && isNaN(parseInt(prop as any))) {214 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore215 }216 return objectTraps.set!.call(this, state[0], prop, value, state[0])217}218219/**220 * Map drafts221 */222223// Access a property without creating an Immer draft.

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 225–232why, one step back
225 const state = draft[DRAFT_STATE]
226 const desc = Reflect.getOwnPropertyDescriptor(227 state ? latest(state) : draft,228 prop229 )230 return desc && desc.value231}232

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

lines 236–245why, one step back
236 if (
237 state.type === ProxyType.ProxyObject ||238 state.type === ProxyType.ProxyArray239 ) {240 const copy = (state.copy = shallowCopy(state.base))241 each(state.drafts!, (key, value) => {242 // @ts-ignore243 copy[key] = value244 })245 state.drafts = undefined

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

Small code cleanupMichel Weststratebefore adoption9217809

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

lines 254–256why, one step back
254function prepareCopy(state: ProxyState) {
255 if (!state.copy) {256 state.copy = shallowCopy(state.base)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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