proxy.ts
This file
Path
src/proxy.ts
255 lines · 6.9 KiB · TypeScript
Last changed by
use invariant -321 bytesMichel Weststratebefore adoptionac57da5
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
lines 22–31why, one step back22interface ProxyBaseState extends ImmerBaseState {23 assigned: {24 [property: string]: boolean25 }26 parent?: ImmerState27 drafts?: {28 [property: string]: Drafted<any, any>29 }30 revoke(): void31}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 32why, one step back32feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 33–38why, one step back33export interface ProxyObjectState extends ProxyBaseState {34 type: ProxyType.ProxyObject35 base: AnyObject36 copy: AnyObject | null37 draft: Drafted<AnyObject, ProxyObjectState>38}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 39why, one step back39feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 40–44why, one step back40export interface ProxyArrayState extends ProxyBaseState {41 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–60why, one step back56 parent?: ImmerState57): Drafted<T, ProxyState> {58 const isArray = Array.isArray(base)59 const state: ProxyState = {60 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.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 62why, one step back62 scope: parent ? parent.scope : ImmerScope.current!,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 63–73why, one step back63 // True for both shallow and deep changes.64 modified: false,65 // Used during finalization.66 finalized: false,67 // Track which properties have been assigned (true) or deleted (false).68 assigned: {},69 // The parent draft state.70 parent,71 // The base state.72 base,73 // The base proxy.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 74why, one step back74 draft: null as any, // set belowfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 75–79why, one step back75 // Any property proxies.76 drafts: {},77 // The base copy with any updated values.78 copy: null,79 // Called by the `produce` function.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
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)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 100why, one step back100 state.draft = proxy as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 101why, one step back101 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–114why, one step back109 get(state, prop) {110 if (prop === DRAFT_STATE) return state111 let {drafts} = state112113 // Check for existing draft in unmodified state.114 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–127why, one step back116 }117118 const value = latest(state)[prop]119 if (state.finalized || !isDraftable(value)) {120 return value121 }122123 // Check for existing draft in modified state.124 if (state.modified) {125 // Assigned values are never drafted. This catches any drafts we created, too.126 if (value !== peek(state.base, prop)) return value127 // 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-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 129–131why, one step back129 drafts = state.copy130 }131Small code cleanupMichel Weststratebefore adoption9217809
line 132why, one step back132 return (drafts![prop as any] = createProxy(state.scope.immer, value, state))feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 133–139why, one step back133 },134 has(state, prop) {135 return prop in latest(state)136 },137 ownKeys(state) {138 return Reflect.ownKeys(latest(state))139 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 140why, one step back140 set(state, prop: string /* strictly not, but helps TS */, value) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 141–146why, one step back141 if (!state.modified) {142 const baseValue = peek(state.base, prop)143 // Optimize based on value's truthiness. Truthy values are guaranteed to144 // never be undefined, so we can avoid the `in` operator. Lastly, truthy145 // values may be drafts, but falsy values are never drafts.146 const isUnchanged = valuefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 147why, one step back147 ? is(baseValue, value) || value === state.drafts![prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 148–149why, one step back148 : is(baseValue, value) && prop in state.base149 if (isUnchanged) return truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 150why, one step back150 prepareCopy(state)Small code cleanupMichel Weststratebefore adoption9217809
line 151why, one step back151 markChangedProxy(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 152–153why, one step back152 }153 state.assigned[prop] = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 154–155why, one step back154 // @ts-ignore155 state.copy![prop] = valuefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 156–157why, one step back156 return true157 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 158why, one step back158 deleteProperty(state, prop: string) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 159–161why, one step back159 // The `undefined` check is a fast path for pre-existing keys.160 if (peek(state.base, prop) !== undefined || prop in state.base) {161 state.assigned[prop] = falsefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 162why, one step back162 prepareCopy(state)Small code cleanupMichel Weststratebefore adoption9217809
line 163why, one step back163 markChangedProxy(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 164–167why, one step back164 } else if (state.assigned[prop]) {165 // if an originally not assigned property was deleted166 delete state.assigned[prop]167 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 168why, one step back168 // @ts-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 169–178why, one step back169 if (state.copy) delete state.copy[prop]170 return true171 },172 // Note: We never coerce `desc.value` into an Immer draft, because we can't make173 // the same guarantee in ES5 mode.174 getOwnPropertyDescriptor(state, prop) {175 const owner = latest(state)176 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)177 if (desc) {178 desc.writable = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 179–180why, one step back179 desc.configurable =180 state.type !== ProxyType.ProxyArray || prop !== "length"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 181–184why, one step back181 }182 return desc183 },184 defineProperty() {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 185why, one step back185 invariant(false, "Object.defineProperty() cannot be used on an Immer draft") // prettier-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 186–190why, one step back186 },187 getPrototypeOf(state) {188 return Object.getPrototypeOf(state.base)189 },190 setPrototypeOf() {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 191why, one step back191 invariant(false, "Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 192–198why, one step back192 }193}194195/**196 * Array drafts197 */198feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 199why, one step back199const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 200why, one step back200each(objectTraps, (key, fn) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 201why, one step back201 // @ts-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 202–207why, one step back202 arrayTraps[key] = function() {203 arguments[0] = arguments[0][0]204 return fn.apply(this, arguments)205 }206})207arrayTraps.deleteProperty = function(state, prop) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 208why, one step back208 invariant(!isNaN(parseInt(prop as any)), "Immer only supports deleting array indices") // prettier-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 209–211why, one step back209 return objectTraps.deleteProperty!.call(this, state[0], prop)210}211arrayTraps.set = function(state, prop, value) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 212why, one step back212 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 213–220why, one step back213 return objectTraps.set!.call(this, state[0], prop, value, state[0])214}215216/**217 * Map drafts218 */219220// Access a property without creating an Immer draft.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 221why, one step back221function peek(draft: Drafted, prop: PropertyKey): any {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 222–229why, one step back222 const state = draft[DRAFT_STATE]223 const desc = Reflect.getOwnPropertyDescriptor(224 state ? latest(state) : draft,225 prop226 )227 return desc && desc.value228}229Small code cleanupMichel Weststratebefore adoption9217809
line 230why, one step back230export function markChangedProxy(state: ImmerState) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 231–232why, one step back231 if (!state.modified) {232 state.modified = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 233–242why, one step back233 if (234 state.type === ProxyType.ProxyObject ||235 state.type === ProxyType.ProxyArray236 ) {237 const copy = (state.copy = shallowCopy(state.base))238 each(state.drafts!, (key, value) => {239 // @ts-ignore240 copy[key] = value241 })242 state.drafts = undefinedfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 243–244why, one step back243 }244feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 245why, one step back245 if (state.parent) {Small code cleanupMichel Weststratebefore adoption9217809
line 246why, one step back246 markChangedProxy(state.parent)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 247–250why, one step back247 }248 }249}250feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 251–253why, one step back251function prepareCopy(state: ProxyState) {252 if (!state.copy) {253 state.copy = shallowCopy(state.base)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 254–255why, one step back254 }255}