scope.js
This file
Path
src/scope.js
42 lines · 1.1 KiB · JavaScript
Last changed by
fix: recursive produce call in async recipealeclarsonbefore adoption327d659
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243import {DRAFT_STATE} from "./common"
/** Each scope represents a `produce` call. */export class ImmerScope { 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) { if (patchListener) { this.patches = [] this.inversePatches = [] this.patchListener = patchListener } } revoke() { this.leave() this.drafts.forEach(revoke) this.drafts = null // Make draft-related methods throw. } leave() { if (this === ImmerScope.current) { ImmerScope.current = this.parent } }}
ImmerScope.current = nullImmerScope.enter = function() { return (this.current = new ImmerScope(this.current))}
function revoke(draft) { draft[DRAFT_STATE].revoke()}