Skip to content
Myr

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 file

Take it

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 1–7why, one step back
1"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 back
8 latest,
9 ImmerBaseState,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 10why, one step back
10 ImmerState,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 11–13why, one step back
11 Drafted,
12 ProxyType,13 AnyObject,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 14–15why, one step back
14 AnyArray,
15 Objectish,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 16–18why, one step back
16 ImmerScope,
17 DRAFT_STATE18} from "./internal"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 19why, one step back
19

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 20–29why, one step back
20interface 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 back
30

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 31–36why, one step back
31export 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 back
37

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 38–42why, one step back
38export 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 back
43}
44

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 45–46why, one step back
45type ProxyState = ProxyObjectState | ProxyArrayState
46

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 47–51why, one step back
47/**
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 back
52export function createProxy<T extends Objectish>(

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 53why, one step back
53 base: T,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 54–58why, one step back
54 parent?: ImmerState
55): 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 back
59 // Track which produce call this is associated with.

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 60why, one step back
60 scope: parent ? parent.scope : ImmerScope.current!,

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 61–71why, one step back
61 // 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 back
72 draft: null as any, // set below

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 73–77why, one step back
73 // 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 back
78 revoke: null as any,
79 isManual: false

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 80–89why, one step back
80 }
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>> = objectTraps

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 90why, one step back
90 if (isArray) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 91–94why, one step back
91 target = [state] as any
92 traps = arrayTraps93 }94

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 95–96why, one step back
95 // TODO: optimization: might be faster, cheaper if we created a non-revocable proxy
96 // and administrate revoking ourselves

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 97why, one step back
97 const {revoke, proxy} = Proxy.revocable(target, traps)

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 98why, one step back
98 state.draft = proxy as any

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 99why, one step back
99 state.revoke = revoke

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 100why, one step back
100 return proxy as any

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 101–105why, one step back
101}
102103/**104 * Object drafts105 */

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 106why, one step back
106const objectTraps: ProxyHandler<ProxyState> = {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 107–112why, one step back
107 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 back
113 return drafts![prop as any]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 114–125why, one step back
114 }
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 back
126 // @ts-ignore

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 127–129why, one step back
127 drafts = state.copy
128 }129

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 130why, one step back
130 return (drafts![prop as any] = state.scope.immer.createProxy(value, state))

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 131–137why, one step back
131 },
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 back
138 set(state, prop: string /* strictly not, but helps TS */, value) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 139–144why, one step back
139 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 = value

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 145why, one step back
145 ? is(baseValue, value) || value === state.drafts![prop]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 146–147why, one step back
146 : is(baseValue, value) && prop in state.base
147 if (isUnchanged) return true

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 148why, one step back
148 prepareCopy(state)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 149–151why, one step back
149 markChanged(state)
150 }151 state.assigned[prop] = true

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 152–153why, one step back
152 // @ts-ignore
153 state.copy![prop] = value

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 154–155why, one step back
154 return true
155 },

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 156why, one step back
156 deleteProperty(state, prop: string) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 157–159why, one step back
157 // 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] = false

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 160why, one step back
160 prepareCopy(state)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 161–165why, one step back
161 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 back
166 // @ts-ignore

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 167–176why, one step back
167 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 = true

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 177–178why, one step back
177 desc.configurable =
178 state.type !== ProxyType.ProxyArray || prop !== "length"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 179–196why, one step back
179 }
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 */196

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 197why, one step back
197const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 198why, one step back
198each(objectTraps, (key, fn) => {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 199why, one step back
199 // @ts-ignore

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 200–222why, one step back
200 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 back
223function peek(draft: Drafted, prop: PropertyKey): any {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 224–231why, one step back
224 const state = draft[DRAFT_STATE]
225 const desc = Reflect.getOwnPropertyDescriptor(226 state ? latest(state) : draft,227 prop228 )229 return desc && desc.value230}231

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 232why, one step back
232export function markChanged(state: ImmerState) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 233–234why, one step back
233 if (!state.modified) {
234 state.modified = true

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 235–244why, one step back
235 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 = undefined

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 245–246why, one step back
245 }
246

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 247–248why, one step back
247 if (state.parent) {
248 markChanged(state.parent)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 249–252why, one step back
249 }
250 }251}252

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 253–255why, one step back
253function 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 back
256 }
257}