Skip to content
Myr

proxy.ts

This file

Path

src/core/proxy.ts

307 lines · 8.1 KiB · TypeScript

Last changed by

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

History of this file

Take it

The walk stopped after 200 commits: lines older than 6555173 point at it.

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 1–14why, one step back
1import {
2 each,3 has,4 is,5 isDraftable,6 shallowCopy,7 latest,8 ImmerBaseState,9 ImmerState,10 Drafted,11 AnyObject,12 AnyArray,13 Objectish,14 getCurrentScope,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 15why, one step back
15 getPrototypeOf,

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

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

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 19why, one step back
19 ArchType,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 20–26why, one step back
20 ImmerScope,
21 handleCrossReference,22 WRITABLE,23 CONFIGURABLE,24 ENUMERABLE,25 VALUE,26 isArray

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 27–34why, one step back
27} from "../internal"
2829interface ProxyBaseState extends ImmerBaseState {30 parent_?: ImmerState31 revoke_(): void32}3334export interface ProxyObjectState extends ProxyBaseState {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 35why, one step back
35 type_: ArchType.Object

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 36–41why, one step back
36 base_: any
37 copy_: any38 draft_: Drafted<AnyObject, ProxyObjectState>39}4041export interface ProxyArrayState extends ProxyBaseState {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 42why, one step back
42 type_: ArchType.Array

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 43–57why, one step back
43 base_: AnyArray
44 copy_: AnyArray | null45 draft_: Drafted<AnyArray, ProxyArrayState>46}4748type ProxyState = ProxyObjectState | ProxyArrayState4950/**51 * Returns a new draft of the `base` object.52 *53 * The second argument is the parent draft-state (used internally).54 */55export function createProxyProxy<T extends Objectish>(56 base: T,57 parent?: ImmerState

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 58–59why, one step back
58): [Drafted<T, ProxyState>, ProxyState] {
59 const baseIsArray = isArray(base)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 60why, one step back
60 const state: ProxyState = {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 61why, one step back
61 type_: baseIsArray ? ArchType.Array : (ArchType.Object as any),

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 62–68why, one step back
62 // Track which produce call this is associated with.
63 scope_: parent ? parent.scope_ : getCurrentScope()!,64 // True for both shallow and deep changes.65 modified_: false,66 // Used during finalization.67 finalized_: false,68 // Track which properties have been assigned (true) or deleted (false).

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 69–70why, one step back
69 // actually instantiated in `prepareCopy()`
70 assigned_: undefined,

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 71–80why, one step back
71 // The parent draft state.
72 parent_: parent,73 // The base state.74 base_: base,75 // The base proxy.76 draft_: null as any, // set below77 // The base copy with any updated values.78 copy_: null,79 // Called by the `produce` function.80 revoke_: null as any,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 81–83why, one step back
81 isManual_: false,
82 // `callbacks` actually gets assigned in `createProxy`83 callbacks_: undefined as any

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

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

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 94why, one step back
94 if (baseIsArray) {

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 95–101why, one step back
95 target = [state] as any
96 traps = arrayTraps97 }9899 const {revoke, proxy} = Proxy.revocable(target, traps)100 state.draft_ = proxy as any101 state.revoke_ = revoke

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 102why, one step back
102 return [proxy as any, state]

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 103–112why, one step back
103}
104105/**106 * Object drafts107 */108export const objectTraps: ProxyHandler<ProxyState> = {109 get(state, prop) {110 if (prop === DRAFT_STATE) return state111112 const source = latest(state)

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 113why, one step back
113 if (!has(source, prop, state.type_)) {

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 114–124why, one step back
114 // non-existing or non-own property...
115 return readPropFromProto(state, source, prop)116 }117 const value = source[prop]118 if (state.finalized_ || !isDraftable(value)) {119 return value120 }121 // Check for existing draft in modified state.122 // Assigned values are never drafted. This catches any drafts we created, too.123 if (value === peek(state.base_, prop)) {124 prepareCopy(state)

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 125–129why, one step back
125 // Ensure array keys are always numbers
126 const childKey = state.type_ === ArchType.Array ? +(prop as string) : prop127 const childDraft = createProxy(state.scope_, value, state, childKey)128129 return (state.copy_![childKey] = childDraft)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 130–158why, one step back
130 }
131 return value132 },133 has(state, prop) {134 return prop in latest(state)135 },136 ownKeys(state) {137 return Reflect.ownKeys(latest(state))138 },139 set(140 state: ProxyObjectState,141 prop: string /* strictly not, but helps TS */,142 value143 ) {144 const desc = getDescriptorFromProto(latest(state), prop)145 if (desc?.set) {146 // special case: if this write is captured by a setter, we have147 // to trigger it with the correct context148 desc.set.call(state.draft_, value)149 return true150 }151 if (!state.modified_) {152 // the last check is because we need to be able to distinguish setting a non-existing to undefined (which is a change)153 // from setting an existing property with value undefined to undefined (which is not a change)154 const current = peek(latest(state), prop)155 // special case, if we assigning the original value to a draft, we can ignore the assignment156 const currentState: ProxyObjectState = current?.[DRAFT_STATE]157 if (currentState && currentState.base_ === value) {158 state.copy_![prop] = value

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 159why, one step back
159 state.assigned_!.set(prop, false)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

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

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 162–165why, one step back
162 if (
163 is(value, current) &&164 (value !== undefined || has(state.base_, prop, state.type_))165 )

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 166–170why, one step back
166 return true
167 prepareCopy(state)168 markChanged(state)169 }170

fix: #807 new undefined properties should end up in result objectMichel Weststratebefore adoptiondc3f66c

line 171why, one step back
171 if (

fix: special case NaN in setter (#912)Raybefore adoption5721bb7

lines 172–174why, one step back
172 (state.copy_![prop] === value &&
173 // special case: handle new props with value 'undefined'174 (value !== undefined || prop in state.copy_)) ||

fix: #807 new undefined properties should end up in result objectMichel Weststratebefore adoptiondc3f66c

line 175why, one step back
175 // special case: NaN

fix: special case NaN in setter (#912)Raybefore adoption5721bb7

line 176why, one step back
176 (Number.isNaN(value) && Number.isNaN(state.copy_![prop]))

fix: #807 new undefined properties should end up in result objectMichel Weststratebefore adoptiondc3f66c

lines 177–178why, one step back
177 )
178 return true

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 179–181why, one step back
179
180 // @ts-ignore181 state.copy_![prop] = value

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 182–184why, one step back
182 state.assigned_!.set(prop, true)
183184 handleCrossReference(state, prop, value)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 185–187why, one step back
185 return true
186 },187 deleteProperty(state, prop: string) {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

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

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 189–190why, one step back
189 // The `undefined` check is a fast path for pre-existing keys.
190 if (peek(state.base_, prop) !== undefined || prop in state.base_) {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 191why, one step back
191 state.assigned_!.set(prop, false)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 192–194why, one step back
192 markChanged(state)
193 } else {194 // if an originally not assigned property was deleted

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 195why, one step back
195 state.assigned_!.delete(prop)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 196why, one step back
196 }

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 197–199why, one step back
197 if (state.copy_) {
198 delete state.copy_[prop]199 }

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 200–208why, one step back
200 return true
201 },202 // Note: We never coerce `desc.value` into an Immer draft, because we can't make203 // the same guarantee in ES5 mode.204 getOwnPropertyDescriptor(state, prop) {205 const owner = latest(state)206 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)207 if (!desc) return desc208 return {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 209–212why, one step back
209 [WRITABLE]: true,
210 [CONFIGURABLE]: state.type_ !== ArchType.Array || prop !== "length",211 [ENUMERABLE]: desc[ENUMERABLE],212 [VALUE]: owner[prop]

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 213–218why, one step back
213 }
214 },215 defineProperty() {216 die(11)217 },218 getPrototypeOf(state) {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

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

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 220–233why, one step back
220 },
221 setPrototypeOf() {222 die(12)223 }224}225226/**227 * Array drafts228 */229230const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}231each(objectTraps, (key, fn) => {232 // @ts-ignore233 arrayTraps[key] = function() {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 234–236why, one step back
234 const args = arguments
235 args[0] = args[0][0]236 return fn.apply(this, args)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 237–239why, one step back
237 }
238})239arrayTraps.deleteProperty = function(state, prop) {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 240–241why, one step back
240 if (process.env.NODE_ENV !== "production" && isNaN(parseInt(prop as any)))
241 die(13)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 242–243why, one step back
242 // @ts-ignore
243 return arrayTraps.set!.call(this, state, prop, undefined)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 244–245why, one step back
244}
245arrayTraps.set = function(state, prop, value) {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 246–251why, one step back
246 if (
247 process.env.NODE_ENV !== "production" &&248 prop !== "length" &&249 isNaN(parseInt(prop as any))250 )251 die(14)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 252–264why, one step back
252 return objectTraps.set!.call(this, state[0], prop, value, state[0])
253}254255// Access a property without creating an Immer draft.256function peek(draft: Drafted, prop: PropertyKey) {257 const state = draft[DRAFT_STATE]258 const source = state ? latest(state) : draft259 return source[prop]260}261262function readPropFromProto(state: ImmerState, source: any, prop: PropertyKey) {263 const desc = getDescriptorFromProto(source, prop)264 return desc

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 265–266why, one step back
265 ? VALUE in desc
266 ? desc[VALUE]

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 267–278why, one step back
267 : // This is a very special case, if the prop is a getter defined by the
268 // prototype, we should invoke it with the draft as context!269 desc.get?.call(state.draft_)270 : undefined271}272273function getDescriptorFromProto(274 source: any,275 prop: PropertyKey276): PropertyDescriptor | undefined {277 // 'in' checks proto!278 if (!(prop in source)) return undefined

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 279why, one step back
279 let proto = getPrototypeOf(source)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 280–282why, one step back
280 while (proto) {
281 const desc = Object.getOwnPropertyDescriptor(proto, prop)282 if (desc) return desc

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 283why, one step back
283 proto = getPrototypeOf(proto)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 284–296why, one step back
284 }
285 return undefined286}287288export function markChanged(state: ImmerState) {289 if (!state.modified_) {290 state.modified_ = true291 if (state.parent_) {292 markChanged(state.parent_)293 }294 }295}296

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 297why, one step back
297export function prepareCopy(state: ImmerState) {

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 298why, one step back
298 if (!state.copy_) {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 299–301why, one step back
299 // Actually create the `assigned_` map now that we
300 // know this is a modified draft.301 state.assigned_ = new Map()

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 302–305why, one step back
302 state.copy_ = shallowCopy(
303 state.base_,304 state.scope_.immer_.useStrictShallowCopy_305 )

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 306–307why, one step back
306 }
307}