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
Provenance
- Commit
44ef0c8- Tree
529293c- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
edda437
5 files changed+34 added−19 removed
modified:src/es5.js+2 added-2 removed
26 unmodified lines27 markChangesSweep(scope.drafts)28 }29 }30 31-export function createDraft(base, parent) {31+export 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 lines69 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 linesmodified:src/immer.d.ts+8 added-5 removed
153 unmodified lines154 */155 export function applyPatches<S>(base: S, patches: Patch[]): S156 157 /**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 */161 export function createDraft<T>(base: T): Draft<T>162 163 /**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 */171 export function finishDraft<T>(base: Draft<T>, listener?: PatchListener): T172 173 /** Get the underlying object that is represented by the given draft */69 unmodified linesmodified:src/immer.js+8 added-8 removed
54 unmodified lines55 56 // 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 lines85 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 linesmodified:src/index.js+14 added-2 removed
44 unmodified lines454647484950515249535455565758596061625163646566676 unmodified lines44 unmodified lines * This function is a producer, which means copy-on-write is in effect. */export const applyPatches = immer.applyPatches.bind(immer)
/** * Create an Immer draft from the given base state, which may be a draft itself. * The draft can be modified until you finalize it with the `finishDraft` function. */export const createDraft = immer.createPublicDraft.bind(immer)export const createDraft = immer.createDraft.bind(immer)
/** * Finalize an Immer draft from a `createDraft` call, returning the base state * (if no changes were made) or a modified copy. The draft must *not* be * mutated afterwards. * * Pass a function as the 2nd argument to generate Immer patches based on the * changes that were made. */export const finishDraft = immer.finishPublicDraft.bind(immer)export const finishDraft = immer.finishDraft.bind(immer)
export { original, isDraft,6 unmodified linesmodified:src/proxy.js+2 added-2 removed
12 unmodified lines13 14 // Do nothing before being finalized.15 export function willFinalize() {}16 17-export function createDraft(base, parent) {17+export 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 lines118 // Store drafts on the copy (when one exists).119 drafts = state.copy120 }121 122- return (drafts[prop] = createDraft(value, state))122+ return (drafts[prop] = createProxy(value, state))123 }124 125 function set(state, prop, value) {126 if (!state.modified) {42 unmodified lines