fix: recursive produce call in async recipe
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by aleclarson · committed by aleclarson · parent e266f4b · Browse files · 2 files·+26 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+26 added−18 removed
- other:+26 added−18 removed
Provenance
- Commit
327d659- Tree
f61b481- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
e266f4b
2 files changed+26 added−18 removed
modified:src/immer.js+22 added-18 removed
173 unmodified lines174 if (this.onCopy) {175 this.onCopy(state)176 }177 178- // Nested producers must never auto-freeze their result,179- // because it may contain drafts from parent producers.180- if (this.autoFreeze && !scope.parent) {178+ // At this point, all descendants of `state.copy` have been finalized,179+ // so we can be sure that `scope.canAutoFreeze` is accurate.180+ if (this.autoFreeze && scope.canAutoFreeze) {181 Object.freeze(state.copy)182 }183 184 if (path && scope.patches) {10 unmodified lines195 /**196 * @internal197 * Finalize all drafts in the given state tree.198 */199- finalizeTree(root, path, scope) {199+ finalizeTree(root, rootPath, scope) {200 const state = root[DRAFT_STATE]201 if (state) {202 if (!this.useProxies) {203 state.finalizing = true2 unmodified lines206 }207 root = state.copy208 }209 210- const {onAssign} = this210+ const needPatches = !!rootPath && !!scope.patches211 const finalizeProperty = (prop, value, parent) => {212 if (value === parent) {213 throw Error("Immer forbids circular references")214 }215 216- // The only possible draft (in the scope of a `finalizeTree` call) is the `root` object.217- const inDraft = !!state && parent === root216+ // In the `finalizeTree` method, only the `root` object may be a draft.217+ const isDraftProp = !!state && parent === root218 219 if (isDraft(value)) {220- const needPatches =221- inDraft && path && scope.patches && !state.assigned[prop]220+ const path =221+ isDraftProp && needPatches && !state.assigned[prop]222+ ? rootPath.concat(prop)223+ : null224 223- value = this.finalize(224- value,225- needPatches ? path.concat(prop) : null,226- scope227- )225+ // Drafts owned by `scope` are finalized here.226+ value = this.finalize(value, path, scope)227 228+ // Drafts from another scope must prevent auto-freezing.229+ if (isDraft(value)) {230+ scope.canAutoFreeze = false231+ }232+233 // Preserve non-enumerable properties.234 if (Array.isArray(parent) || isEnumerable(parent, prop)) {235 parent[prop] = value236 } else {237 Object.defineProperty(parent, prop, {value})238 }239 240 // Unchanged drafts are never passed to the `onAssign` hook.237- if (inDraft && value === state.base[prop]) return241+ if (isDraftProp && value === state.base[prop]) return242 }243 // Unchanged draft properties are ignored.240- else if (inDraft && is(value, state.base[prop])) {244+ else if (isDraftProp && is(value, state.base[prop])) {245 return246 }247 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.248 else if (isDraftable(value) && !Object.isFrozen(value)) {249 each(value, finalizeProperty)250 }251 248- if (inDraft && onAssign) {249- onAssign(state, prop, value)252+ if (isDraftProp && this.onAssign) {253+ this.onAssign(state, prop, value)254 }255 }256 257 each(root, finalizeProperty)3 unmodified linesmodified:src/scope.js+4 added
4 unmodified lines567891011121314151626 unmodified lines4 unmodified lines constructor(parent) { this.drafts = [] this.parent = parent
// Whenever the modified draft contains a draft from another scope, we // need to prevent auto-freezing so the unowned draft can be finalized. this.canAutoFreeze = true
// To avoid prototype lookups: this.patches = null } usePatches(patchListener) {26 unmodified lines