simplifying proxy impl
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 1466db3 · Browse files · 1 file·+36 added−49 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+36 added−49 removed
- other:+36 added−49 removed
Provenance
- Commit
b1c47c2- Tree
92c77e6- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
1466db3
1 file changed+36 added−49 removed
modified:src/core/proxy.ts+36 added-49 removed
1-"use strict"1 import {2 each,3 has,4 is,18 unmodified lines23 assigned_: {24 [property: string]: boolean25 }26 parent_?: ImmerState28- drafts_?: {29- [property: string]: Drafted<any, any>30- }27 revoke_(): void28 }29 30 export interface ProxyObjectState extends ProxyBaseState {31 type_: typeof ProxyTypeProxyObject32 base_: AnyObject37- copy_: AnyObject | null33+ copy_: any34 draft_: Drafted<AnyObject, ProxyObjectState>35 }36 37 export interface ProxyArrayState extends ProxyBaseState {30 unmodified lines68 // The base state.69 base_: base,70 // The base proxy.71 draft_: null as any, // set below76- // Any property proxies.77- drafts_: {},72 // The base copy with any updated values.73 copy_: null,74 // Called by the `produce` function.75 revoke_: null as any,24 unmodified lines100 */101 const objectTraps: ProxyHandler<ProxyState> = {102 get(state, prop) {103 if (prop === DRAFT_STATE) return state110- let {drafts_: drafts} = state104 112- // Check for existing draft in unmodified state.113- if (!state.modified_ && has(drafts, prop)) {114- return drafts![prop as any]115- }116-105 const value = latest(state)[prop]106 if (state.finalized_ || !isDraftable(value)) {107 return value108 }109 110 // 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 value111+ // TODO: dedupe112+ // Assigned values are never drafted. This catches any drafts we created, too.113+ if (value === peek(state.base_, prop)) {114+ prepareCopy(state)115+ return (state.copy_![prop as any] = createProxy(116+ state.scope_.immer_,117+ value,118+ state119+ ))120 // Store drafts on the copy (when one exists).121 // @ts-ignore128- drafts = state.copy_122 }130-131- return (drafts![prop as any] = createProxy(132- state.scope_.immer_,133- value,134- state135- ))123+ return value124 },125 has(state, prop) {126 return prop in latest(state)127 },128 ownKeys(state) {129 return Reflect.ownKeys(latest(state))130 },131+ // TODO: dedupe132 set(state, prop: string /* strictly not, but helps TS */, value) {133+ state.assigned_[prop] = true134 if (!state.modified_) {145- const baseValue = peek(state.base_, prop)146- // Optimize based on value's truthiness. Truthy values are guaranteed to147- // never be undefined, so we can avoid the `in` operator. Lastly, truthy148- // values may be drafts, but falsy values are never drafts.149- const isUnchanged = value150- ? is(baseValue, value) || value === state.drafts_![prop]151- : is(baseValue, value) && prop in state.base_152- if (isUnchanged) return true135+ if (is(value, peek(latest(state), prop))) return true136 prepareCopy(state)137 markChangedProxy(state)138 }156- state.assigned_[prop] = true139 // @ts-ignore140 state.copy_![prop] = value141 return true142 },15 unmodified lines158 // the same guarantee in ES5 mode.159 getOwnPropertyDescriptor(state, prop) {160 const owner = latest(state)161 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)180- if (desc) {181- desc.writable = true182- desc.configurable =183- state.type_ !== ProxyTypeProxyArray || prop !== "length"162+ if (!desc) return desc163+ return {164+ writable: true,165+ configurable: state.type_ !== ProxyTypeProxyArray || prop !== "length",166+ enumerable: desc.enumerable,167+ value: owner[prop]168 }185- return desc169 },170 defineProperty() {171 die(11)172 },30 unmodified lines203 * Map drafts204 */205 206 // Access a property without creating an Immer draft.207+// TODO: duplicate with ES5?208 function peek(draft: Drafted, prop: PropertyKey): any {209 const state = draft[DRAFT_STATE]210 const desc = Reflect.getOwnPropertyDescriptor(211 state ? latest(state) : draft,1 unmodified line213 )214 return desc && desc.value215 }216 217+// TODO: dedupe218 export function markChangedProxy(state: ImmerState) {219 if (!state.modified_) {220 state.modified_ = true236- if (237- state.type_ === ProxyTypeProxyObject ||238- state.type_ === ProxyTypeProxyArray239- ) {240- const copy = (state.copy_ = shallowCopy(state.base_))241- each(state.drafts_!, (key, value) => {242- // @ts-ignore243- copy[key] = value244- })245- state.drafts_ = undefined246- }221+ // if (222+ // state.type_ === ProxyTypeProxyObject ||223+ // state.type_ === ProxyTypeProxyArray224+ // ) {225+ // prepareCopy(state);226+ // const copy = (state.copy_ = shallowCopy(state.base_, true))227+ // each(state.drafts_!, (key, value) => {228+ // // @ts-ignore229+ // copy[key] = value230+ // })231+ // state.drafts_ = undefined232+ // }233 234 if (state.parent_) {235 markChangedProxy(state.parent_)236 }237 }238 }239 240+// TODO: duplicate with ES5241 function prepareCopy(state: ProxyState) {242 if (!state.copy_) {256- state.copy_ = shallowCopy(state.base_)243+ state.copy_ = shallowCopy(state.base_, true)244 }245 }