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 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"Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 12–20why, one step back12import {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 back21Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 22–31why, one step back22interface 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 back32Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 33–38why, one step back33export 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 back39Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 40–44why, one step back40export 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 back45}46Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
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 */Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
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,Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 56–60why, one step back56 parent?: ImmerState57): 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 back61 // Track which produce call this is associated with.Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
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.Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
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.Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
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>> = objectTrapsMerge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
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 pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 97–98why, one step back97 // TODO: optimization: might be faster, cheaper if we created a non-revocable proxy98 // and administrate revoking ourselvesMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 99why, one step back99 const {revoke, proxy} = Proxy.revocable(target, traps)Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 100why, one step back100 state.draft = proxy as anyMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 101why, one step back101 state.revoke = revokeMerge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 102why, one step back102 return proxy as anyMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 103–107why, one step back103}104105/**106 * Object drafts107 */Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 108why, one step back108const objectTraps: ProxyHandler<ProxyState> = {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 109–114why, one step back109 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 back115 return drafts![prop as any]Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 116–127why, one step back116 }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 back128 // @ts-ignoreMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 129–131why, one step back129 drafts = state.copy130 }131Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 132why, one step back132 return (drafts![prop as any] = state.scope.immer.createProxy(value, state))Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 133–139why, one step back133 },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 back140 set(state, prop: string /* strictly not, but helps TS */, value) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 141–146why, one step back141 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 = valueMerge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 147why, one step back147 ? is(baseValue, value) || value === state.drafts![prop]Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 148–149why, one step back148 : is(baseValue, value) && prop in state.base149 if (isUnchanged) return truebasic map tests succeedingMichel Weststratebefore adoption735c402
line 150why, one step back150 prepareCopy(state)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 151–153why, one step back151 markChanged(state)152 }153 state.assigned[prop] = trueMerge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 154–155why, one step back154 // @ts-ignore155 state.copy![prop] = valueMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 156–157why, one step back156 return true157 },Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 158why, one step back158 deleteProperty(state, prop: string) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 159–161why, one step back159 // 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] = falsebasic map tests succeedingMichel Weststratebefore adoption735c402
line 162why, one step back162 prepareCopy(state)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 163–167why, one step back163 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 back168 // @ts-ignoreMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 169–178why, one step back169 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 = trueMerge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 179–180why, one step back179 desc.configurable =180 state.type !== ProxyType.ProxyArray || prop !== "length"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 181–198why, one step back181 }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 */198Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 199why, one step back199const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 200why, one step back200each(objectTraps, (key, fn) => {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 201why, one step back201 // @ts-ignoreMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 202–224why, one step back202 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 back225function peek(draft: Drafted, prop: PropertyKey): any {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 226–233why, one step back226 const state = draft[DRAFT_STATE]227 const desc = Reflect.getOwnPropertyDescriptor(228 state ? latest(state) : draft,229 prop230 )231 return desc && desc.value232}233Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 234why, one step back234export function markChanged(state: ImmerState) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 235–236why, one step back235 if (!state.modified) {236 state.modified = trueMerge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 237–246why, one step back237 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 = undefinedMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 247–248why, one step back247 }248Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 249–250why, one step back249 if (state.parent) {250 markChanged(state.parent)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 251why, one step back251 }basic map tests succeedingMichel Weststratebefore adoption735c402
lines 252–254why, one step back252 }253}254Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 255why, one step back255function prepareCopy(state: ProxyState) {basic map tests succeedingMichel Weststratebefore adoption735c402
lines 256–257why, one step back256 if (!state.copy) {257 state.copy = shallowCopy(state.base)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 258–259why, one step back258 }259}