proxy.ts
This file
Path
src/proxy.ts
259 lines · 7.0 KiB · TypeScript
Last changed by
Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
History of this fileTake it
feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 1–7why, one step back1"use strict"2import {3 each,4 has,5 is,6 isDraftable,7 shallowCopy,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 8–9why, one step back8 latest,9 ImmerBaseState,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 10why, one step back10 ImmerState,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 11–13why, one step back11 Drafted,12 ProxyType,13 AnyObject,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 14–15why, one step back14 AnyArray,15 Objectish,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 16why, one step back16 ImmerScope,Small code cleanupMichel Weststratebefore adoption9217809
lines 17–18why, one step back17 DRAFT_STATE,18 createProxyfix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 19why, one step back19} from "./internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 20why, one step back20import invariant from "tiny-invariant"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 21why, one step back21feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 22why, one step back22interface ProxyBaseState extends ImmerBaseState {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 23why, one step back23 assigned_: {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 24–25why, one step back24 [property: string]: boolean25 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 26–27why, one step back26 parent_?: ImmerState27 drafts_?: {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 28–29why, one step back28 [property: string]: Drafted<any, any>29 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 30why, one step back30 revoke_(): voidfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 31why, one step back31}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 32why, one step back32feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 33why, one step back33export interface ProxyObjectState extends ProxyBaseState {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 34–37why, one step back34 type_: ProxyType.ProxyObject35 base_: AnyObject36 copy_: AnyObject | null37 draft_: Drafted<AnyObject, ProxyObjectState>feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 38why, one step back38}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 39why, one step back39feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 40why, one step back40export interface ProxyArrayState extends ProxyBaseState {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 41–44why, one step back41 type_: ProxyType.ProxyArray42 base_: AnyArray43 copy_: AnyArray | null44 draft_: Drafted<AnyArray, ProxyArrayState>feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 45–46why, one step back45}46feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 47–48why, one step back47type ProxyState = ProxyObjectState | ProxyArrayState48feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 49–53why, one step back49/**50 * Returns a new draft of the `base` object.51 *52 * The second argument is the parent draft-state (used internally).53 */Small code cleanupMichel Weststratebefore adoption9217809
line 54why, one step back54export function createProxyProxy<T extends Objectish>(feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 55why, one step back55 base: T,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 56–59why, one step back56 parent?: ImmerState57): Drafted<T, ProxyState> {58 const isArray = Array.isArray(base)59 const state: ProxyState = {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 60why, one step back60 type_: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 61why, one step back61 // Track which produce call this is associated with.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 62why, one step back62 scope_: parent ? parent.scope_ : ImmerScope.current_!,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 63why, one step back63 // True for both shallow and deep changes.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 64why, one step back64 modified_: false,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 65why, one step back65 // Used during finalization.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 66why, one step back66 finalized_: false,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 67why, one step back67 // Track which properties have been assigned (true) or deleted (false).Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 68why, one step back68 assigned_: {},feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 69why, one step back69 // The parent draft state.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 70why, one step back70 parent_: parent,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 71why, one step back71 // The base state.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 72why, one step back72 base_: base,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 73why, one step back73 // The base proxy.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 74why, one step back74 draft_: null as any, // set belowfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 75why, one step back75 // Any property proxies.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 76why, one step back76 drafts_: {},feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 77why, one step back77 // The base copy with any updated values.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 78why, one step back78 copy_: null,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 79why, one step back79 // Called by the `produce` function.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 80–81why, one step back80 revoke_: null as any,81 isManual_: falsefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 82–91why, one step back82 }8384 // the traps must target something, a bit like the 'real' base.85 // but also, we need to be able to determine from the target what the relevant state is86 // (to avoid creating traps per instance to capture the state in closure,87 // and to avoid creating weird hidden properties as well)88 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)89 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb90 let target: T = state as any91 let traps: ProxyHandler<object | Array<any>> = objectTrapsfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 92why, one step back92 if (isArray) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 93–96why, one step back93 target = [state] as any94 traps = arrayTraps95 }96feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 97–98why, one step back97 // TODO: optimization: might be faster, cheaper if we created a non-revocable proxy98 // and administrate revoking ourselvesfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 99why, one step back99 const {revoke, proxy} = Proxy.revocable(target, traps)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 100–101why, one step back100 state.draft_ = proxy as any101 state.revoke_ = revokefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 102why, one step back102 return proxy as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 103–107why, one step back103}104105/**106 * Object drafts107 */feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 108why, one step back108const objectTraps: ProxyHandler<ProxyState> = {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 109–110why, one step back109 get(state, prop) {110 if (prop === DRAFT_STATE) return stateMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 111why, one step back111 let {drafts_: drafts} = statefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 112–113why, one step back112113 // Check for existing draft in unmodified state.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 114why, one step back114 if (!state.modified_ && has(drafts, prop)) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 115why, one step back115 return drafts![prop as any]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 116–118why, one step back116 }117118 const value = latest(state)[prop]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 119why, one step back119 if (state.finalized_ || !isDraftable(value)) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 120–123why, one step back120 return value121 }122123 // Check for existing draft in modified state.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 124why, one step back124 if (state.modified_) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 125why, one step back125 // Assigned values are never drafted. This catches any drafts we created, too.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 126why, one step back126 if (value !== peek(state.base_, prop)) return valuefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 127why, one step back127 // Store drafts on the copy (when one exists).feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 128why, one step back128 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 129why, one step back129 drafts = state.copy_feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 130–131why, one step back130 }131Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 132–136why, one step back132 return (drafts![prop as any] = createProxy(133 state.scope_.immer_,134 value,135 state136 ))feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 137–143why, one step back137 },138 has(state, prop) {139 return prop in latest(state)140 },141 ownKeys(state) {142 return Reflect.ownKeys(latest(state))143 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 144why, one step back144 set(state, prop: string /* strictly not, but helps TS */, value) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 145–146why, one step back145 if (!state.modified_) {146 const baseValue = peek(state.base_, prop)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 147–150why, one step back147 // Optimize based on value's truthiness. Truthy values are guaranteed to148 // never be undefined, so we can avoid the `in` operator. Lastly, truthy149 // values may be drafts, but falsy values are never drafts.150 const isUnchanged = valueMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 151–152why, one step back151 ? is(baseValue, value) || value === state.drafts_![prop]152 : is(baseValue, value) && prop in state.base_feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 153why, one step back153 if (isUnchanged) return truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 154why, one step back154 prepareCopy(state)Small code cleanupMichel Weststratebefore adoption9217809
line 155why, one step back155 markChangedProxy(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 156why, one step back156 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 157why, one step back157 state.assigned_[prop] = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 158why, one step back158 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 159why, one step back159 state.copy_![prop] = valuefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 160–161why, one step back160 return true161 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 162why, one step back162 deleteProperty(state, prop: string) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 163why, one step back163 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 164–165why, one step back164 if (peek(state.base_, prop) !== undefined || prop in state.base_) {165 state.assigned_[prop] = falsefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 166why, one step back166 prepareCopy(state)Small code cleanupMichel Weststratebefore adoption9217809
line 167why, one step back167 markChangedProxy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 168why, one step back168 } else if (state.assigned_[prop]) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 169why, one step back169 // if an originally not assigned property was deletedMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 170why, one step back170 delete state.assigned_[prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 171why, one step back171 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 172why, one step back172 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 173why, one step back173 if (state.copy_) delete state.copy_[prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 174–182why, one step back174 return true175 },176 // Note: We never coerce `desc.value` into an Immer draft, because we can't make177 // the same guarantee in ES5 mode.178 getOwnPropertyDescriptor(state, prop) {179 const owner = latest(state)180 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)181 if (desc) {182 desc.writable = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 183why, one step back183 desc.configurable =Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 184why, one step back184 state.type_ !== ProxyType.ProxyArray || prop !== "length"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 185–188why, one step back185 }186 return desc187 },188 defineProperty() {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 189why, one step back189 invariant(false, "Object.defineProperty() cannot be used on an Immer draft") // prettier-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 190–191why, one step back190 },191 getPrototypeOf(state) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 192why, one step back192 return Object.getPrototypeOf(state.base_)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 193–194why, one step back193 },194 setPrototypeOf() {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 195why, one step back195 invariant(false, "Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 196–202why, one step back196 }197}198199/**200 * Array drafts201 */202feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 203why, one step back203const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 204why, one step back204each(objectTraps, (key, fn) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 205why, one step back205 // @ts-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 206–211why, one step back206 arrayTraps[key] = function() {207 arguments[0] = arguments[0][0]208 return fn.apply(this, arguments)209 }210})211arrayTraps.deleteProperty = function(state, prop) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 212why, one step back212 invariant(!isNaN(parseInt(prop as any)), "Immer only supports deleting array indices") // prettier-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 213–215why, one step back213 return objectTraps.deleteProperty!.call(this, state[0], prop)214}215arrayTraps.set = function(state, prop, value) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 216why, one step back216 invariant(prop === "length" || !isNaN(parseInt(prop as any)), "Immer only supports setting array indices and the 'length' property") // prettier-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 217–224why, one step back217 return objectTraps.set!.call(this, state[0], prop, value, state[0])218}219220/**221 * Map drafts222 */223224// Access a property without creating an Immer draft.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 225why, one step back225function peek(draft: Drafted, prop: PropertyKey): any {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 226–233why, one step back226 const state = draft[DRAFT_STATE]227 const desc = Reflect.getOwnPropertyDescriptor(228 state ? latest(state) : draft,229 prop230 )231 return desc && desc.value232}233Small code cleanupMichel Weststratebefore adoption9217809
line 234why, one step back234export function markChangedProxy(state: ImmerState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 235–236why, one step back235 if (!state.modified_) {236 state.modified_ = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 237why, one step back237 if (Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 238–239why, one step back238 state.type_ === ProxyType.ProxyObject ||239 state.type_ === ProxyType.ProxyArrayfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 240why, one step back240 ) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 241–242why, one step back241 const copy = (state.copy_ = shallowCopy(state.base_))242 each(state.drafts_!, (key, value) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 243–245why, one step back243 // @ts-ignore244 copy[key] = value245 })Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 246why, one step back246 state.drafts_ = undefinedfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 247–248why, one step back247 }248Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 249–250why, one step back249 if (state.parent_) {250 markChangedProxy(state.parent_)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 251–254why, one step back251 }252 }253}254feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 255why, one step back255function prepareCopy(state: ProxyState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 256–257why, one step back256 if (!state.copy_) {257 state.copy_ = shallowCopy(state.base_)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 258–259why, one step back258 }259}