Skip to content
Myr

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

4 files+49 added−42 removed

modified:src/plugins/mapset/set.ts+3 added-4 removed
modified:src/finalize.ts+3 added-2 removed
16 unmodified lines
17 isDraft,18 SetState,19 set,20 is,21 get21 get,22 willFinalize23} from "./internal"2425export 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-ignore
176 unmodified lines
modified:src/immer.ts+35 added-29 removed
22 unmodified lines
23 isMap,24 proxyMap,25 isSet,26 proxySet,27 createProxy,28 markChanged27 markChangedProxy,28 createProxyProxy29} from "./internal"3031/* istanbul ignore next */32function verifyMinified() {}
94 unmodified lines
127128 // 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 = false
45 unmodified lines
181 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 lines
243 return this.produce(base, (draft: Drafted) =>244 applyPatches(draft, patches.slice(i + 1))245 )246 }247}248249export 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)262261 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}267268export 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}276270 markChanged(state: ImmerState) {271 if (this.useProxies) {272 markChanged(state)273 } else {274 markChangedES5(state)275 }277export 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 lines
13 AnyObject,14 AnyArray,15 Objectish,16 ImmerScope,17 DRAFT_STATE17 DRAFT_STATE,18 createProxy19} from "./internal"2021interface ProxyBaseState extends ImmerBaseState {22 assigned: {
26 unmodified lines
49 * Returns a new draft of the `base` object.50 *51 * The second argument is the parent draft-state (used internally).52 */52export function createProxy<T extends Objectish>(53export function createProxyProxy<T extends Objectish>(54 base: T,55 parent?: ImmerState56): Drafted<T, ProxyState> {57 const isArray = Array.isArray(base)
69 unmodified lines
127 // @ts-ignore128 drafts = state.copy129 }130130 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 lines
146 ? 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] = value
3 unmodified lines
158 // 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 lines
229 )230 return desc && desc.value231}232232export function markChanged(state: ImmerState) {233export function markChangedProxy(state: ImmerState) {234 if (!state.modified) {235 state.modified = true236 if (237 state.type === ProxyType.ProxyObject ||
7 unmodified lines
245 state.drafts = undefined246 }247248 if (state.parent) {248 markChanged(state.parent)249 markChangedProxy(state.parent)250 }251 }252}253
5 unmodified lines