Skip to content
Myr

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 file

Take it

feat: support async recipesaleclarsonbefore adoption5fee518

lines 1–8why, 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 = parent8

fix: recursive produce call in async recipealeclarsonbefore adoption327d659

lines 9–12why, one step back
9 // Whenever the modified draft contains a draft from another scope, we
10 // need to prevent auto-freezing so the unowned draft can be finalized.11 this.canAutoFreeze = true12

feat: support async recipesaleclarsonbefore adoption5fee518

lines 13–42why, one step back
13 // To avoid prototype lookups:
14 this.patches = null15 }16 usePatches(patchListener) {17 if (patchListener) {18 this.patches = []19 this.inversePatches = []20 this.patchListener = patchListener21 }22 }23 revoke() {24 this.leave()25 this.drafts.forEach(revoke)26 this.drafts = null // Make draft-related methods throw.27 }28 leave() {29 if (this === ImmerScope.current) {30 ImmerScope.current = this.parent31 }32 }33}3435ImmerScope.current = null36ImmerScope.enter = function() {37 return (this.current = new ImmerScope(this.current))38}3940function revoke(draft) {41 draft[DRAFT_STATE].revoke()42}