scope.ts
This file
Path
src/scope.ts
58 lines · 1.3 KiB · TypeScript
Last changed by
Processed bunch of todosMichel Weststratebefore adoption00425e1
History of this fileTake it
Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 1why, one step back1import {DRAFT_STATE} from "./common"TS type improvementsMichel Weststratebefore adoptiondd1808e
line 2why, one step back2import {Patch, PatchListener, Drafted} from "./types"Removed unused stuffMichel Weststratebefore adoption74c0536
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–38why, 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 = true25 }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-ignoreProcessed bunch of todosMichel Weststratebefore adoption00425e1
line 39why, one step back39 this.drafts = nullMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 40–47why, one step back40 }4142 leave() {43 if (this === ImmerScope.current) {44 ImmerScope.current = this.parent45 }46 }47map unification WIPMichel Weststratebefore adoption9bcefb6
lines 48–49why, one step back48 static enter(immer: Immer) {49 const scope = new ImmerScope(ImmerScope.current, immer)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 50–54why, one step back50 ImmerScope.current = scope51 return scope52 }53}54TS type improvementsMichel Weststratebefore adoptiondd1808e
line 55why, one step back55function revoke(draft: Drafted) {Processed bunch of todosMichel Weststratebefore adoption00425e1
line 56why, one step back56 // TODO: switch per type and remove from data structuresMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 57–58why, one step back57 draft[DRAFT_STATE].revoke()58}