Skip to content
Myr

proxy.ts

This file

Path

src/proxy.ts

257 lines · 6.8 KiB · TypeScript

Last changed by

Enums FTWMichel Weststratebefore adoptionfd76f1c

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"

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 12–17why, one step back
12import {
13 AnyObject,14 Drafted,15 ImmerState,16 AnyArray,17 Objectish,

Enums FTWMichel Weststratebefore adoptionfd76f1c

lines 18–19why, one step back
18 ImmerBaseState,
19 ProxyType

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 20why, one step back
20} from "./types"

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 21why, one step back
21

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 22why, one step back
22interface ProxyBaseState extends ImmerBaseState {

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 23–25why, one step back
23 assigned: {
24 [property: string]: boolean25 }

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 26why, one step back
26 parent?: ImmerState

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 27why, one step back
27 drafts?: {

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 28–29why, one step back
28 [property: string]: Drafted<any, any>
29 }

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 30why, one step back
30 revoke(): void

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 31–32why, one step back
31}
32

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 33why, one step back
33export interface ProxyObjectState extends ProxyBaseState {

Enums FTWMichel Weststratebefore adoptionfd76f1c

line 34why, one step back
34 type: ProxyType.ProxyObject

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 35–40why, one step back
35 base: AnyObject
36 copy: AnyObject | null37 draft: Drafted<AnyObject, ProxyObjectState>38}3940export interface ProxyArrayState extends ProxyBaseState {

Enums FTWMichel Weststratebefore adoptionfd76f1c

line 41why, one step back
41 type: ProxyType.ProxyArray

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 42–46why, one step back
42 base: AnyArray
43 copy: AnyArray | null44 draft: Drafted<AnyArray, ProxyArrayState>45}46

TS type improvementsMichel Weststratebefore adoptiondd1808e

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 */

TS type improvementsMichel Weststratebefore adoptiondd1808e

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,

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 56why, one step back
56 parent?: ImmerState

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 57why, one step back
57): Drafted<T, ProxyState> {

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 58why, one step back
58 const isArray = Array.isArray(base)

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 59why, one step back
59 const state: ProxyState = {

Enums FTWMichel Weststratebefore adoptionfd76f1c

line 60why, one step back
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.

clean up create proxyMichel Weststratebefore adoptionc182c1e

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.

Introduced proper state typesMichel Weststratebefore adoption3842f85

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.

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

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 branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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)) {

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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).

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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 },

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

basic map tests succeedingMichel Weststratebefore adoption735c402

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 153why, one step back
153 state.copy![prop] = value

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

basic map tests succeedingMichel Weststratebefore adoption735c402

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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 }

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Enums FTWMichel Weststratebefore adoptionfd76f1c

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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.

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Enums FTWMichel Weststratebefore adoptionfd76f1c

lines 235–238why, one step back
235 if (
236 state.type === ProxyType.ProxyObject ||237 state.type === ProxyType.ProxyArray238 ) {

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 239–240why, one step back
239 const copy = (state.copy = shallowCopy(state.base))
240 each(state.drafts!, (key, value) => {

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Dropped Object.assignMichel Weststratebefore adoption79a1d2c

lines 242–243why, one step back
242 copy[key] = value
243 })

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 244why, one step back
244 state.drafts = undefined

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 249why, one step back
249 }

basic map tests succeedingMichel Weststratebefore adoption735c402

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

basic map tests succeedingMichel Weststratebefore adoption735c402

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 256–257why, one step back
256 }
257}