Skip to content
Myr

refactor: rename createPublicDraft/finishPublicDraft for consistency

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

authored by aleclarson · committed by aleclarson · parent edda437 · Browse files · 5 files+34 added−19 removed
This commit

State

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

Diff stats

5 files+34 added−19 removed

  • other:+34 added−19 removed

5 files+34 added−19 removed

modified:src/es5.js+2 added-2 removed
26 unmodified lines
27 markChangesSweep(scope.drafts)28 }29}3031export function createDraft(base, parent) {31export function createProxy(base, parent) {32 const isArray = Array.isArray(base)33 const draft = clonePotentialDraft(base)34 each(draft, prop => {35 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))
33 unmodified lines
69 const value = source(state)[prop]70 // Drafts are only created for proxyable values that exist in the base state.71 if (!state.finalizing && value === state.base[prop] && isDraftable(value)) {72 prepareCopy(state)73 return (state.copy[prop] = createDraft(value, state))73 return (state.copy[prop] = createProxy(value, state))74 }75 return value76}77
158 unmodified lines
modified:src/immer.d.ts+8 added-5 removed
153 unmodified lines
154 */155export function applyPatches<S>(base: S, patches: Patch[]): S156157/**158 * Creates a mutable draft from an (immutable) object / array.159 * The draft can be modified until `finishDraft` is called158 * Create an Immer draft from the given base state, which may be a draft itself.159 * The draft can be modified until you finalize it with the `finishDraft` function.160 */161export function createDraft<T>(base: T): Draft<T>162163/**164 * Given a draft that was created using `createDraft`,165 * finalizes the draft into a new immutable object.166 * Optionally a patch-listener can be provided to gather the patches that are needed to construct the object.164 * Finalize an Immer draft from a `createDraft` call, returning the base state165 * (if no changes were made) or a modified copy. The draft must *not* be166 * mutated afterwards.167 *168 * Pass a function as the 2nd argument to generate Immer patches based on the169 * changes that were made.170 */171export function finishDraft<T>(base: Draft<T>, listener?: PatchListener): T172173/** Get the underlying object that is represented by the given draft */
69 unmodified lines
modified:src/immer.js+8 added-8 removed
54 unmodified lines
5556 // Only plain objects, arrays, and "immerable classes" are drafted.57 if (isDraftable(base)) {58 const scope = ImmerScope.enter()59 const baseDraft = this.createDraft(base)59 const proxy = this.createProxy(base)60 let hasError = true61 try {62 result = recipe.call(baseDraft, baseDraft)62 result = recipe.call(proxy, proxy)63 hasError = false64 } finally {65 // finally instead of catch + rethrow better preserves original stack66 if (hasError) scope.revoke()
18 unmodified lines
85 if (result === undefined) return base86 return result !== NOTHING ? result : undefined87 }88 }89 createPublicDraft(value) {90 if (!isDraftable(value)) throw new Error("First argument to createDraft should be a plain-, or immerable object, or array.") // prettier-ignore89 createDraft(base) {90 if (!isDraftable(base)) throw new Error("First argument to createDraft should be a plain object, an array, or an immerable object.") // prettier-ignore91 const scope = ImmerScope.enter()92 const draft = this.createDraft(value)92 const proxy = this.createProxy(base)93 scope.leave()94 return draft94 return proxy95 }96 finishPublicDraft(draft, patchListener) {97 if (!isDraft(draft)) throw new Error("First argument to finishDraft should be an object created with createDraft.") // prettier-ignore96 finishDraft(draft, patchListener) {97 if (!isDraft(draft)) throw new Error("First argument to finishDraft should be an object from createDraft.") // prettier-ignore98 const {scope} = draft[DRAFT_STATE]99 scope.usePatches(patchListener)100 return this.processResult(undefined, scope)101 }
174 unmodified lines
modified:src/index.js+14 added-2 removed
modified:src/proxy.js+2 added-2 removed
12 unmodified lines
1314// Do nothing before being finalized.15export function willFinalize() {}1617export function createDraft(base, parent) {17export function createProxy(base, parent) {18 const scope = parent ? parent.scope : ImmerScope.current19 const state = {20 // Track which produce call this is associated with.21 scope,
96 unmodified lines
118 // Store drafts on the copy (when one exists).119 drafts = state.copy120 }121122 return (drafts[prop] = createDraft(value, state))122 return (drafts[prop] = createProxy(value, state))123}124125function set(state, prop, value) {126 if (!state.modified) {
42 unmodified lines