Skip to content
Myr

scope.js

This file

Path

src/scope.js

38 lines · 896 bytes · JavaScript

Last changed by

feat: support async recipesaleclarsonbefore adoption5fee518

History of this file

Take it

feat: support async recipesaleclarsonbefore adoption5fee518

lines 1–38why, one step back
1import {DRAFT_STATE} from "./common"
23/** Each scope represents a `produce` call. */4export class ImmerScope {5 constructor(parent) {6 this.drafts = []7 this.parent = parent89 // To avoid prototype lookups:10 this.patches = null11 }12 usePatches(patchListener) {13 if (patchListener) {14 this.patches = []15 this.inversePatches = []16 this.patchListener = patchListener17 }18 }19 revoke() {20 this.leave()21 this.drafts.forEach(revoke)22 this.drafts = null // Make draft-related methods throw.23 }24 leave() {25 if (this === ImmerScope.current) {26 ImmerScope.current = this.parent27 }28 }29}3031ImmerScope.current = null32ImmerScope.enter = function() {33 return (this.current = new ImmerScope(this.current))34}3536function revoke(draft) {37 draft[DRAFT_STATE].revoke()38}