proxy.ts
This file
Path
src/proxy.ts
257 lines · 6.8 KiB · TypeScript
Last changed by
Enums FTWMichel Weststratebefore adoptionfd76f1c
History of this fileTake it
Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 1–8why, one step back1"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 back9 latestMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 10–11why, one step back10} from "./common"11import {ImmerScope} from "./scope"Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 12–17why, one step back12import {13 AnyObject,14 Drafted,15 ImmerState,16 AnyArray,17 Objectish,Enums FTWMichel Weststratebefore adoptionfd76f1c
lines 18–19why, one step back18 ImmerBaseState,19 ProxyTypeProcessed bunch of todosMichel Weststratebefore adoption00425e1
line 20why, one step back20} from "./types"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 21why, one step back21Processed bunch of todosMichel Weststratebefore adoption00425e1
line 22why, one step back22interface ProxyBaseState extends ImmerBaseState {Introduced proper state typesMichel Weststratebefore adoption3842f85
lines 23–25why, one step back23 assigned: {24 [property: string]: boolean25 }clean up create proxyMichel Weststratebefore adoptionc182c1e
line 26why, one step back26 parent?: ImmerStateProcessed bunch of todosMichel Weststratebefore adoption00425e1
line 27why, one step back27 drafts?: {Introduced proper state typesMichel Weststratebefore adoption3842f85
lines 28–29why, one step back28 [property: string]: Drafted<any, any>29 }Processed bunch of todosMichel Weststratebefore adoption00425e1
line 30why, one step back30 revoke(): voidMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 31–32why, one step back31}32Introduced proper state typesMichel Weststratebefore adoption3842f85
line 33why, one step back33export interface ProxyObjectState extends ProxyBaseState {Enums FTWMichel Weststratebefore adoptionfd76f1c
line 34why, one step back34 type: ProxyType.ProxyObjectIntroduced proper state typesMichel Weststratebefore adoption3842f85
lines 35–40why, one step back35 base: AnyObject36 copy: AnyObject | null37 draft: Drafted<AnyObject, ProxyObjectState>38}3940export interface ProxyArrayState extends ProxyBaseState {Enums FTWMichel Weststratebefore adoptionfd76f1c
line 41why, one step back41 type: ProxyType.ProxyArrayIntroduced proper state typesMichel Weststratebefore adoption3842f85
lines 42–46why, one step back42 base: AnyArray43 copy: AnyArray | null44 draft: Drafted<AnyArray, ProxyArrayState>45}46TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 47–48why, one step back47type ProxyState = ProxyObjectState | ProxyArrayState48Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 49–53why, one step back49/**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 back54export function createProxy<T extends Objectish>(Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 55why, one step back55 base: T,clean up create proxyMichel Weststratebefore adoptionc182c1e
line 56why, one step back56 parent?: ImmerStateTS type improvementsMichel Weststratebefore adoptiondd1808e
line 57why, one step back57): Drafted<T, ProxyState> {Introduced proper state typesMichel Weststratebefore adoption3842f85
line 58why, one step back58 const isArray = Array.isArray(base)TS type improvementsMichel Weststratebefore adoptiondd1808e
line 59why, one step back59 const state: ProxyState = {Enums FTWMichel Weststratebefore adoptionfd76f1c
line 60why, one step back60 type: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 61why, one step back61 // Track which produce call this is associated with.clean up create proxyMichel Weststratebefore adoptionc182c1e
line 62why, one step back62 scope: parent ? parent.scope : ImmerScope.current!,Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 63–73why, one step back63 // 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 back74 draft: null as any, // set belowMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 75–79why, one step back75 // 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 back80 revoke: null as any,81 isManual: falseMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 82–91why, one step back82 }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>> = objectTrapsSeveral refactoringsMichel Weststratebefore adoptionc7f8f5b
line 92why, one step back92 if (isArray) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 93–95why, one step back93 target = [state] as any94 traps = arrayTraps95 }basic map tests succeedingMichel Weststratebefore adoption735c402
line 96why, one step back96Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 97why, one step back97 const {revoke, proxy} = Proxy.revocable(target, traps)Introduced proper state typesMichel Weststratebefore adoption3842f85
line 98why, one step back98 state.draft = proxy as anyMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 99why, one step back99 state.revoke = revokeIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 100why, one step back100 return proxy as anyMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 101–105why, one step back101}102103/**104 * Object drafts105 */TS type improvementsMichel Weststratebefore adoptiondd1808e
line 106why, one step back106const objectTraps: ProxyHandler<ProxyState> = {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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)) {Processed bunch of todosMichel Weststratebefore adoption00425e1
line 113why, one step back113 return drafts![prop as any]Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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).Processed bunch of todosMichel Weststratebefore adoption00425e1
line 126why, one step back126 // @ts-ignoreMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 127–129why, one step back127 drafts = state.copy128 }129Processed bunch of todosMichel Weststratebefore adoption00425e1
line 130why, one step back130 return (drafts![prop as any] = state.scope.immer.createProxy(value, state))Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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 },Introduced proper state typesMichel Weststratebefore adoption3842f85
line 138why, one step back138 set(state, prop: string /* strictly not, but helps TS */, value) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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 = valueProcessed bunch of todosMichel Weststratebefore adoption00425e1
line 145why, one step back145 ? is(baseValue, value) || value === state.drafts![prop]Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 146–147why, one step back146 : is(baseValue, value) && prop in state.base147 if (isUnchanged) return truebasic map tests succeedingMichel Weststratebefore adoption735c402
line 148why, one step back148 prepareCopy(state)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 149–151why, one step back149 markChanged(state)150 }151 state.assigned[prop] = trueTS type improvementsMichel Weststratebefore adoptiondd1808e
line 152why, one step back152 // @ts-ignoreIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 153why, one step back153 state.copy![prop] = valueMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 154–155why, one step back154 return true155 },Introduced proper state typesMichel Weststratebefore adoption3842f85
line 156why, one step back156 deleteProperty(state, prop: string) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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] = falsebasic map tests succeedingMichel Weststratebefore adoption735c402
line 160why, one step back160 prepareCopy(state)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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 }TS type improvementsMichel Weststratebefore adoptiondd1808e
line 166why, one step back166 // @ts-ignoreMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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 = trueEnums FTWMichel Weststratebefore adoptionfd76f1c
lines 177–178why, one step back177 desc.configurable =178 state.type !== ProxyType.ProxyArray || prop !== "length"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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 */196Introduced proper state typesMichel Weststratebefore adoption3842f85
line 197why, one step back197const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 198why, one step back198each(objectTraps, (key, fn) => {TS type improvementsMichel Weststratebefore adoptiondd1808e
line 199why, one step back199 // @ts-ignoreMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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.TS type improvementsMichel Weststratebefore adoptiondd1808e
line 223why, one step back223function peek(draft: Drafted, prop: PropertyKey): any {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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}231TS type improvementsMichel Weststratebefore adoptiondd1808e
line 232why, one step back232export function markChanged(state: ImmerState) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 233–234why, one step back233 if (!state.modified) {234 state.modified = trueEnums FTWMichel Weststratebefore adoptionfd76f1c
lines 235–238why, one step back235 if (236 state.type === ProxyType.ProxyObject ||237 state.type === ProxyType.ProxyArray238 ) {Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 239–240why, one step back239 const copy = (state.copy = shallowCopy(state.base))240 each(state.drafts!, (key, value) => {TS type improvementsMichel Weststratebefore adoptiondd1808e
line 241why, one step back241 // @ts-ignoreDropped Object.assignMichel Weststratebefore adoption79a1d2c
lines 242–243why, one step back242 copy[key] = value243 })Processed bunch of todosMichel Weststratebefore adoption00425e1
line 244why, one step back244 state.drafts = undefinedMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 245–246why, one step back245 }246Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 247–248why, one step back247 if (state.parent) {248 markChanged(state.parent)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 249why, one step back249 }basic map tests succeedingMichel Weststratebefore adoption735c402
lines 250–252why, one step back250 }251}252TS type improvementsMichel Weststratebefore adoptiondd1808e
line 253why, one step back253function prepareCopy(state: ProxyState) {basic map tests succeedingMichel Weststratebefore adoption735c402
lines 254–255why, one step back254 if (!state.copy) {255 state.copy = shallowCopy(state.base)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 256–257why, one step back256 }257}