Skip to content
Myr

proxy.ts

This file

Path

src/proxy.ts

259 lines · 6.9 KiB · TypeScript

Last changed by

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

History of this file

Take it

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 1–8why, one step back
1"use strict"
2import {3 each,4 has,5 is,6 isDraftable,7 shallowCopy,8 DRAFT_STATE,

map unification WIPMichel Weststratebefore adoption9bcefb6

line 9why, one step back
9 latest

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 10–11why, one step back
10} from "./common"
11import {ImmerScope} from "./scope"

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 12–20why, one step back
12import {
13 AnyObject,14 Drafted,15 ImmerState,16 AnyArray,17 Objectish,18 ImmerBaseState,19 ProxyType20} from "./types"

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 21why, one step back
21

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 22–31why, one step back
22interface ProxyBaseState extends ImmerBaseState {
23 assigned: {24 [property: string]: boolean25 }26 parent?: ImmerState27 drafts?: {28 [property: string]: Drafted<any, any>29 }30 revoke(): void31}

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 32why, one step back
32

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 33–38why, one step back
33export interface ProxyObjectState extends ProxyBaseState {
34 type: ProxyType.ProxyObject35 base: AnyObject36 copy: AnyObject | null37 draft: Drafted<AnyObject, ProxyObjectState>38}

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 39why, one step back
39

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 40–44why, one step back
40export interface ProxyArrayState extends ProxyBaseState {
41 type: ProxyType.ProxyArray42 base: AnyArray43 copy: AnyArray | null44 draft: Drafted<AnyArray, ProxyArrayState>

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 45–46why, one step back
45}
46

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 47–48why, one step back
47type ProxyState = ProxyObjectState | ProxyArrayState
48

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 49–53why, one step back
49/**
50 * Returns a new draft of the `base` object.51 *52 * The second argument is the parent draft-state (used internally).53 */

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 54why, one step back
54export function createProxy<T extends Objectish>(

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 56–60why, one step back
56 parent?: ImmerState
57): Drafted<T, ProxyState> {58 const isArray = Array.isArray(base)59 const state: ProxyState = {60 type: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 61why, one step back
61 // Track which produce call this is associated with.

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 63–73why, one step back
63 // 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.

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 74why, one step back
74 draft: null as any, // set below

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 75–79why, one step back
75 // Any property proxies.
76 drafts: {},77 // The base copy with any updated values.78 copy: null,79 // Called by the `produce` function.

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 80–81why, one step back
80 revoke: null as any,
81 isManual: false

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 93–95why, one step back
93 target = [state] as any
94 traps = arrayTraps95 }

basic map tests succeedingMichel Weststratebefore adoption735c402

line 96why, one step back
96

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 103–107why, one step back
103}
104105/**106 * Object drafts107 */

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 109–114why, one step back
109 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)) {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 115why, one step back
115 return drafts![prop as any]

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 116–127why, one step back
116 }
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).

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 129–131why, one step back
129 drafts = state.copy
130 }131

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 133–139why, one step back
133 },
134 has(state, prop) {135 return prop in latest(state)136 },137 ownKeys(state) {138 return Reflect.ownKeys(latest(state))139 },

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 140why, one step back
140 set(state, prop: string /* strictly not, but helps TS */, value) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

basic map tests succeedingMichel Weststratebefore adoption735c402

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 156–157why, one step back
156 return true
157 },

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

basic map tests succeedingMichel Weststratebefore adoption735c402

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 163–167why, one step back
163 markChanged(state)
164 } else if (state.assigned[prop]) {165 // if an originally not assigned property was deleted166 delete state.assigned[prop]167 }

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 181–198why, one step back
181 }
182 return desc183 },184 defineProperty() {185 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore186 },187 getPrototypeOf(state) {188 return Object.getPrototypeOf(state.base)189 },190 setPrototypeOf() {191 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore192 }193}194195/**196 * Array drafts197 */198

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 202–224why, one step back
202 arrayTraps[key] = function() {
203 arguments[0] = arguments[0][0]204 return fn.apply(this, arguments)205 }206})207arrayTraps.deleteProperty = function(state, prop) {208 if (isNaN(parseInt(prop as any))) {209 throw new Error("Immer only supports deleting array indices") // prettier-ignore210 }211 return objectTraps.deleteProperty!.call(this, state[0], prop)212}213arrayTraps.set = function(state, prop, value) {214 if (prop !== "length" && isNaN(parseInt(prop as any))) {215 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore216 }217 return objectTraps.set!.call(this, state[0], prop, value, state[0])218}219220/**221 * Map drafts222 */223224// Access a property without creating an Immer draft.

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 225why, one step back
225function peek(draft: Drafted, prop: PropertyKey): any {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 226–233why, one step back
226 const state = draft[DRAFT_STATE]
227 const desc = Reflect.getOwnPropertyDescriptor(228 state ? latest(state) : draft,229 prop230 )231 return desc && desc.value232}233

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 237–246why, one step back
237 if (
238 state.type === ProxyType.ProxyObject ||239 state.type === ProxyType.ProxyArray240 ) {241 const copy = (state.copy = shallowCopy(state.base))242 each(state.drafts!, (key, value) => {243 // @ts-ignore244 copy[key] = value245 })246 state.drafts = undefined

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 247–248why, one step back
247 }
248

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 249–250why, one step back
249 if (state.parent) {
250 markChanged(state.parent)

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 251why, one step back
251 }

basic map tests succeedingMichel Weststratebefore adoption735c402

lines 252–254why, one step back
252 }
253}254

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 255why, one step back
255function prepareCopy(state: ProxyState) {

basic map tests succeedingMichel Weststratebefore adoption735c402

lines 256–257why, one step back
256 if (!state.copy) {
257 state.copy = shallowCopy(state.base)

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 258–259why, one step back
258 }
259}