proxy.ts
This file
Path
src/proxy.ts
260 lines · 7.0 KiB · TypeScript
Last changed by
Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
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–12why, one step back11 Drafted,12 AnyObject,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 13–14why, one step back13 AnyArray,14 Objectish,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 15why, one step back15 ImmerScope,Small code cleanupMichel Weststratebefore adoption9217809
line 16why, one step back16 DRAFT_STATE,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 17–19why, one step back17 createProxy,18 ProxyTypeProxyObject,19 ProxyTypeProxyArrayfix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 20why, one step back20} from "./internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 21why, one step back21import invariant from "tiny-invariant"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 22why, one step back22feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 23why, one step back23interface ProxyBaseState extends ImmerBaseState {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 24why, one step back24 assigned_: {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 25–26why, one step back25 [property: string]: boolean26 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 27–28why, one step back27 parent_?: ImmerState28 drafts_?: {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 29–30why, one step back29 [property: string]: Drafted<any, any>30 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 31why, one step back31 revoke_(): voidfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 32why, one step back32}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 33why, one step back33feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 34why, one step back34export interface ProxyObjectState extends ProxyBaseState {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 35why, one step back35 type_: typeof ProxyTypeProxyObjectMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 36–38why, one step back36 base_: AnyObject37 copy_: AnyObject | null38 draft_: Drafted<AnyObject, ProxyObjectState>feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 39why, one step back39}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 40why, one step back40feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 41why, one step back41export interface ProxyArrayState extends ProxyBaseState {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 42why, one step back42 type_: typeof ProxyTypeProxyArrayMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 43–45why, one step back43 base_: AnyArray44 copy_: AnyArray | null45 draft_: Drafted<AnyArray, ProxyArrayState>feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 46–47why, one step back46}47feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 48–49why, one step back48type ProxyState = ProxyObjectState | ProxyArrayState49feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 50–54why, one step back50/**51 * Returns a new draft of the `base` object.52 *53 * The second argument is the parent draft-state (used internally).54 */Small code cleanupMichel Weststratebefore adoption9217809
line 55why, one step back55export function createProxyProxy<T extends Objectish>(feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 56why, one step back56 base: T,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 57–60why, one step back57 parent?: ImmerState58): Drafted<T, ProxyState> {59 const isArray = Array.isArray(base)60 const state: ProxyState = {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 61why, one step back61 type_: isArray ? ProxyTypeProxyArray : (ProxyTypeProxyObject as any),feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 62why, one step back62 // Track which produce call this is associated with.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 63why, one step back63 scope_: parent ? parent.scope_ : ImmerScope.current_!,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 64why, one step back64 // True for both shallow and deep changes.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 65why, one step back65 modified_: false,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 66why, one step back66 // Used during finalization.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 67why, one step back67 finalized_: false,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 68why, one step back68 // Track which properties have been assigned (true) or deleted (false).Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 69why, one step back69 assigned_: {},feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 70why, one step back70 // The parent draft state.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 71why, one step back71 parent_: parent,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 72why, one step back72 // The base state.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 73why, one step back73 base_: base,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 74why, one step back74 // The base proxy.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 75why, one step back75 draft_: null as any, // set belowfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 76why, one step back76 // Any property proxies.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 77why, one step back77 drafts_: {},feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 78why, one step back78 // The base copy with any updated values.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 79why, one step back79 copy_: null,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 80why, one step back80 // Called by the `produce` function.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 81–82why, one step back81 revoke_: null as any,82 isManual_: falsefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 83–92why, one step back83 }8485 // the traps must target something, a bit like the 'real' base.86 // but also, we need to be able to determine from the target what the relevant state is87 // (to avoid creating traps per instance to capture the state in closure,88 // and to avoid creating weird hidden properties as well)89 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)90 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb91 let target: T = state as any92 let traps: ProxyHandler<object | Array<any>> = objectTrapsfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 93why, one step back93 if (isArray) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 94–97why, one step back94 target = [state] as any95 traps = arrayTraps96 }97feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 98–99why, one step back98 // TODO: optimization: might be faster, cheaper if we created a non-revocable proxy99 // and administrate revoking ourselvesfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 100why, one step back100 const {revoke, proxy} = Proxy.revocable(target, traps)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 101–102why, one step back101 state.draft_ = proxy as any102 state.revoke_ = revokefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 103why, one step back103 return proxy as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 104–108why, one step back104}105106/**107 * Object drafts108 */feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 109why, one step back109const objectTraps: ProxyHandler<ProxyState> = {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 110–111why, one step back110 get(state, prop) {111 if (prop === DRAFT_STATE) return stateMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 112why, one step back112 let {drafts_: drafts} = statefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 113–114why, one step back113114 // Check for existing draft in unmodified state.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 115why, one step back115 if (!state.modified_ && has(drafts, prop)) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 116why, one step back116 return drafts![prop as any]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 117–119why, one step back117 }118119 const value = latest(state)[prop]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 120why, one step back120 if (state.finalized_ || !isDraftable(value)) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 121–124why, one step back121 return value122 }123124 // Check for existing draft in modified state.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 125why, one step back125 if (state.modified_) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 126why, one step back126 // Assigned values are never drafted. This catches any drafts we created, too.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 127why, one step back127 if (value !== peek(state.base_, prop)) return valuefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 128why, one step back128 // Store drafts on the copy (when one exists).feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 129why, one step back129 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 130why, one step back130 drafts = state.copy_feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 131–132why, one step back131 }132Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 133–137why, one step back133 return (drafts![prop as any] = createProxy(134 state.scope_.immer_,135 value,136 state137 ))feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 138–144why, one step back138 },139 has(state, prop) {140 return prop in latest(state)141 },142 ownKeys(state) {143 return Reflect.ownKeys(latest(state))144 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 145why, one step back145 set(state, prop: string /* strictly not, but helps TS */, value) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 146–147why, one step back146 if (!state.modified_) {147 const baseValue = peek(state.base_, prop)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 148–151why, one step back148 // Optimize based on value's truthiness. Truthy values are guaranteed to149 // never be undefined, so we can avoid the `in` operator. Lastly, truthy150 // values may be drafts, but falsy values are never drafts.151 const isUnchanged = valueMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 152–153why, one step back152 ? is(baseValue, value) || value === state.drafts_![prop]153 : is(baseValue, value) && prop in state.base_feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 154why, one step back154 if (isUnchanged) return truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 155why, one step back155 prepareCopy(state)Small code cleanupMichel Weststratebefore adoption9217809
line 156why, one step back156 markChangedProxy(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 157why, one step back157 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 158why, one step back158 state.assigned_[prop] = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 159why, one step back159 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 160why, one step back160 state.copy_![prop] = valuefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 161–162why, one step back161 return true162 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 163why, one step back163 deleteProperty(state, prop: string) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 164why, one step back164 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 165–166why, one step back165 if (peek(state.base_, prop) !== undefined || prop in state.base_) {166 state.assigned_[prop] = falsefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 167why, one step back167 prepareCopy(state)Small code cleanupMichel Weststratebefore adoption9217809
line 168why, one step back168 markChangedProxy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 169why, one step back169 } else if (state.assigned_[prop]) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 170why, one step back170 // if an originally not assigned property was deletedMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 171why, one step back171 delete state.assigned_[prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 172why, one step back172 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 173why, one step back173 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 174why, one step back174 if (state.copy_) delete state.copy_[prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 175–183why, one step back175 return true176 },177 // Note: We never coerce `desc.value` into an Immer draft, because we can't make178 // the same guarantee in ES5 mode.179 getOwnPropertyDescriptor(state, prop) {180 const owner = latest(state)181 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)182 if (desc) {183 desc.writable = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 184why, one step back184 desc.configurable =Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 185why, one step back185 state.type_ !== ProxyTypeProxyArray || prop !== "length"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 186–189why, one step back186 }187 return desc188 },189 defineProperty() {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 190why, one step back190 invariant(false, "Object.defineProperty() cannot be used on an Immer draft") // prettier-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 191–192why, one step back191 },192 getPrototypeOf(state) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 193why, one step back193 return Object.getPrototypeOf(state.base_)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 194–195why, one step back194 },195 setPrototypeOf() {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 196why, one step back196 invariant(false, "Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 197–203why, one step back197 }198}199200/**201 * Array drafts202 */203feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 204why, one step back204const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 205why, one step back205each(objectTraps, (key, fn) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 206why, one step back206 // @ts-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 207–212why, one step back207 arrayTraps[key] = function() {208 arguments[0] = arguments[0][0]209 return fn.apply(this, arguments)210 }211})212arrayTraps.deleteProperty = function(state, prop) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 213why, one step back213 invariant(!isNaN(parseInt(prop as any)), "Immer only supports deleting array indices") // prettier-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 214–216why, one step back214 return objectTraps.deleteProperty!.call(this, state[0], prop)215}216arrayTraps.set = function(state, prop, value) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 217why, one step back217 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 218–225why, one step back218 return objectTraps.set!.call(this, state[0], prop, value, state[0])219}220221/**222 * Map drafts223 */224225// Access a property without creating an Immer draft.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 226why, one step back226function peek(draft: Drafted, prop: PropertyKey): any {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 227–234why, one step back227 const state = draft[DRAFT_STATE]228 const desc = Reflect.getOwnPropertyDescriptor(229 state ? latest(state) : draft,230 prop231 )232 return desc && desc.value233}234Small code cleanupMichel Weststratebefore adoption9217809
line 235why, one step back235export function markChangedProxy(state: ImmerState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 236–237why, one step back236 if (!state.modified_) {237 state.modified_ = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 238why, one step back238 if (Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 239–240why, one step back239 state.type_ === ProxyTypeProxyObject ||240 state.type_ === ProxyTypeProxyArrayfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 241why, one step back241 ) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 242–243why, one step back242 const copy = (state.copy_ = shallowCopy(state.base_))243 each(state.drafts_!, (key, value) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 244–246why, one step back244 // @ts-ignore245 copy[key] = value246 })Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 247why, one step back247 state.drafts_ = undefinedfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 248–249why, one step back248 }249Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 250–251why, one step back250 if (state.parent_) {251 markChangedProxy(state.parent_)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 252–255why, one step back252 }253 }254}255feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 256why, one step back256function prepareCopy(state: ProxyState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 257–258why, one step back257 if (!state.copy_) {258 state.copy_ = shallowCopy(state.base_)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 259–260why, one step back259 }260}