scope.ts
This file
Path
src/scope.ts
63 lines · 1.4 KiB · TypeScript
Last changed by
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
History of this fileTake it
feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 1why, one step back1import {DRAFT_STATE} from "./common"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 2–3why, one step back2import {Patch, PatchListener, Drafted, ProxyType} from "./types"3import {Immer} from "./immer"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
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?: PatchListenerfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 15why, one step back15 immer: Immerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 16why, one step back16feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 17why, one step back17 constructor(parent: ImmerScope | undefined, immer: Immer) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 18–19why, one step back18 this.drafts = []19 this.parent = parentfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 20why, one step back20 this.immer = immerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 21–26why, 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 }26feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 27why, one step back27 usePatches(patchListener?: PatchListener) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 28–38why, one step back28 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-ignorefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 39why, one step back39 this.drafts = nullfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 40–47why, one step back40 }4142 leave() {43 if (this === ImmerScope.current) {44 ImmerScope.current = this.parent45 }46 }47feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 48–49why, one step back48 static enter(immer: Immer) {49 const scope = new ImmerScope(ImmerScope.current, immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 50–54why, one step back50 ImmerScope.current = scope51 return scope52 }53}54feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 55–62why, one step back55function revoke(draft: Drafted) {56 const state = draft[DRAFT_STATE]57 if (58 state.type === ProxyType.ProxyObject ||59 state.type === ProxyType.ProxyArray60 )61 state.revoke()62 else state.revoked = truefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 63why, one step back63}