scope.ts
This file
Path
src/scope.ts
60 lines · 1.3 KiB · TypeScript
Last changed by
map unification WIPMichel Weststratebefore adoption9bcefb6
History of this fileTake it
Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 1–2why, one step back1import {DRAFT_STATE} from "./common"2import {ImmerState, Patch, PatchListener} from "./types"map unification WIPMichel Weststratebefore adoption9bcefb6
line 3why, one step back3import { Immer } from "./immer"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 4–14why, one step back45/** Each scope represents a `produce` call. */6export class ImmerScope {7 static current?: ImmerScope89 patches?: Patch[]10 inversePatches?: Patch[]11 canAutoFreeze: boolean12 drafts: any[]13 parent?: ImmerScope14 patchListener?: PatchListenermap unification WIPMichel Weststratebefore adoption9bcefb6
line 15why, one step back15 immer: ImmerMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 16why, one step back16map unification WIPMichel Weststratebefore adoption9bcefb6
line 17why, one step back17 constructor(parent: ImmerScope | undefined, immer: Immer) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 18–19why, one step back18 this.drafts = []19 this.parent = parentmap unification WIPMichel Weststratebefore adoption9bcefb6
line 20why, one step back20 this.immer = immerMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 21–50why, one step back2122 // Whenever the modified draft contains a draft from another scope, we23 // need to prevent auto-freezing so the unowned draft can be finalized.24 this.canAutoFreeze = true2526 // To avoid prototype lookups:27 this.patches = null as any // TODO:28 }2930 usePatches(patchListener: PatchListener) {31 if (patchListener) {32 this.patches = []33 this.inversePatches = []34 this.patchListener = patchListener35 }36 }3738 revoke() {39 this.leave()40 this.drafts.forEach(revoke)41 // @ts-ignore42 this.drafts = null // TODO: // Make draft-related methods throw.43 }4445 leave() {46 if (this === ImmerScope.current) {47 ImmerScope.current = this.parent48 }49 }50map unification WIPMichel Weststratebefore adoption9bcefb6
lines 51–52why, one step back51 static enter(immer: Immer) {52 const scope = new ImmerScope(ImmerScope.current, immer)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 53–60why, one step back53 ImmerScope.current = scope54 return scope55 }56}5758function revoke(draft) {59 draft[DRAFT_STATE].revoke()60}