Skip to content
Myr

scope.ts

This file

Path

src/scope.ts

57 lines · 1.2 KiB · TypeScript

Last changed by

Making mosts tests passMichel Weststratebefore adoptionf8d4056

History of this file

Take it

TS migration in progressMichel Weststratebefore adoptiondd56639

lines 1–10why, one step back
1import {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: boolean

Making mosts tests passMichel Weststratebefore adoptionf8d4056

line 11why, one step back
11 drafts: any[]

TS migration in progressMichel Weststratebefore adoptiondd56639

lines 12–23why, one step back
12 parent?: ImmerScope
13 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:

Making mosts tests passMichel Weststratebefore adoptionf8d4056

line 24why, one step back
24 this.patches = null as any // TODO:

TS migration in progressMichel Weststratebefore adoptiondd56639

lines 25–36why, one step back
25 }
2627 usePatches(patchListener: PatchListener) {28 if (patchListener) {29 this.patches = []30 this.inversePatches = []31 this.patchListener = patchListener32 }33 }3435 revoke() {36 this.leave()

Making mosts tests passMichel Weststratebefore adoptionf8d4056

lines 37–39why, one step back
37 this.drafts.forEach(revoke)
38 // @ts-ignore39 this.drafts = null // TODO: // Make draft-related methods throw.

TS migration in progressMichel Weststratebefore adoptiondd56639

lines 40–48why, one step back
40 }
4142 leave() {43 if (this === ImmerScope.current) {44 ImmerScope.current = this.parent45 }46 }4748 static enter() {

Making mosts tests passMichel Weststratebefore adoptionf8d4056

lines 49–51why, one step back
49 const scope = new ImmerScope(ImmerScope.current)
50 ImmerScope.current = scope51 return scope

TS migration in progressMichel Weststratebefore adoptiondd56639

lines 52–57why, one step back
52 }
53}5455function revoke(draft) {56 draft[DRAFT_STATE].revoke()57}