Skip to content
Myr

proxy.ts

This file

Path

src/core/proxy.ts

280 lines · 7.5 KiB · TypeScript

Last changed by

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

History of this file

Take it

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 1–17why, 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,15 DRAFT_STATE,16 die,17 createProxy,

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 18why, one step back
18 ProxyType

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 19–29why, one step back
19} from "../internal"
2021interface ProxyBaseState extends ImmerBaseState {22 assigned_: {23 [property: string]: boolean24 }25 parent_?: ImmerState26 revoke_(): void27}2829export interface ProxyObjectState extends ProxyBaseState {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 30why, one step back
30 type_: ProxyType.ProxyObject

Immer 7Michel Weststratebefore adoption467ea5d

lines 31–32why, one step back
31 base_: any
32 copy_: any

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 33–36why, one step back
33 draft_: Drafted<AnyObject, ProxyObjectState>
34}3536export interface ProxyArrayState extends ProxyBaseState {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 37why, one step back
37 type_: ProxyType.ProxyArray

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 38–55why, one step back
38 base_: AnyArray
39 copy_: AnyArray | null40 draft_: Drafted<AnyArray, ProxyArrayState>41}4243type ProxyState = ProxyObjectState | ProxyArrayState4445/**46 * Returns a new draft of the `base` object.47 *48 * The second argument is the parent draft-state (used internally).49 */50export function createProxyProxy<T extends Objectish>(51 base: T,52 parent?: ImmerState53): Drafted<T, ProxyState> {54 const isArray = Array.isArray(base)55 const state: ProxyState = {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 57–99why, one step back
57 // Track which produce call this is associated with.
58 scope_: parent ? parent.scope_ : getCurrentScope()!,59 // True for both shallow and deep changes.60 modified_: false,61 // Used during finalization.62 finalized_: false,63 // Track which properties have been assigned (true) or deleted (false).64 assigned_: {},65 // The parent draft state.66 parent_: parent,67 // The base state.68 base_: base,69 // The base proxy.70 draft_: null as any, // set below71 // The base copy with any updated values.72 copy_: null,73 // Called by the `produce` function.74 revoke_: null as any,75 isManual_: false76 }7778 // the traps must target something, a bit like the 'real' base.79 // but also, we need to be able to determine from the target what the relevant state is80 // (to avoid creating traps per instance to capture the state in closure,81 // and to avoid creating weird hidden properties as well)82 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)83 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb84 let target: T = state as any85 let traps: ProxyHandler<object | Array<any>> = objectTraps86 if (isArray) {87 target = [state] as any88 traps = arrayTraps89 }9091 const {revoke, proxy} = Proxy.revocable(target, traps)92 state.draft_ = proxy as any93 state.revoke_ = revoke94 return proxy as any95}9697/**98 * Object drafts99 */

Immer 7Michel Weststratebefore adoption467ea5d

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 101–103why, one step back
101 get(state, prop) {
102 if (prop === DRAFT_STATE) return state103

Immer 7Michel Weststratebefore adoption467ea5d

lines 104–107why, one step back
104 const source = latest(state)
105 if (!has(source, prop)) {106 // non-existing or non-own property...107 return readPropFromProto(state, source, prop)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 108why, one step back
108 }

Immer 7Michel Weststratebefore adoption467ea5d

line 109why, one step back
109 const value = source[prop]

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 110–113why, one step back
110 if (state.finalized_ || !isDraftable(value)) {
111 return value112 }113 // Check for existing draft in modified state.

Immer 7Michel Weststratebefore adoption467ea5d

lines 114–121why, one step back
114 // Assigned values are never drafted. This catches any drafts we created, too.
115 if (value === peek(state.base_, prop)) {116 prepareCopy(state)117 return (state.copy_![prop as any] = createProxy(118 state.scope_.immer_,119 value,120 state121 ))

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 122why, one step back
122 }

Immer 7Michel Weststratebefore adoption467ea5d

line 123why, one step back
123 return value

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 124–130why, one step back
124 },
125 has(state, prop) {126 return prop in latest(state)127 },128 ownKeys(state) {129 return Reflect.ownKeys(latest(state))130 },

fix: reconcile if the original value is assigned after creating a draft. Fixes #659Michel Weststratebefore adoptionc0e6749

lines 131–135why, one step back
131 set(
132 state: ProxyObjectState,133 prop: string /* strictly not, but helps TS */,134 value135 ) {

fix: Trigger setters with the correct context, fixes #604Michel Weststratebefore adoption2697430

lines 136–142why, one step back
136 const desc = getDescriptorFromProto(latest(state), prop)
137 if (desc?.set) {138 // special case: if this write is captured by a setter, we have139 // to trigger it with the correct context140 desc.set.call(state.draft_, value)141 return true142 }

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

chore: Fix typo in comment (#776)inokawabefore adoptionffa3972

line 144why, one step back
144 // the last check is because we need to be able to distinguish setting a non-existing to undefined (which is a change)

fix: make sure changing an undefined value to undefined is not picked up as change. Fixes #646Michel Weststratebefore adoption5521527

line 145why, one step back
145 // from setting an existing property with value undefined to undefined (which is not a change)

fix: reconcile if the original value is assigned after creating a draft. Fixes #659Michel Weststratebefore adoptionc0e6749

lines 146–151why, one step back
146 const current = peek(latest(state), prop)
147 // special case, if we assigning the original value to a draft, we can ignore the assignment148 const currentState: ProxyObjectState = current?.[DRAFT_STATE]149 if (currentState && currentState.base_ === value) {150 state.copy_![prop] = value151 state.assigned_[prop] = false

Immer 7Michel Weststratebefore adoption467ea5d

line 152why, one step back
152 return true

fix: reconcile if the original value is assigned after creating a draft. Fixes #659Michel Weststratebefore adoptionc0e6749

lines 153–155why, one step back
153 }
154 if (is(value, current) && (value !== undefined || has(state.base_, prop)))155 return true

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

Immer 7Michel Weststratebefore adoption467ea5d

line 157why, one step back
157 markChanged(state)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 158why, one step back
158 }

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 159why, one step back
159

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

line 160why, one step back
160 if (

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

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

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

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

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

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

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

lines 166–167why, one step back
166 )
167 return true

fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d

line 168why, one step back
168

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 169–170why, one step back
169 // @ts-ignore
170 state.copy_![prop] = value

fix: reconcile if the original value is assigned after creating a draft. Fixes #659Michel Weststratebefore adoptionc0e6749

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 172–178why, one step back
172 return true
173 },174 deleteProperty(state, prop: string) {175 // The `undefined` check is a fast path for pre-existing keys.176 if (peek(state.base_, prop) !== undefined || prop in state.base_) {177 state.assigned_[prop] = false178 prepareCopy(state)

Immer 7Michel Weststratebefore adoption467ea5d

lines 179–180why, one step back
179 markChanged(state)
180 } else {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 181–192why, one step back
181 // if an originally not assigned property was deleted
182 delete state.assigned_[prop]183 }184 // @ts-ignore185 if (state.copy_) delete state.copy_[prop]186 return true187 },188 // Note: We never coerce `desc.value` into an Immer draft, because we can't make189 // the same guarantee in ES5 mode.190 getOwnPropertyDescriptor(state, prop) {191 const owner = latest(state)192 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)

Immer 7Michel Weststratebefore adoption467ea5d

lines 193–195why, one step back
193 if (!desc) return desc
194 return {195 writable: true,

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

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

Immer 7Michel Weststratebefore adoption467ea5d

lines 197–198why, one step back
197 enumerable: desc.enumerable,
198 value: owner[prop]

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 199–225why, one step back
199 }
200 },201 defineProperty() {202 die(11)203 },204 getPrototypeOf(state) {205 return Object.getPrototypeOf(state.base_)206 },207 setPrototypeOf() {208 die(12)209 }210}211212/**213 * Array drafts214 */215216const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}217each(objectTraps, (key, fn) => {218 // @ts-ignore219 arrayTraps[key] = function() {220 arguments[0] = arguments[0][0]221 return fn.apply(this, arguments)222 }223})224arrayTraps.deleteProperty = function(state, prop) {225 if (__DEV__ && isNaN(parseInt(prop as any))) die(13)

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

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 228–234why, one step back
228}
229arrayTraps.set = function(state, prop, value) {230 if (__DEV__ && prop !== "length" && isNaN(parseInt(prop as any))) die(14)231 return objectTraps.set!.call(this, state[0], prop, value, state[0])232}233234// Access a property without creating an Immer draft.

Immer 7Michel Weststratebefore adoption467ea5d

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 236why, one step back
236 const state = draft[DRAFT_STATE]

Immer 7Michel Weststratebefore adoption467ea5d

lines 237–238why, one step back
237 const source = state ? latest(state) : draft
238 return source[prop]

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 239–240why, one step back
239}
240

Immer 7Michel Weststratebefore adoption467ea5d

line 241why, one step back
241function readPropFromProto(state: ImmerState, source: any, prop: PropertyKey) {

fix: Trigger setters with the correct context, fixes #604Michel Weststratebefore adoption2697430

lines 242–255why, one step back
242 const desc = getDescriptorFromProto(source, prop)
243 return desc244 ? `value` in desc245 ? desc.value246 : // This is a very special case, if the prop is a getter defined by the247 // prototype, we should invoke it with the draft as context!248 desc.get?.call(state.draft_)249 : undefined250}251252function getDescriptorFromProto(253 source: any,254 prop: PropertyKey255): PropertyDescriptor | undefined {

Immer 7Michel Weststratebefore adoption467ea5d

lines 256–260why, one step back
256 // 'in' checks proto!
257 if (!(prop in source)) return undefined258 let proto = Object.getPrototypeOf(source)259 while (proto) {260 const desc = Object.getOwnPropertyDescriptor(proto, prop)

fix: Trigger setters with the correct context, fixes #604Michel Weststratebefore adoption2697430

line 261why, one step back
261 if (desc) return desc

Immer 7Michel Weststratebefore adoption467ea5d

lines 262–267why, one step back
262 proto = Object.getPrototypeOf(proto)
263 }264 return undefined265}266267export function markChanged(state: ImmerState) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 268–270why, one step back
268 if (!state.modified_) {
269 state.modified_ = true270 if (state.parent_) {

Immer 7Michel Weststratebefore adoption467ea5d

line 271why, one step back
271 markChanged(state.parent_)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 272–275why, one step back
272 }
273 }274}275

Immer 7Michel Weststratebefore adoption467ea5d

line 276why, one step back
276export function prepareCopy(state: {base_: any; copy_: any}) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 277–280why, one step back
277 if (!state.copy_) {
278 state.copy_ = shallowCopy(state.base_)279 }280}