proxy.ts
This file
Path
src/core/proxy.ts
272 lines · 7.3 KiB · TypeScript
Last changed by
chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–17why, one step back1import {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 back18 ProxyTypeMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 19–29why, one step back19} 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 back30 type_: ProxyType.ProxyObjectImmer 7Michel Weststratebefore adoption467ea5d
lines 31–32why, one step back31 base_: any32 copy_: anyMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 33–36why, one step back33 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 back37 type_: ProxyType.ProxyArrayMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 38–55why, one step back38 base_: AnyArray39 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 back56 type_: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 57–99why, one step back57 // 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 back100export const objectTraps: ProxyHandler<ProxyState> = {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 101–103why, one step back101 get(state, prop) {102 if (prop === DRAFT_STATE) return state103Immer 7Michel Weststratebefore adoption467ea5d
lines 104–107why, one step back104 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 back108 }Immer 7Michel Weststratebefore adoption467ea5d
line 109why, one step back109 const value = source[prop]Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 110–113why, one step back110 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 back114 // 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 back122 }Immer 7Michel Weststratebefore adoption467ea5d
line 123why, one step back123 return valueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 124–130why, one step back124 },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 back131 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 back136 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 back143 if (!state.modified_) {chore: Fix typo in comment (#776)inokawabefore adoptionffa3972
line 144why, one step back144 // 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 back145 // 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 back146 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] = falseImmer 7Michel Weststratebefore adoption467ea5d
line 152why, one step back152 return truefix: reconcile if the original value is assigned after creating a draft. Fixes #659Michel Weststratebefore adoptionc0e6749
lines 153–155why, one step back153 }154 if (is(value, current) && (value !== undefined || has(state.base_, prop)))155 return trueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 156why, one step back156 prepareCopy(state)Immer 7Michel Weststratebefore adoption467ea5d
line 157why, one step back157 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 158why, one step back158 }fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d
lines 159–161why, one step back159160 if (state.copy_![prop] === value && typeof value !== "number") return true161Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 162–163why, one step back162 // @ts-ignore163 state.copy_![prop] = valuefix: reconcile if the original value is assigned after creating a draft. Fixes #659Michel Weststratebefore adoptionc0e6749
line 164why, one step back164 state.assigned_[prop] = trueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 165–171why, one step back165 return true166 },167 deleteProperty(state, prop: string) {168 // The `undefined` check is a fast path for pre-existing keys.169 if (peek(state.base_, prop) !== undefined || prop in state.base_) {170 state.assigned_[prop] = false171 prepareCopy(state)Immer 7Michel Weststratebefore adoption467ea5d
lines 172–173why, one step back172 markChanged(state)173 } else {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 174–185why, one step back174 // if an originally not assigned property was deleted175 delete state.assigned_[prop]176 }177 // @ts-ignore178 if (state.copy_) delete state.copy_[prop]179 return true180 },181 // Note: We never coerce `desc.value` into an Immer draft, because we can't make182 // the same guarantee in ES5 mode.183 getOwnPropertyDescriptor(state, prop) {184 const owner = latest(state)185 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)Immer 7Michel Weststratebefore adoption467ea5d
lines 186–188why, one step back186 if (!desc) return desc187 return {188 writable: true,chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 189why, one step back189 configurable: state.type_ !== ProxyType.ProxyArray || prop !== "length",Immer 7Michel Weststratebefore adoption467ea5d
lines 190–191why, one step back190 enumerable: desc.enumerable,191 value: owner[prop]Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 192–226why, one step back192 }193 },194 defineProperty() {195 die(11)196 },197 getPrototypeOf(state) {198 return Object.getPrototypeOf(state.base_)199 },200 setPrototypeOf() {201 die(12)202 }203}204205/**206 * Array drafts207 */208209const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}210each(objectTraps, (key, fn) => {211 // @ts-ignore212 arrayTraps[key] = function() {213 arguments[0] = arguments[0][0]214 return fn.apply(this, arguments)215 }216})217arrayTraps.deleteProperty = function(state, prop) {218 if (__DEV__ && isNaN(parseInt(prop as any))) die(13)219 return objectTraps.deleteProperty!.call(this, state[0], prop)220}221arrayTraps.set = function(state, prop, value) {222 if (__DEV__ && prop !== "length" && isNaN(parseInt(prop as any))) die(14)223 return objectTraps.set!.call(this, state[0], prop, value, state[0])224}225226// Access a property without creating an Immer draft.Immer 7Michel Weststratebefore adoption467ea5d
line 227why, one step back227function peek(draft: Drafted, prop: PropertyKey) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 228why, one step back228 const state = draft[DRAFT_STATE]Immer 7Michel Weststratebefore adoption467ea5d
lines 229–230why, one step back229 const source = state ? latest(state) : draft230 return source[prop]Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 231–232why, one step back231}232Immer 7Michel Weststratebefore adoption467ea5d
line 233why, one step back233function readPropFromProto(state: ImmerState, source: any, prop: PropertyKey) {fix: Trigger setters with the correct context, fixes #604Michel Weststratebefore adoption2697430
lines 234–247why, one step back234 const desc = getDescriptorFromProto(source, prop)235 return desc236 ? `value` in desc237 ? desc.value238 : // This is a very special case, if the prop is a getter defined by the239 // prototype, we should invoke it with the draft as context!240 desc.get?.call(state.draft_)241 : undefined242}243244function getDescriptorFromProto(245 source: any,246 prop: PropertyKey247): PropertyDescriptor | undefined {Immer 7Michel Weststratebefore adoption467ea5d
lines 248–252why, one step back248 // 'in' checks proto!249 if (!(prop in source)) return undefined250 let proto = Object.getPrototypeOf(source)251 while (proto) {252 const desc = Object.getOwnPropertyDescriptor(proto, prop)fix: Trigger setters with the correct context, fixes #604Michel Weststratebefore adoption2697430
line 253why, one step back253 if (desc) return descImmer 7Michel Weststratebefore adoption467ea5d
lines 254–259why, one step back254 proto = Object.getPrototypeOf(proto)255 }256 return undefined257}258259export function markChanged(state: ImmerState) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 260–262why, one step back260 if (!state.modified_) {261 state.modified_ = true262 if (state.parent_) {Immer 7Michel Weststratebefore adoption467ea5d
line 263why, one step back263 markChanged(state.parent_)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 264–267why, one step back264 }265 }266}267Immer 7Michel Weststratebefore adoption467ea5d
line 268why, one step back268export function prepareCopy(state: {base_: any; copy_: any}) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 269–272why, one step back269 if (!state.copy_) {270 state.copy_ = shallowCopy(state.base_)271 }272}