Small code cleanup
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 124518b · Browse files · 4 files·+49 added−42 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+49 added−42 removed
- other:+49 added−42 removed
Provenance
- Commit
9217809- Tree
82462cc- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
124518b
4 files changed+49 added−42 removed
modified:src/plugins/mapset/set.ts+3 added-4 removed
12334656789101114121314151617126 unmodified linesExpand all1441451461471491481491501511524 unmodified linesimport {__extends} from "./extends"import { ImmerBaseState, ProxyType, AnySet, Drafted, ImmerState, DRAFT_STATE, ImmerScope, latest, assertUnrevoked, iteratorSymbol, isDraftable, SetState SetState, createProxy} from "../../internal"
const DraftSet = (function(_super) { if (!_super) {126 unmodified linesExpand all // create drafts for all entries to preserve insertion order state.copy = new Set() state.base.forEach(value => { if (isDraftable(value)) { const draft = state.scope.immer.createProxy(value, state) const draft = createProxy(state.scope.immer, value, state) state.drafts.set(value, draft) state.copy!.add(draft) } else { state.copy!.add(value)4 unmodified linesmodified:src/finalize.ts+3 added-2 removed
16 unmodified lines17 isDraft,18 SetState,19 set,20 is,21- get21+ get,22+ willFinalize23 } from "./internal"24 25 export function processResult(immer: Immer, result: any, scope: ImmerScope) {26 const baseDraft = scope.drafts![0]27 const isReplaced = result !== undefined && result !== baseDraft27- immer.willFinalize(scope, result, isReplaced)28+ willFinalize(immer, scope, result, isReplaced)29 if (isReplaced) {30 if (baseDraft[DRAFT_STATE].modified) {31 scope.revoke()32 throw new Error("An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore176 unmodified linesmodified:src/immer.ts+35 added-29 removed
22 unmodified lines23 isMap,24 proxyMap,25 isSet,26 proxySet,27- createProxy,28- markChanged27+ markChangedProxy,28+ createProxyProxy29 } from "./internal"30 31 /* istanbul ignore next */32 function verifyMinified() {}94 unmodified lines127 128 // Only plain objects, arrays, and "immerable classes" are drafted.129 if (isDraftable(base)) {130 const scope = ImmerScope.enter(this)131- const proxy = this.createProxy(base, undefined)131+ const proxy = createProxy(this, base, undefined)132 let hasError = true133 try {134 result = recipe(proxy)135 hasError = false45 unmodified lines181 if (!isDraftable(base)) {182 throw new Error("First argument to `createDraft` must be a plain object, an array, or an immerable object") // prettier-ignore183 }184 const scope = ImmerScope.enter(this)185- const proxy = this.createProxy(base, undefined)185+ const proxy = createProxy(this, base, undefined)186 proxy[DRAFT_STATE].isManual = true187 scope.leave()188 return proxy as any189 }53 unmodified lines243 return this.produce(base, (draft: Drafted) =>244 applyPatches(draft, patches.slice(i + 1))245 )246 }247+}248 249+export function createProxy<T extends Objectish>(248- createProxy<T extends Objectish>(249- value: T,250- parent?: ImmerState251- ): Drafted<T, ImmerState> {252- // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft253- const draft: Drafted = isMap(value)254- ? proxyMap(value, parent)255- : isSet(value)256- ? proxySet(value, parent)257- : this.useProxies258- ? createProxy(value, parent)259- : createES5Proxy(value, parent)250+ immer: Immer,251+ value: T,252+ parent?: ImmerState253+): Drafted<T, ImmerState> {254+ // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft255+ const draft: Drafted = isMap(value)256+ ? proxyMap(value, parent)257+ : isSet(value)258+ ? proxySet(value, parent)259+ : immer.useProxies260+ ? createProxyProxy(value, parent)261+ : createES5Proxy(value, parent)262 261- const scope = parent ? parent.scope : ImmerScope.current!262- scope.drafts.push(draft)263- return draft264- }263+ const scope = parent ? parent.scope : ImmerScope.current!264+ scope.drafts.push(draft)265+ return draft266+}267 268+export function willFinalize(269+ immer: Immer,270+ scope: ImmerScope,271+ thing: any,272+ isReplaced: boolean266- willFinalize(scope: ImmerScope, thing: any, isReplaced: boolean) {267- if (!this.useProxies) willFinalizeES5(scope, thing, isReplaced)268- }273+) {274+ if (!immer.useProxies) willFinalizeES5(scope, thing, isReplaced)275+}276 270- markChanged(state: ImmerState) {271- if (this.useProxies) {272- markChanged(state)273- } else {274- markChangedES5(state)275- }277+export function markChanged(immer: Immer, state: ImmerState) {278+ if (immer.useProxies) {279+ markChangedProxy(state)280+ } else {281+ markChangedES5(state)282 }283 }modified:src/proxy.ts+8 added-7 removed
12 unmodified lines13 AnyObject,14 AnyArray,15 Objectish,16 ImmerScope,17- DRAFT_STATE17+ DRAFT_STATE,18+ createProxy19 } from "./internal"20 21 interface ProxyBaseState extends ImmerBaseState {22 assigned: {26 unmodified lines49 * Returns a new draft of the `base` object.50 *51 * The second argument is the parent draft-state (used internally).52 */52-export function createProxy<T extends Objectish>(53+export function createProxyProxy<T extends Objectish>(54 base: T,55 parent?: ImmerState56 ): Drafted<T, ProxyState> {57 const isArray = Array.isArray(base)69 unmodified lines127 // @ts-ignore128 drafts = state.copy129 }130 130- return (drafts![prop as any] = state.scope.immer.createProxy(value, state))131+ return (drafts![prop as any] = createProxy(state.scope.immer, value, state))132 },133 has(state, prop) {134 return prop in latest(state)135 },10 unmodified lines146 ? is(baseValue, value) || value === state.drafts![prop]147 : is(baseValue, value) && prop in state.base148 if (isUnchanged) return true149 prepareCopy(state)149- markChanged(state)150+ markChangedProxy(state)151 }152 state.assigned[prop] = true153 // @ts-ignore154 state.copy![prop] = value3 unmodified lines158 // The `undefined` check is a fast path for pre-existing keys.159 if (peek(state.base, prop) !== undefined || prop in state.base) {160 state.assigned[prop] = false161 prepareCopy(state)161- markChanged(state)162+ markChangedProxy(state)163 } else if (state.assigned[prop]) {164 // if an originally not assigned property was deleted165 delete state.assigned[prop]166 }62 unmodified lines229 )230 return desc && desc.value231 }232 232-export function markChanged(state: ImmerState) {233+export function markChangedProxy(state: ImmerState) {234 if (!state.modified) {235 state.modified = true236 if (237 state.type === ProxyType.ProxyObject ||7 unmodified lines245 state.drafts = undefined246 }247 248 if (state.parent) {248- markChanged(state.parent)249+ markChangedProxy(state.parent)250 }251 }252 }253 5 unmodified lines