Skip to content
Myr

feat: passing drafts to produce

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

Improve support for nested producers by allowing drafts to be passed as or in the base state. DETAILS: - Added `state.scope` for knowing when a draft is owned by the current producer or not. - Objects included in the return value of a nested producer are never auto-freezed, because they may contain drafts from a parent producer. - Drafts are always finalized by the producer that created them. - Now that `state.base` can be a draft, we must ensure `state.finalized` is false before creating drafts in the `get` handler. This only affects ES2015+ proxies.

authored by aleclarson · committed by Alec Larson · parent 483a27a · Browse files · 3 files+70 added−35 removed
This commit

State

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

Diff stats

3 files+70 added−35 removed

  • other:+70 added−35 removed

3 files+70 added−35 removed

modified:src/es5.js+17 added-8 removed
26 unmodified lines
27 }28}2930export function createDraft(base, parent) {31 if (isDraft(base)) throw new Error("This should never happen. Please report: https://github.com/mweststrate/immer/issues/new") // prettier-ignore3233 const draft = shallowCopy(base)31 let draft32 if (isDraft(base)) {33 const state = base[DRAFT_STATE]34 // Avoid creating new drafts when copying.35 state.finalizing = true36 draft = shallowCopy(state.draft)37 state.finalizing = false38 } else {39 draft = shallowCopy(base)40 }41 each(base, prop => {42 Object.defineProperty(draft, "" + prop, createPropertyProxy("" + prop))43 })4445 // See "proxy.js" for property documentation.46 const state = {47 scope: parent ? parent.scope : currentScope(),48 modified: false,40 finalizing: false,49 finalizing: false, // es5 only50 finalized: false,42 assigned: {}, // true: value was assigned to these props, false: was removed51 assigned: {},52 parent,53 base,54 draft,55 copy: null,56 revoke,48 revoked: false57 revoked: false // es5 only58 }5960 createHiddenProperty(draft, DRAFT_STATE, state)52 currentScope().push(state)61 state.scope.push(state)62 return draft63}6465function revoke() {
16 unmodified lines
82}8384function set(state, prop, value) {85 assertUnrevoked(state)77 state.assigned[prop] = true // optimization; skip this if there is no listener86 state.assigned[prop] = true87 if (!state.modified) {88 if (is(source(state)[prop], value)) return89 markChanged(state)90 prepareCopy(state)
137 unmodified lines
modified:src/immer.js+15 added-2 removed
modified:src/proxy.js+38 added-25 removed
18 unmodified lines
19// Do nothing before being finalized.20export function willFinalize() {}2122export function createDraft(base, parent) {23 if (isDraft(base)) throw new Error("This should never happen. Please report: https://github.com/mweststrate/immer/issues/new") // prettier-ignore2423 const state = {26 modified: false, // this tree is modified (either this object or one of it's children)27 assigned: {}, // true: value was assigned to these props, false: was removed24 // Track which produce call this is associated with.25 scope: parent ? parent.scope : currentScope(),26 // True for both shallow and deep changes.27 modified: false,28 // Used during finalization.29 finalized: false,30 // Track which properties have been assigned (true) or deleted (false).31 assigned: {},32 // The parent draft state.33 parent,34 // The base state.35 base,30 draft: null, // the root proxy31 drafts: {}, // proxied properties36 // The base proxy.37 draft: null,38 // Any property proxies.39 drafts: {},40 // The base copy with any updated values.41 copy: null,33 revoke: null,34 finalized: false42 // Called by the `produce` function.43 revoke: null44 }4546 const {revoke, proxy} = Array.isArray(base)47 ? Proxy.revocable([state], arrayTraps)
1 unmodified line
4950 state.draft = proxy51 state.revoke = revoke5244 currentScope().push(state)53 state.scope.push(state)54 return proxy55}5657const objectTraps = {
36 unmodified lines
94 return objectTraps.set.call(this, state[0], prop, value)95}9697function source(state) {89 return state.modified === true ? state.copy : state.base98 return state.copy || state.base99}100101function get(state, prop) {102 if (prop === DRAFT_STATE) return state103 let {drafts} = state104105 // Check for existing draft in unmodified state.106 if (!state.modified && has(drafts, prop)) {107 return drafts[prop]108 }109110 const value = source(state)[prop]111 if (state.finalized || !isDraftable(value)) return value112113 // Check for existing draft in modified state.114 if (state.modified) {95 const value = state.copy[prop]96 if (value === state.base[prop] && isDraftable(value))97 // only create proxy if it is not yet a proxy, and not a new object98 // (new objects don't need proxying, they will be processed in finalize anyway)99 return (state.copy[prop] = createDraft(value, state))100 return value101 } else {102 if (has(state.drafts, prop)) return state.drafts[prop]103 const value = state.base[prop]104 if (!isDraft(value) && isDraftable(value))105 return (state.drafts[prop] = createDraft(value, state))106 return value115 // Assigned values are never drafted. This catches any drafts we created, too.116 if (value !== state.base[prop]) return value117 // Store drafts on the copy (when one exists).118 drafts = state.copy119 }120121 return (drafts[prop] = createDraft(value, state))122}123124function set(state, prop, value) {125 if (!state.modified) {
41 unmodified lines
167168function markChanged(state) {169 if (!state.modified) {170 state.modified = true157 state.copy = shallowCopy(state.base)158 // copy the drafts over the base-copy159 assign(state.copy, state.drafts) // yup that works for arrays as well171 state.copy = assign(shallowCopy(state.base), state.drafts)172 state.drafts = null173 if (state.parent) markChanged(state.parent)174 }175}