Skip to content
Myr

clean up create proxy

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent 073bc09 · Browse files · 4 files+41 added−59 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+41 added−59 removed

  • other:+41 added−59 removed

4 files+41 added−59 removed

modified:src/es5.ts+6 added-23 removed
24 unmodified lines
25 modified: boolean26 finalizing: boolean27 finalized: boolean28 assigned: {[key: string]: any}29 parent: ImmerState29 parent?: ImmerState30 drafts: AnyMap | null // TODO: don't use map?31 revoke()32 revoked: boolean33}
36 unmodified lines
70}7172export function createES5Proxy<T>(73 base: T,74 parent: ImmerState74 parent?: ImmerState75): Drafted<any, ES5ObjectState | ES5ArrayState> {76 if (!base || typeof base !== "object" || !isDraftable(base)) {77 // TODO: || isDraft ?78 return base as any // TODO: fix79 }80 const scope = parent ? parent.scope : ImmerScope.current!81 if (isMap(base)) {82 const draft = proxyMap(base, parent) as any // TODO: typefix83 scope.drafts.push(draft)84 return draft85 }86 if (isSet(base)) {87 const draft = proxySet(base, parent) as any // TODO: typefix88 scope.drafts.push(draft)89 return draft90 }9176 const isArray = Array.isArray(base)77 const draft = clonePotentialDraft(base)7879 each(draft, prop => {
1 unmodified line
81 })8283 const state: ES5ObjectState | ES5ArrayState = {84 type: isArray ? "es5_array" : ("es5_object" as any),101 scope,85 scope: parent ? parent.scope : ImmerScope.current!,86 modified: false,87 finalizing: false, // es5 only88 finalized: false,105 assigned: isMap(base) ? new Map() : {},89 assigned: {},90 parent,91 base,92 draft,109 drafts: isSet(base) ? new Map() : null,93 drafts: null,94 copy: null,95 revoke,96 revoked: false // es5 only97 }9899 createHiddenProperty(draft, DRAFT_STATE, state)116 scope.drafts!.push(draft)100 return draft101}102103function revoke(this: ES5BaseState) {
19 unmodified lines
123 if (state.finalizing) return value124 // Create a draft if the value is unmodified.125 if (value === peek(state.base, prop) && isDraftable(value)) {126 prepareCopy(state)144 return (state.copy[prop] = createES5Proxy(value, state))127 return (state.copy[prop] = state.scope.immer.createProxy(value, state))128 }129 return value130}131
219 unmodified lines
modified:src/immer.ts+28 added-5 removed
15 unmodified lines
16 shallowCopy,17 DRAFT_STATE,18 NOTHING,19 freeze,20 latest20 latest,21 isPlainObject,22 DRAFTABLE23} from "./common"24import {ImmerScope} from "./scope"25import {26 ImmerState,
1 unmodified line
28 IProduceWithPatches,29 Objectish,30 PatchListener,31 Draft,30 Patch32 Patch,33 Drafted34} from "./types"35import {proxyMap} from "./map"36import {proxySet} from "./set"3738function verifyMinified() {}3940const configDefaults = {
240 unmodified lines
281 }282 return result !== NOTHING ? result : undefined283 }284280 createProxy<T>(value: any, parent: any) {281 if (this.useProxies) return createProxy(value, parent)282 else return createES5Proxy(value, parent)285 createProxy<T>(value: any, parent?: ImmerState) {286 if (!value || typeof value !== "object") return value287288 let draft: Drafted289290 if (291 isPlainObject(value) ||292 Array.isArray(value) ||293 value[DRAFTABLE] ||294 value?.constructor?.[DRAFTABLE]295 ) {296 draft = this.useProxies297 ? createProxy(value, parent)298 : createES5Proxy(value, parent)299 } else if (isMap(value)) draft = proxyMap(value, parent)300 else if (isSet(value)) draft = proxySet(value, parent)301 else return value302303 const scope = parent ? parent.scope : ImmerScope.current!304 scope.drafts.push(draft)305 return draft306 }307308 willFinalize(scope: ImmerScope, thing: any, isReplaced: boolean) {309 if (!this.useProxies) willFinalizeES5(scope, thing, isReplaced)
160 unmodified lines
modified:src/proxy.ts+4 added-28 removed
26 unmodified lines
27 finalized: boolean28 assigned: {29 [property: string]: boolean30 }31 parent: ImmerState31 parent?: ImmerState32 drafts: {33 [property: string]: Drafted<any, any>34 }35 revoke: null | (() => void)
19 unmodified lines
55 * The second argument is the parent draft-state (used internally).56 */57export function createProxy<T extends object>(58 base: T,59 parent: ProxyObjectState | ProxyArrayState59 parent?: ImmerState60): Drafted<T, ProxyObjectState | ProxyArrayState> {61 // TODO: extract create proxy and refine62 // TODO: dedupe63 if (!base || typeof base !== "object" || !isDraftable(base)) {64 // TODO: || isDraft ?65 return base as any // TODO: fix66 }6768 const scope = parent ? parent.scope : ImmerScope.current!6970 // TODO: dedupe this, it is the same for ES571 if (isMap(base)) {72 const draft = proxyMap(base, parent) as any // TODO: typefix73 scope.drafts.push(draft)74 return draft75 }76 if (isSet(base)) {77 const draft = proxySet(base, parent) as any // TODO: typefix78 scope.drafts.push(draft)79 return draft80 }8161 const isArray = Array.isArray(base)62 const state: ProxyObjectState | ProxyArrayState = {63 type: isArray ? "proxy_array" : ("proxy_object" as any),64 // Track which produce call this is associated with.86 scope,65 scope: parent ? parent.scope : ImmerScope.current!,66 // True for both shallow and deep changes.67 modified: false,68 // Used during finalization.69 finalized: false,
26 unmodified lines
96 traps = arrayTraps97 }9899 const {revoke, proxy} = Proxy.revocable(target, traps)121100 state.draft = proxy as any101 state.revoke = revoke124125 scope.drafts!.push(proxy)102 return proxy as any103}104105/**
22 unmodified lines
128 // @ts-ignore TODO: this line seems of?129 drafts = state.copy130 }131156 return (drafts[prop as any] = createProxy(value, state))132 return (drafts[prop as any] = state.scope.immer.createProxy(value, state))133 },134 has(state, prop) {135 return prop in latest(state)136 },
141 unmodified lines
modified:src/types.ts+3 added-3 removed
34 unmodified lines
35 | ES5ArrayState36 | MapState37 | SetState3839export type Drafted<Base, T extends ImmerState> = {39export type Drafted<Base = any, T extends ImmerState = ImmerState> = {40 [DRAFT_STATE]: T41} & Base4243type Tail<T extends any[]> = ((...t: T) => any) extends ((43type Tail<T extends any[]> = ((...t: T) => any) extends (44 _: any,45 ...tail: infer TT46) => any)46) => any47 ? TT48 : []4950/** Object types that should never be mapped */
166 unmodified lines