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
Provenance
- Commit
c182c1e- Tree
ed91b7a- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
073bc09
4 files changed+41 added−59 removed
modified:src/es5.ts+6 added-23 removed
24 unmodified lines25 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 lines70 }71 72 export 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- }91-76 const isArray = Array.isArray(base)77 const draft = clonePotentialDraft(base)78 79 each(draft, prop => {1 unmodified line81 })82 83 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 }98 99 createHiddenProperty(draft, DRAFT_STATE, state)116- scope.drafts!.push(draft)100 return draft101 }102 103 function revoke(this: ES5BaseState) {19 unmodified lines123 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 linesmodified:src/immer.ts+28 added-5 removed
15 unmodified lines16 shallowCopy,17 DRAFT_STATE,18 NOTHING,19 freeze,20- latest20+ latest,21+ isPlainObject,22+ DRAFTABLE23 } from "./common"24 import {ImmerScope} from "./scope"25 import {26 ImmerState,1 unmodified line28 IProduceWithPatches,29 Objectish,30 PatchListener,31 Draft,30- Patch32+ Patch,33+ Drafted34 } from "./types"35+import {proxyMap} from "./map"36+import {proxySet} from "./set"37 38 function verifyMinified() {}39 40 const configDefaults = {240 unmodified lines281 }282 return result !== NOTHING ? result : undefined283 }284 280- 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 value287+288+ let draft: Drafted289+290+ 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 value302+303+ const scope = parent ? parent.scope : ImmerScope.current!304+ scope.drafts.push(draft)305+ return draft306 }307 308 willFinalize(scope: ImmerScope, thing: any, isReplaced: boolean) {309 if (!this.useProxies) willFinalizeES5(scope, thing, isReplaced)160 unmodified linesmodified:src/proxy.ts+4 added-28 removed
26 unmodified lines27 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 lines55 * The second argument is the parent draft-state (used internally).56 */57 export 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- }67-68- const scope = parent ? parent.scope : ImmerScope.current!69-70- // 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- }81-61 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 lines96 traps = arrayTraps97 }98 99 const {revoke, proxy} = Proxy.revocable(target, traps)121-100 state.draft = proxy as any101 state.revoke = revoke124-125- scope.drafts!.push(proxy)102 return proxy as any103 }104 105 /**22 unmodified lines128 // @ts-ignore TODO: this line seems of?129 drafts = state.copy130 }131 156- 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 linesmodified:src/types.ts+3 added-3 removed
34 unmodified lines35 | ES5ArrayState36 | MapState37 | SetState38 39-export type Drafted<Base, T extends ImmerState> = {39+export type Drafted<Base = any, T extends ImmerState = ImmerState> = {40 [DRAFT_STATE]: T41 } & Base42 43-type Tail<T extends any[]> = ((...t: T) => any) extends ((43+type Tail<T extends any[]> = ((...t: T) => any) extends (44 _: any,45 ...tail: infer TT46-) => any)46+) => any47 ? TT48 : []49 50 /** Object types that should never be mapped */166 unmodified lines