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.
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
Provenance
- Commit
9c53415- Tree
c76f74d- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
483a27a
3 files changed+70 added−35 removed
modified:src/es5.js+17 added-8 removed
26 unmodified lines27 }28 }29 30 export 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-ignore32-33- 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 })44 45+ // 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 }59 60 createHiddenProperty(draft, DRAFT_STATE, state)52- currentScope().push(state)61+ state.scope.push(state)62 return draft63 }64 65 function revoke() {16 unmodified lines82 }83 84 function 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 linesmodified:src/immer.js+15 added-2 removed
127 unmodified linesExpand all1281291301311321331341351361371381392 unmodified lines14214314414514614714814914215015115215315415515620 unmodified lines17717817918018118218318417218518618718818913 unmodified lines127 unmodified linesExpand all if (!state) { if (Object.isFrozen(draft)) return draft return this.finalizeTree(draft) } // Never finalize drafts owned by an outer scope. if (state.scope !== this.currentScope()) { return draft } if (!state.modified) return state.base if (!state.finalized) { state.finalized = true this.finalizeTree(state.draft, path, patches, inversePatches)2 unmodified lines for (const prop in assigned) assigned[prop] || this.onDelete(state, prop) } if (this.onCopy) this.onCopy(state)
// Nested producers must never auto-freeze their result, // because it may contain drafts from parent producers. if (this.autoFreeze && this.scopes.length === 1) { if (this.autoFreeze) Object.freeze(state.copy) Object.freeze(state.copy) }
if (patches) generatePatches(state, path, patches, inversePatches) } return state.copy }20 unmodified lines // Patches are never generated for assigned properties. patches && inDraft && !state.assigned[prop] ? this.finalize(value, path.concat(prop), patches, inversePatches) : this.finalize(value)
// Unchanged drafts are ignored. if (inDraft && value === state.base[prop]) return } // Unchanged base state is always ignored. // Unchanged draft properties are ignored. else if (inDraft && is(value, state.base[prop])) { return } // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.13 unmodified linesmodified:src/proxy.js+38 added-25 removed
18 unmodified lines19 // Do nothing before being finalized.20 export function willFinalize() {}21 22 export 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-ignore24-23 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 }45 46 const {revoke, proxy} = Array.isArray(base)47 ? Proxy.revocable([state], arrayTraps)1 unmodified line49 50 state.draft = proxy51 state.revoke = revoke52 44- currentScope().push(state)53+ state.scope.push(state)54 return proxy55 }56 57 const objectTraps = {36 unmodified lines94 return objectTraps.set.call(this, state[0], prop, value)95 }96 97 function source(state) {89- return state.modified === true ? state.copy : state.base98+ return state.copy || state.base99 }100 101 function get(state, prop) {102 if (prop === DRAFT_STATE) return state103+ let {drafts} = state104+105+ // Check for existing draft in unmodified state.106+ if (!state.modified && has(drafts, prop)) {107+ return drafts[prop]108+ }109+110+ const value = source(state)[prop]111+ if (state.finalized || !isDraftable(value)) return value112+113+ // 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 }120+121+ return (drafts[prop] = createDraft(value, state))122 }123 124 function set(state, prop, value) {125 if (!state.modified) {41 unmodified lines167 168 function 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 }