proxy.ts
This file
Path
src/core/proxy.ts
239 lines · 6.3 KiB · TypeScript
Last changed by
Some cleanupMichel Weststratebefore adoptionb3b43b9
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–31why, 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,18 ProxyTypeProxyObject,19 ProxyTypeProxyArray20} from "../internal"2122interface ProxyBaseState extends ImmerBaseState {23 assigned_: {24 [property: string]: boolean25 }26 parent_?: ImmerState27 revoke_(): void28}2930export interface ProxyObjectState extends ProxyBaseState {31 type_: typeof ProxyTypeProxyObjectCode cleanup, bug fixesMichel Weststratebefore adoption0592d3c
line 32why, one step back32 base_: anysimplifying proxy implMichel Weststratebefore adoptionb1c47c2
line 33why, one step back33 copy_: anyMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 34–100why, one step back34 draft_: Drafted<AnyObject, ProxyObjectState>35}3637export interface ProxyArrayState extends ProxyBaseState {38 type_: typeof ProxyTypeProxyArray39 base_: AnyArray40 copy_: AnyArray | null41 draft_: Drafted<AnyArray, ProxyArrayState>42}4344type ProxyState = ProxyObjectState | ProxyArrayState4546/**47 * Returns a new draft of the `base` object.48 *49 * The second argument is the parent draft-state (used internally).50 */51export function createProxyProxy<T extends Objectish>(52 base: T,53 parent?: ImmerState54): Drafted<T, ProxyState> {55 const isArray = Array.isArray(base)56 const state: ProxyState = {57 type_: isArray ? ProxyTypeProxyArray : (ProxyTypeProxyObject as any),58 // Track which produce call this is associated with.59 scope_: parent ? parent.scope_ : getCurrentScope()!,60 // True for both shallow and deep changes.61 modified_: false,62 // Used during finalization.63 finalized_: false,64 // Track which properties have been assigned (true) or deleted (false).65 assigned_: {},66 // The parent draft state.67 parent_: parent,68 // The base state.69 base_: base,70 // The base proxy.71 draft_: null as any, // set below72 // The base copy with any updated values.73 copy_: null,74 // Called by the `produce` function.75 revoke_: null as any,76 isManual_: false77 }7879 // the traps must target something, a bit like the 'real' base.80 // but also, we need to be able to determine from the target what the relevant state is81 // (to avoid creating traps per instance to capture the state in closure,82 // and to avoid creating weird hidden properties as well)83 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)84 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb85 let target: T = state as any86 let traps: ProxyHandler<object | Array<any>> = objectTraps87 if (isArray) {88 target = [state] as any89 traps = arrayTraps90 }9192 const {revoke, proxy} = Proxy.revocable(target, traps)93 state.draft_ = proxy as any94 state.revoke_ = revoke95 return proxy as any96}9798/**99 * Object drafts100 */Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
line 101why, one step back101export const objectTraps: ProxyHandler<ProxyState> = {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 102–104why, one step back102 get(state, prop) {103 if (prop === DRAFT_STATE) return state104Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
lines 105–110why, one step back105 const source = latest(state)106 if (!has(source, prop)) {107 // non-existing or non-own property...108 return readPropFromProto(state, source, prop)109 }110 const value = source[prop]Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 111–114why, one step back111 if (state.finalized_ || !isDraftable(value)) {112 return value113 }114 // Check for existing draft in modified state.simplifying proxy implMichel Weststratebefore adoptionb1c47c2
lines 115–122why, one step back115 // Assigned values are never drafted. This catches any drafts we created, too.116 if (value === peek(state.base_, prop)) {117 prepareCopy(state)118 return (state.copy_![prop as any] = createProxy(119 state.scope_.immer_,120 value,121 state122 ))Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 123–125why, one step back123 // Store drafts on the copy (when one exists).124 // @ts-ignore125 }simplifying proxy implMichel Weststratebefore adoptionb1c47c2
line 126why, one step back126 return valueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 127–134why, one step back127 },128 has(state, prop) {129 return prop in latest(state)130 },131 ownKeys(state) {132 return Reflect.ownKeys(latest(state))133 },134 set(state, prop: string /* strictly not, but helps TS */, value) {simplifying proxy implMichel Weststratebefore adoptionb1c47c2
line 135why, one step back135 state.assigned_[prop] = trueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 136why, one step back136 if (!state.modified_) {Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
lines 137–138why, one step back137 if (is(value, peek(latest(state), prop)) && value !== undefined)138 return trueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 139why, one step back139 prepareCopy(state)Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
line 140why, one step back140 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 141–150why, one step back141 }142 // @ts-ignore143 state.copy_![prop] = value144 return true145 },146 deleteProperty(state, prop: string) {147 // The `undefined` check is a fast path for pre-existing keys.148 if (peek(state.base_, prop) !== undefined || prop in state.base_) {149 state.assigned_[prop] = false150 prepareCopy(state)Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
line 151why, one step back151 markChanged(state)Some cleanupMichel Weststratebefore adoptionb3b43b9
line 152why, one step back152 } else {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 153–164why, one step back153 // if an originally not assigned property was deleted154 delete state.assigned_[prop]155 }156 // @ts-ignore157 if (state.copy_) delete state.copy_[prop]158 return true159 },160 // Note: We never coerce `desc.value` into an Immer draft, because we can't make161 // the same guarantee in ES5 mode.162 getOwnPropertyDescriptor(state, prop) {163 const owner = latest(state)164 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)simplifying proxy implMichel Weststratebefore adoptionb1c47c2
lines 165–170why, one step back165 if (!desc) return desc166 return {167 writable: true,168 configurable: state.type_ !== ProxyTypeProxyArray || prop !== "length",169 enumerable: desc.enumerable,170 value: owner[prop]Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 171–205why, one step back171 }172 },173 defineProperty() {174 die(11)175 },176 getPrototypeOf(state) {177 return Object.getPrototypeOf(state.base_)178 },179 setPrototypeOf() {180 die(12)181 }182}183184/**185 * Array drafts186 */187188const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}189each(objectTraps, (key, fn) => {190 // @ts-ignore191 arrayTraps[key] = function() {192 arguments[0] = arguments[0][0]193 return fn.apply(this, arguments)194 }195})196arrayTraps.deleteProperty = function(state, prop) {197 if (__DEV__ && isNaN(parseInt(prop as any))) die(13)198 return objectTraps.deleteProperty!.call(this, state[0], prop)199}200arrayTraps.set = function(state, prop, value) {201 if (__DEV__ && prop !== "length" && isNaN(parseInt(prop as any))) die(14)202 return objectTraps.set!.call(this, state[0], prop, value, state[0])203}204205// Access a property without creating an Immer draft.Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
line 206why, one step back206function peek(draft: Drafted, prop: PropertyKey) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 207why, one step back207 const state = draft[DRAFT_STATE]Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
lines 208–209why, one step back208 const source = state ? latest(state) : draft209 return source[prop]Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 210–211why, one step back210}211Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
lines 212–226why, one step back212function readPropFromProto(state: ImmerState, source: any, prop: PropertyKey) {213 // 'in' checks proto!214 if (!(prop in source)) return undefined215 let proto = Object.getPrototypeOf(source)216 while (proto) {217 const desc = Object.getOwnPropertyDescriptor(proto, prop)218 // This is a very special case, if the prop is a getter defined by the219 // prototype, we should invoke it with the draft as context!220 if (desc) return `value` in desc ? desc.value : desc.get?.call(state.draft_)221 proto = Object.getPrototypeOf(proto)222 }223 return undefined224}225226export function markChanged(state: ImmerState) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 227–229why, one step back227 if (!state.modified_) {228 state.modified_ = true229 if (state.parent_) {Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
line 230why, one step back230 markChanged(state.parent_)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 231–234why, one step back231 }232 }233}234Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c
line 235why, one step back235export function prepareCopy(state: {base_: any; copy_: any}) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 236why, one step back236 if (!state.copy_) {Some cleanupMichel Weststratebefore adoptionb3b43b9
line 237why, one step back237 state.copy_ = shallowCopy(state.base_)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 238–239why, one step back238 }239}