proxy.ts
This file
Path
src/proxy.ts
258 lines · 6.9 KiB · TypeScript
Last changed by
Small code cleanupMichel Weststratebefore adoption9217809
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"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 20why, one step back20feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 21–30why, one step back21interface ProxyBaseState extends ImmerBaseState {22 assigned: {23 [property: string]: boolean24 }25 parent?: ImmerState26 drafts?: {27 [property: string]: Drafted<any, any>28 }29 revoke(): void30}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 31why, one step back31feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 32–37why, one step back32export interface ProxyObjectState extends ProxyBaseState {33 type: ProxyType.ProxyObject34 base: AnyObject35 copy: AnyObject | null36 draft: Drafted<AnyObject, ProxyObjectState>37}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 38why, one step back38feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 39–43why, one step back39export interface ProxyArrayState extends ProxyBaseState {40 type: ProxyType.ProxyArray41 base: AnyArray42 copy: AnyArray | null43 draft: Drafted<AnyArray, ProxyArrayState>feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 44–45why, one step back44}45feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 46–47why, one step back46type ProxyState = ProxyObjectState | ProxyArrayState47feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 48–52why, one step back48/**49 * Returns a new draft of the `base` object.50 *51 * The second argument is the parent draft-state (used internally).52 */Small code cleanupMichel Weststratebefore adoption9217809
line 53why, one step back53export function createProxyProxy<T extends Objectish>(feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 54why, one step back54 base: T,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 55–59why, one step back55 parent?: ImmerState56): Drafted<T, ProxyState> {57 const isArray = Array.isArray(base)58 const state: ProxyState = {59 type: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 60why, one step back60 // Track which produce call this is associated with.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 61why, one step back61 scope: parent ? parent.scope : ImmerScope.current!,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 62–72why, one step back62 // True for both shallow and deep changes.63 modified: false,64 // Used during finalization.65 finalized: false,66 // Track which properties have been assigned (true) or deleted (false).67 assigned: {},68 // The parent draft state.69 parent,70 // The base state.71 base,72 // The base proxy.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 73why, one step back73 draft: null as any, // set belowfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 74–78why, one step back74 // Any property proxies.75 drafts: {},76 // The base copy with any updated values.77 copy: null,78 // Called by the `produce` function.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 79–80why, one step back79 revoke: null as any,80 isManual: falsefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 81–90why, one step back81 }8283 // the traps must target something, a bit like the 'real' base.84 // but also, we need to be able to determine from the target what the relevant state is85 // (to avoid creating traps per instance to capture the state in closure,86 // and to avoid creating weird hidden properties as well)87 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)88 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb89 let target: T = state as any90 let traps: ProxyHandler<object | Array<any>> = objectTrapsfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 91why, one step back91 if (isArray) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 92–95why, one step back92 target = [state] as any93 traps = arrayTraps94 }95feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 96–97why, one step back96 // TODO: optimization: might be faster, cheaper if we created a non-revocable proxy97 // and administrate revoking ourselvesfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 98why, one step back98 const {revoke, proxy} = Proxy.revocable(target, traps)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 99why, one step back99 state.draft = proxy as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 100why, one step back100 state.revoke = revokefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 101why, one step back101 return proxy as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 102–106why, one step back102}103104/**105 * Object drafts106 */feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 107why, one step back107const objectTraps: ProxyHandler<ProxyState> = {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 108–113why, one step back108 get(state, prop) {109 if (prop === DRAFT_STATE) return state110 let {drafts} = state111112 // Check for existing draft in unmodified state.113 if (!state.modified && has(drafts, prop)) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 114why, one step back114 return drafts![prop as any]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 115–126why, one step back115 }116117 const value = latest(state)[prop]118 if (state.finalized || !isDraftable(value)) {119 return value120 }121122 // Check for existing draft in modified state.123 if (state.modified) {124 // Assigned values are never drafted. This catches any drafts we created, too.125 if (value !== peek(state.base, prop)) return value126 // Store drafts on the copy (when one exists).feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 127why, one step back127 // @ts-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 128–130why, one step back128 drafts = state.copy129 }130Small code cleanupMichel Weststratebefore adoption9217809
line 131why, one step back131 return (drafts![prop as any] = createProxy(state.scope.immer, value, state))feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 132–138why, one step back132 },133 has(state, prop) {134 return prop in latest(state)135 },136 ownKeys(state) {137 return Reflect.ownKeys(latest(state))138 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 139why, one step back139 set(state, prop: string /* strictly not, but helps TS */, value) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 140–145why, one step back140 if (!state.modified) {141 const baseValue = peek(state.base, prop)142 // Optimize based on value's truthiness. Truthy values are guaranteed to143 // never be undefined, so we can avoid the `in` operator. Lastly, truthy144 // values may be drafts, but falsy values are never drafts.145 const isUnchanged = valuefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 146why, one step back146 ? is(baseValue, value) || value === state.drafts![prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 147–148why, one step back147 : is(baseValue, value) && prop in state.base148 if (isUnchanged) return truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 149why, one step back149 prepareCopy(state)Small code cleanupMichel Weststratebefore adoption9217809
line 150why, one step back150 markChangedProxy(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 151–152why, one step back151 }152 state.assigned[prop] = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 153–154why, one step back153 // @ts-ignore154 state.copy![prop] = valuefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 155–156why, one step back155 return true156 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 157why, one step back157 deleteProperty(state, prop: string) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 158–160why, one step back158 // The `undefined` check is a fast path for pre-existing keys.159 if (peek(state.base, prop) !== undefined || prop in state.base) {160 state.assigned[prop] = falsefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 161why, one step back161 prepareCopy(state)Small code cleanupMichel Weststratebefore adoption9217809
line 162why, one step back162 markChangedProxy(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 163–166why, one step back163 } else if (state.assigned[prop]) {164 // if an originally not assigned property was deleted165 delete state.assigned[prop]166 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 167why, one step back167 // @ts-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 168–177why, one step back168 if (state.copy) delete state.copy[prop]169 return true170 },171 // Note: We never coerce `desc.value` into an Immer draft, because we can't make172 // the same guarantee in ES5 mode.173 getOwnPropertyDescriptor(state, prop) {174 const owner = latest(state)175 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)176 if (desc) {177 desc.writable = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 178–179why, one step back178 desc.configurable =179 state.type !== ProxyType.ProxyArray || prop !== "length"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 180–197why, one step back180 }181 return desc182 },183 defineProperty() {184 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore185 },186 getPrototypeOf(state) {187 return Object.getPrototypeOf(state.base)188 },189 setPrototypeOf() {190 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore191 }192}193194/**195 * Array drafts196 */197feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 198why, one step back198const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 199why, one step back199each(objectTraps, (key, fn) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 200why, one step back200 // @ts-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 201–223why, one step back201 arrayTraps[key] = function() {202 arguments[0] = arguments[0][0]203 return fn.apply(this, arguments)204 }205})206arrayTraps.deleteProperty = function(state, prop) {207 if (isNaN(parseInt(prop as any))) {208 throw new Error("Immer only supports deleting array indices") // prettier-ignore209 }210 return objectTraps.deleteProperty!.call(this, state[0], prop)211}212arrayTraps.set = function(state, prop, value) {213 if (prop !== "length" && isNaN(parseInt(prop as any))) {214 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore215 }216 return objectTraps.set!.call(this, state[0], prop, value, state[0])217}218219/**220 * Map drafts221 */222223// Access a property without creating an Immer draft.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 224why, one step back224function peek(draft: Drafted, prop: PropertyKey): any {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 225–232why, one step back225 const state = draft[DRAFT_STATE]226 const desc = Reflect.getOwnPropertyDescriptor(227 state ? latest(state) : draft,228 prop229 )230 return desc && desc.value231}232Small code cleanupMichel Weststratebefore adoption9217809
line 233why, one step back233export function markChangedProxy(state: ImmerState) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 234–235why, one step back234 if (!state.modified) {235 state.modified = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 236–245why, one step back236 if (237 state.type === ProxyType.ProxyObject ||238 state.type === ProxyType.ProxyArray239 ) {240 const copy = (state.copy = shallowCopy(state.base))241 each(state.drafts!, (key, value) => {242 // @ts-ignore243 copy[key] = value244 })245 state.drafts = undefinedfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 246–247why, one step back246 }247feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 248why, one step back248 if (state.parent) {Small code cleanupMichel Weststratebefore adoption9217809
line 249why, one step back249 markChangedProxy(state.parent)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 250–253why, one step back250 }251 }252}253feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 254–256why, one step back254function prepareCopy(state: ProxyState) {255 if (!state.copy) {256 state.copy = shallowCopy(state.base)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 257–258why, one step back257 }258}