scope.ts
This file
Path
src/scope.ts
57 lines · 1.2 KiB · TypeScript
Last changed by
Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
History of this fileTake it
Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 1–57why, one step back1import {DRAFT_STATE} from "./common"2import {ImmerState, Patch, PatchListener} from "./types"34/** Each scope represents a `produce` call. */5export class ImmerScope {6 static current?: ImmerScope78 patches?: Patch[]9 inversePatches?: Patch[]10 canAutoFreeze: boolean11 drafts: any[]12 parent?: ImmerScope13 patchListener?: PatchListener1415 constructor(parent?: ImmerScope) {16 this.drafts = []17 this.parent = parent1819 // Whenever the modified draft contains a draft from another scope, we20 // need to prevent auto-freezing so the unowned draft can be finalized.21 this.canAutoFreeze = true2223 // To avoid prototype lookups:24 this.patches = null as any // TODO:25 }2627 usePatches(patchListener: PatchListener) {28 if (patchListener) {29 this.patches = []30 this.inversePatches = []31 this.patchListener = patchListener32 }33 }3435 revoke() {36 this.leave()37 this.drafts.forEach(revoke)38 // @ts-ignore39 this.drafts = null // TODO: // Make draft-related methods throw.40 }4142 leave() {43 if (this === ImmerScope.current) {44 ImmerScope.current = this.parent45 }46 }4748 static enter() {49 const scope = new ImmerScope(ImmerScope.current)50 ImmerScope.current = scope51 return scope52 }53}5455function revoke(draft) {56 draft[DRAFT_STATE].revoke()57}