Skip to content
Myr

scope.js

This file

Path

src/scope.js

42 lines · 1.1 KiB · JavaScript

Last changed by

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

History of this file

Take it

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 1–42why, 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 // Whenever the modified draft contains a draft from another scope, we10 // need to prevent auto-freezing so the unowned draft can be finalized.11 this.canAutoFreeze = true1213 // 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}