Skip to content
Myr

proxy.ts

This file

Path

src/core/proxy.ts

292 lines · 7.6 KiB · TypeScript

Last changed by

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

History of this file

Take it

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

lines 19–20why, one step back
19 ArchType,
20 ImmerScope

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 21–31why, one step back
21} from "../internal"
2223interface ProxyBaseState extends ImmerBaseState {24 assigned_: {25 [property: string]: boolean26 }27 parent_?: ImmerState28 revoke_(): void29}3031export interface ProxyObjectState extends ProxyBaseState {

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 33–38why, one step back
33 base_: any
34 copy_: any35 draft_: Drafted<AnyObject, ProxyObjectState>36}3738export interface ProxyArrayState extends ProxyBaseState {

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 40–57why, one step back
40 base_: AnyArray
41 copy_: AnyArray | null42 draft_: Drafted<AnyArray, ProxyArrayState>43}4445type ProxyState = ProxyObjectState | ProxyArrayState4647/**48 * Returns a new draft of the `base` object.49 *50 * The second argument is the parent draft-state (used internally).51 */52export function createProxyProxy<T extends Objectish>(53 base: T,54 parent?: ImmerState55): Drafted<T, ProxyState> {56 const isArray = Array.isArray(base)57 const state: ProxyState = {

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 59–118why, one step back
59 // Track which produce call this is associated with.
60 scope_: parent ? parent.scope_ : getCurrentScope()!,61 // True for both shallow and deep changes.62 modified_: false,63 // Used during finalization.64 finalized_: false,65 // Track which properties have been assigned (true) or deleted (false).66 assigned_: {},67 // The parent draft state.68 parent_: parent,69 // The base state.70 base_: base,71 // The base proxy.72 draft_: null as any, // set below73 // The base copy with any updated values.74 copy_: null,75 // Called by the `produce` function.76 revoke_: null as any,77 isManual_: false78 }7980 // the traps must target something, a bit like the 'real' base.81 // but also, we need to be able to determine from the target what the relevant state is82 // (to avoid creating traps per instance to capture the state in closure,83 // and to avoid creating weird hidden properties as well)84 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)85 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb86 let target: T = state as any87 let traps: ProxyHandler<object | Array<any>> = objectTraps88 if (isArray) {89 target = [state] as any90 traps = arrayTraps91 }9293 const {revoke, proxy} = Proxy.revocable(target, traps)94 state.draft_ = proxy as any95 state.revoke_ = revoke96 return proxy as any97}9899/**100 * Object drafts101 */102export const objectTraps: ProxyHandler<ProxyState> = {103 get(state, prop) {104 if (prop === DRAFT_STATE) return state105106 const source = latest(state)107 if (!has(source, prop)) {108 // non-existing or non-own property...109 return readPropFromProto(state, source, prop)110 }111 const value = source[prop]112 if (state.finalized_ || !isDraftable(value)) {113 return value114 }115 // Check for existing draft in modified state.116 // Assigned values are never drafted. This catches any drafts we created, too.117 if (value === peek(state.base_, prop)) {118 prepareCopy(state)

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

line 119why, one step back
119 return (state.copy_![prop as any] = createProxy(value, state))

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 134–141why, one step back
134 const desc = getDescriptorFromProto(latest(state), prop)
135 if (desc?.set) {136 // special case: if this write is captured by a setter, we have137 // to trigger it with the correct context138 desc.set.call(state.draft_, value)139 return true140 }141 if (!state.modified_) {

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

line 143why, one step back
143 // 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 144–149why, one step back
144 const current = peek(latest(state), prop)
145 // special case, if we assigning the original value to a draft, we can ignore the assignment146 const currentState: ProxyObjectState = current?.[DRAFT_STATE]147 if (currentState && currentState.base_ === value) {148 state.copy_![prop] = value149 state.assigned_[prop] = false

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

line 150why, one step back
150 return true

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 154–156why, one step back
154 prepareCopy(state)
155 markChanged(state)156 }

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

line 157why, one step back
157

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

line 158why, one step back
158 if (

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

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

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

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

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

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

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

lines 164–165why, one step back
164 )
165 return true

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

line 166why, one step back
166

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 170–181why, one step back
170 return true
171 },172 deleteProperty(state, prop: string) {173 // The `undefined` check is a fast path for pre-existing keys.174 if (peek(state.base_, prop) !== undefined || prop in state.base_) {175 state.assigned_[prop] = false176 prepareCopy(state)177 markChanged(state)178 } else {179 // if an originally not assigned property was deleted180 delete state.assigned_[prop]181 }

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

lines 182–184why, one step back
182 if (state.copy_) {
183 delete state.copy_[prop]184 }

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 185–194why, one step back
185 return true
186 },187 // Note: We never coerce `desc.value` into an Immer draft, because we can't make188 // the same guarantee in ES5 mode.189 getOwnPropertyDescriptor(state, prop) {190 const owner = latest(state)191 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)192 if (!desc) return desc193 return {194 writable: true,

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

line 195why, one step back
195 configurable: state.type_ !== ArchType.Array || prop !== "length",

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 196–203why, one step back
196 enumerable: desc.enumerable,
197 value: owner[prop]198 }199 },200 defineProperty() {201 die(11)202 },203 getPrototypeOf(state) {

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 205–223why, one step back
205 },
206 setPrototypeOf() {207 die(12)208 }209}210211/**212 * Array drafts213 */214215const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}216each(objectTraps, (key, fn) => {217 // @ts-ignore218 arrayTraps[key] = function() {219 arguments[0] = arguments[0][0]220 return fn.apply(this, arguments)221 }222})223arrayTraps.deleteProperty = function(state, prop) {

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

lines 224–225why, one step back
224 if (process.env.NODE_ENV !== "production" && isNaN(parseInt(prop as any)))
225 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)

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

lines 230–235why, one step back
230 if (
231 process.env.NODE_ENV !== "production" &&232 prop !== "length" &&233 isNaN(parseInt(prop as any))234 )235 die(14)

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 236–262why, one step back
236 return objectTraps.set!.call(this, state[0], prop, value, state[0])
237}238239// Access a property without creating an Immer draft.240function peek(draft: Drafted, prop: PropertyKey) {241 const state = draft[DRAFT_STATE]242 const source = state ? latest(state) : draft243 return source[prop]244}245246function readPropFromProto(state: ImmerState, source: any, prop: PropertyKey) {247 const desc = getDescriptorFromProto(source, prop)248 return desc249 ? `value` in desc250 ? desc.value251 : // This is a very special case, if the prop is a getter defined by the252 // prototype, we should invoke it with the draft as context!253 desc.get?.call(state.draft_)254 : undefined255}256257function getDescriptorFromProto(258 source: any,259 prop: PropertyKey260): PropertyDescriptor | undefined {261 // 'in' checks proto!262 if (!(prop in source)) return undefined

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 264–266why, one step back
264 while (proto) {
265 const desc = Object.getOwnPropertyDescriptor(proto, prop)266 if (desc) return desc

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 268–280why, one step back
268 }
269 return undefined270}271272export function markChanged(state: ImmerState) {273 if (!state.modified_) {274 state.modified_ = true275 if (state.parent_) {276 markChanged(state.parent_)277 }278 }279}280

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

lines 281–285why, one step back
281export function prepareCopy(state: {
282 base_: any283 copy_: any284 scope_: ImmerScope285}) {

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

lines 287–290why, one step back
287 state.copy_ = shallowCopy(
288 state.base_,289 state.scope_.immer_.useStrictShallowCopy_290 )

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 291–292why, one step back
291 }
292}