proxy.ts
This file
Path
src/proxy.ts
257 lines · 6.8 KiB · TypeScript
Last changed by
fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
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
lines 16–18why, one step back16 ImmerScope,17 DRAFT_STATE18} from "./internal"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 19why, one step back19feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 20–29why, one step back20interface ProxyBaseState extends ImmerBaseState {21 assigned: {22 [property: string]: boolean23 }24 parent?: ImmerState25 drafts?: {26 [property: string]: Drafted<any, any>27 }28 revoke(): void29}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 30why, one step back30feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 31–36why, one step back31export interface ProxyObjectState extends ProxyBaseState {32 type: ProxyType.ProxyObject33 base: AnyObject34 copy: AnyObject | null35 draft: Drafted<AnyObject, ProxyObjectState>36}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 37why, one step back37feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 38–42why, one step back38export interface ProxyArrayState extends ProxyBaseState {39 type: ProxyType.ProxyArray40 base: AnyArray41 copy: AnyArray | null42 draft: Drafted<AnyArray, ProxyArrayState>feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 43–44why, one step back43}44feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 45–46why, one step back45type ProxyState = ProxyObjectState | ProxyArrayState46feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 47–51why, one step back47/**48 * Returns a new draft of the `base` object.49 *50 * The second argument is the parent draft-state (used internally).51 */feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 52why, one step back52export function createProxy<T extends Objectish>(feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 53why, one step back53 base: T,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 54–58why, one step back54 parent?: ImmerState55): Drafted<T, ProxyState> {56 const isArray = Array.isArray(base)57 const state: ProxyState = {58 type: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 59why, one step back59 // Track which produce call this is associated with.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 60why, one step back60 scope: parent ? parent.scope : ImmerScope.current!,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 61–71why, one step back61 // True for both shallow and deep changes.62 modified: false,63 // Used during finalization.64 finalized: false,65 // Track which properties have been assigned (true) or deleted (false).66 assigned: {},67 // The parent draft state.68 parent,69 // The base state.70 base,71 // The base proxy.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 72why, one step back72 draft: null as any, // set belowfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 73–77why, one step back73 // Any property proxies.74 drafts: {},75 // The base copy with any updated values.76 copy: null,77 // Called by the `produce` function.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 78–79why, one step back78 revoke: null as any,79 isManual: falsefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 80–89why, one step back80 }8182 // the traps must target something, a bit like the 'real' base.83 // but also, we need to be able to determine from the target what the relevant state is84 // (to avoid creating traps per instance to capture the state in closure,85 // and to avoid creating weird hidden properties as well)86 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)87 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb88 let target: T = state as any89 let traps: ProxyHandler<object | Array<any>> = objectTrapsfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 90why, one step back90 if (isArray) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 91–94why, one step back91 target = [state] as any92 traps = arrayTraps93 }94feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 95–96why, one step back95 // TODO: optimization: might be faster, cheaper if we created a non-revocable proxy96 // and administrate revoking ourselvesfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 97why, one step back97 const {revoke, proxy} = Proxy.revocable(target, traps)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 98why, one step back98 state.draft = proxy as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 99why, one step back99 state.revoke = revokefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 100why, one step back100 return proxy as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 101–105why, one step back101}102103/**104 * Object drafts105 */feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 106why, one step back106const objectTraps: ProxyHandler<ProxyState> = {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 107–112why, one step back107 get(state, prop) {108 if (prop === DRAFT_STATE) return state109 let {drafts} = state110111 // Check for existing draft in unmodified state.112 if (!state.modified && has(drafts, prop)) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 113why, one step back113 return drafts![prop as any]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 114–125why, one step back114 }115116 const value = latest(state)[prop]117 if (state.finalized || !isDraftable(value)) {118 return value119 }120121 // Check for existing draft in modified state.122 if (state.modified) {123 // Assigned values are never drafted. This catches any drafts we created, too.124 if (value !== peek(state.base, prop)) return value125 // Store drafts on the copy (when one exists).feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 126why, one step back126 // @ts-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 127–129why, one step back127 drafts = state.copy128 }129feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 130why, one step back130 return (drafts![prop as any] = state.scope.immer.createProxy(value, state))feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 131–137why, one step back131 },132 has(state, prop) {133 return prop in latest(state)134 },135 ownKeys(state) {136 return Reflect.ownKeys(latest(state))137 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 138why, one step back138 set(state, prop: string /* strictly not, but helps TS */, value) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 139–144why, one step back139 if (!state.modified) {140 const baseValue = peek(state.base, prop)141 // Optimize based on value's truthiness. Truthy values are guaranteed to142 // never be undefined, so we can avoid the `in` operator. Lastly, truthy143 // values may be drafts, but falsy values are never drafts.144 const isUnchanged = valuefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 145why, one step back145 ? is(baseValue, value) || value === state.drafts![prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 146–147why, one step back146 : is(baseValue, value) && prop in state.base147 if (isUnchanged) return truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 148why, one step back148 prepareCopy(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 149–151why, one step back149 markChanged(state)150 }151 state.assigned[prop] = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 152–153why, one step back152 // @ts-ignore153 state.copy![prop] = valuefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 154–155why, one step back154 return true155 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 156why, one step back156 deleteProperty(state, prop: string) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 157–159why, one step back157 // The `undefined` check is a fast path for pre-existing keys.158 if (peek(state.base, prop) !== undefined || prop in state.base) {159 state.assigned[prop] = falsefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 160why, one step back160 prepareCopy(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 161–165why, one step back161 markChanged(state)162 } else if (state.assigned[prop]) {163 // if an originally not assigned property was deleted164 delete state.assigned[prop]165 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 166why, one step back166 // @ts-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 167–176why, one step back167 if (state.copy) delete state.copy[prop]168 return true169 },170 // Note: We never coerce `desc.value` into an Immer draft, because we can't make171 // the same guarantee in ES5 mode.172 getOwnPropertyDescriptor(state, prop) {173 const owner = latest(state)174 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)175 if (desc) {176 desc.writable = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 177–178why, one step back177 desc.configurable =178 state.type !== ProxyType.ProxyArray || prop !== "length"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 179–196why, one step back179 }180 return desc181 },182 defineProperty() {183 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore184 },185 getPrototypeOf(state) {186 return Object.getPrototypeOf(state.base)187 },188 setPrototypeOf() {189 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore190 }191}192193/**194 * Array drafts195 */196feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 197why, one step back197const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 198why, one step back198each(objectTraps, (key, fn) => {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 199why, one step back199 // @ts-ignorefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 200–222why, one step back200 arrayTraps[key] = function() {201 arguments[0] = arguments[0][0]202 return fn.apply(this, arguments)203 }204})205arrayTraps.deleteProperty = function(state, prop) {206 if (isNaN(parseInt(prop as any))) {207 throw new Error("Immer only supports deleting array indices") // prettier-ignore208 }209 return objectTraps.deleteProperty!.call(this, state[0], prop)210}211arrayTraps.set = function(state, prop, value) {212 if (prop !== "length" && isNaN(parseInt(prop as any))) {213 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore214 }215 return objectTraps.set!.call(this, state[0], prop, value, state[0])216}217218/**219 * Map drafts220 */221222// Access a property without creating an Immer draft.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 223why, one step back223function peek(draft: Drafted, prop: PropertyKey): any {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 224–231why, one step back224 const state = draft[DRAFT_STATE]225 const desc = Reflect.getOwnPropertyDescriptor(226 state ? latest(state) : draft,227 prop228 )229 return desc && desc.value230}231feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 232why, one step back232export function markChanged(state: ImmerState) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 233–234why, one step back233 if (!state.modified) {234 state.modified = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 235–244why, one step back235 if (236 state.type === ProxyType.ProxyObject ||237 state.type === ProxyType.ProxyArray238 ) {239 const copy = (state.copy = shallowCopy(state.base))240 each(state.drafts!, (key, value) => {241 // @ts-ignore242 copy[key] = value243 })244 state.drafts = undefinedfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 245–246why, one step back245 }246feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 247–248why, one step back247 if (state.parent) {248 markChanged(state.parent)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 249–252why, one step back249 }250 }251}252feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 253–255why, one step back253function prepareCopy(state: ProxyState) {254 if (!state.copy) {255 state.copy = shallowCopy(state.base)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 256–257why, one step back256 }257}