proxy.ts
This file
Path
src/core/proxy.ts
292 lines · 7.6 KiB · TypeScript
Last changed by
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
History of this fileTake it
The walk stopped after 200 commits: lines older than 894d190 point at it.
chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 1–14why, one step back1import {2 each,3 has,4 is,5 isDraftable,6 shallowCopy,7 latest,8 ImmerBaseState,9 ImmerState,10 Drafted,11 AnyObject,12 AnyArray,13 Objectish,14 getCurrentScope,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 15why, one step back15 getPrototypeOf,chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 16–18why, one step back16 DRAFT_STATE,17 die,18 createProxy,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 19–20why, one step back19 ArchType,20 ImmerScopechore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 21–31why, one step back21} from "../internal"2223interface ProxyBaseState extends ImmerBaseState {24 assigned_: {25 [property: string]: boolean26 }27 parent_?: ImmerState28 revoke_(): void29}3031export interface ProxyObjectState extends ProxyBaseState {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 32why, one step back32 type_: ArchType.Objectchore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 33–38why, one step back33 base_: any34 copy_: any35 draft_: Drafted<AnyObject, ProxyObjectState>36}3738export interface ProxyArrayState extends ProxyBaseState {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 39why, one step back39 type_: ArchType.Arraychore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 40–57why, one step back40 base_: AnyArray41 copy_: AnyArray | null42 draft_: Drafted<AnyArray, ProxyArrayState>43}4445type ProxyState = ProxyObjectState | ProxyArrayState4647/**48 * Returns a new draft of the `base` object.49 *50 * The second argument is the parent draft-state (used internally).51 */52export function createProxyProxy<T extends Objectish>(53 base: T,54 parent?: ImmerState55): Drafted<T, ProxyState> {56 const isArray = Array.isArray(base)57 const state: ProxyState = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 58why, one step back58 type_: isArray ? ArchType.Array : (ArchType.Object as any),chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 59–118why, one step back59 // Track which produce call this is associated with.60 scope_: parent ? parent.scope_ : getCurrentScope()!,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_: parent,69 // The base state.70 base_: base,71 // The base proxy.72 draft_: null as any, // set below73 // The base copy with any updated values.74 copy_: null,75 // Called by the `produce` function.76 revoke_: null as any,77 isManual_: false78 }7980 // the traps must target something, a bit like the 'real' base.81 // but also, we need to be able to determine from the target what the relevant state is82 // (to avoid creating traps per instance to capture the state in closure,83 // and to avoid creating weird hidden properties as well)84 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)85 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb86 let target: T = state as any87 let traps: ProxyHandler<object | Array<any>> = objectTraps88 if (isArray) {89 target = [state] as any90 traps = arrayTraps91 }9293 const {revoke, proxy} = Proxy.revocable(target, traps)94 state.draft_ = proxy as any95 state.revoke_ = revoke96 return proxy as any97}9899/**100 * Object drafts101 */102export const objectTraps: ProxyHandler<ProxyState> = {103 get(state, prop) {104 if (prop === DRAFT_STATE) return state105106 const source = latest(state)107 if (!has(source, prop)) {108 // non-existing or non-own property...109 return readPropFromProto(state, source, prop)110 }111 const value = source[prop]112 if (state.finalized_ || !isDraftable(value)) {113 return value114 }115 // Check for existing draft in modified state.116 // Assigned values are never drafted. This catches any drafts we created, too.117 if (value === peek(state.base_, prop)) {118 prepareCopy(state)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 119why, one step back119 return (state.copy_![prop as any] = createProxy(value, state))chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 120–141why, one step back120 }121 return value122 },123 has(state, prop) {124 return prop in latest(state)125 },126 ownKeys(state) {127 return Reflect.ownKeys(latest(state))128 },129 set(130 state: ProxyObjectState,131 prop: string /* strictly not, but helps TS */,132 value133 ) {134 const desc = getDescriptorFromProto(latest(state), prop)135 if (desc?.set) {136 // special case: if this write is captured by a setter, we have137 // to trigger it with the correct context138 desc.set.call(state.draft_, value)139 return true140 }141 if (!state.modified_) {chore: Fix typo in comment (#776)inokawabefore adoptionffa3972
line 142why, one step back142 // the last check is because we need to be able to distinguish setting a non-existing to undefined (which is a change)chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 143–156why, one step back143 // from setting an existing property with value undefined to undefined (which is not a change)144 const current = peek(latest(state), prop)145 // special case, if we assigning the original value to a draft, we can ignore the assignment146 const currentState: ProxyObjectState = current?.[DRAFT_STATE]147 if (currentState && currentState.base_ === value) {148 state.copy_![prop] = value149 state.assigned_[prop] = false150 return true151 }152 if (is(value, current) && (value !== undefined || has(state.base_, prop)))153 return true154 prepareCopy(state)155 markChanged(state)156 }fix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d
line 157why, one step back157fix: #807 new undefined properties should end up in result objectMichel Weststratebefore adoptiondc3f66c
line 158why, one step back158 if (fix: special case NaN in setter (#912)Raybefore adoption5721bb7
lines 159–161why, one step back159 (state.copy_![prop] === value &&160 // special case: handle new props with value 'undefined'161 (value !== undefined || prop in state.copy_)) ||fix: #807 new undefined properties should end up in result objectMichel Weststratebefore adoptiondc3f66c
line 162why, one step back162 // special case: NaNfix: special case NaN in setter (#912)Raybefore adoption5721bb7
line 163why, one step back163 (Number.isNaN(value) && Number.isNaN(state.copy_![prop]))fix: #807 new undefined properties should end up in result objectMichel Weststratebefore adoptiondc3f66c
lines 164–165why, one step back164 )165 return truefix: Ignore equal reference assignments. Fixes #648Michel Weststratebefore adoption3b4286d
line 166why, one step back166chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 167–181why, one step back167 // @ts-ignore168 state.copy_![prop] = value169 state.assigned_[prop] = true170 return true171 },172 deleteProperty(state, prop: string) {173 // The `undefined` check is a fast path for pre-existing keys.174 if (peek(state.base_, prop) !== undefined || prop in state.base_) {175 state.assigned_[prop] = false176 prepareCopy(state)177 markChanged(state)178 } else {179 // if an originally not assigned property was deleted180 delete state.assigned_[prop]181 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 182–184why, one step back182 if (state.copy_) {183 delete state.copy_[prop]184 }chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 185–194why, one step back185 return true186 },187 // Note: We never coerce `desc.value` into an Immer draft, because we can't make188 // the same guarantee in ES5 mode.189 getOwnPropertyDescriptor(state, prop) {190 const owner = latest(state)191 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)192 if (!desc) return desc193 return {194 writable: true,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 195why, one step back195 configurable: state.type_ !== ArchType.Array || prop !== "length",chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 196–203why, one step back196 enumerable: desc.enumerable,197 value: owner[prop]198 }199 },200 defineProperty() {201 die(11)202 },203 getPrototypeOf(state) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 204why, one step back204 return getPrototypeOf(state.base_)chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 205–223why, one step back205 },206 setPrototypeOf() {207 die(12)208 }209}210211/**212 * Array drafts213 */214215const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}216each(objectTraps, (key, fn) => {217 // @ts-ignore218 arrayTraps[key] = function() {219 arguments[0] = arguments[0][0]220 return fn.apply(this, arguments)221 }222})223arrayTraps.deleteProperty = function(state, prop) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 224–225why, one step back224 if (process.env.NODE_ENV !== "production" && isNaN(parseInt(prop as any)))225 die(13)fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 226–227why, one step back226 // @ts-ignore227 return arrayTraps.set!.call(this, state, prop, undefined)chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 228–229why, one step back228}229arrayTraps.set = function(state, prop, value) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 230–235why, one step back230 if (231 process.env.NODE_ENV !== "production" &&232 prop !== "length" &&233 isNaN(parseInt(prop as any))234 )235 die(14)chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 236–262why, one step back236 return objectTraps.set!.call(this, state[0], prop, value, state[0])237}238239// Access a property without creating an Immer draft.240function peek(draft: Drafted, prop: PropertyKey) {241 const state = draft[DRAFT_STATE]242 const source = state ? latest(state) : draft243 return source[prop]244}245246function readPropFromProto(state: ImmerState, source: any, prop: PropertyKey) {247 const desc = getDescriptorFromProto(source, prop)248 return desc249 ? `value` in desc250 ? desc.value251 : // This is a very special case, if the prop is a getter defined by the252 // prototype, we should invoke it with the draft as context!253 desc.get?.call(state.draft_)254 : undefined255}256257function getDescriptorFromProto(258 source: any,259 prop: PropertyKey260): PropertyDescriptor | undefined {261 // 'in' checks proto!262 if (!(prop in source)) return undefinedMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 263why, one step back263 let proto = getPrototypeOf(source)chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 264–266why, one step back264 while (proto) {265 const desc = Object.getOwnPropertyDescriptor(proto, prop)266 if (desc) return descMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 267why, one step back267 proto = getPrototypeOf(proto)chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 268–280why, one step back268 }269 return undefined270}271272export function markChanged(state: ImmerState) {273 if (!state.modified_) {274 state.modified_ = true275 if (state.parent_) {276 markChanged(state.parent_)277 }278 }279}280Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 281–285why, one step back281export function prepareCopy(state: {282 base_: any283 copy_: any284 scope_: ImmerScope285}) {chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
line 286why, one step back286 if (!state.copy_) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 287–290why, one step back287 state.copy_ = shallowCopy(288 state.base_,289 state.scope_.immer_.useStrictShallowCopy_290 )chore(deps): bump highlight.js from 9.15.10 to 9.18.5 in /website (#709)dependabot[bot]before adoption894d190
lines 291–292why, one step back291 }292}