scope.js
This file
Path
src/scope.js
38 lines · 896 bytes · JavaScript
Last changed by
feat: support async recipesaleclarsonbefore adoption5fee518
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839import {DRAFT_STATE} from "./common"
/** Each scope represents a `produce` call. */export class ImmerScope { constructor(parent) { this.drafts = [] this.parent = parent
// 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()}