scope.ts
This file
Path
src/scope.ts
68 lines · 1.3 KiB · TypeScript
Last changed by
fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
History of this fileTake it
fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 1–8why, one step back1import {2 Patch,3 PatchListener,4 Drafted,5 ProxyType,6 Immer,7 DRAFT_STATE8} from "./internal"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 9–19why, one step back910/** Each scope represents a `produce` call. */11export class ImmerScope {12 static current?: ImmerScope1314 patches?: Patch[]15 inversePatches?: Patch[]16 canAutoFreeze: boolean17 drafts: any[]18 parent?: ImmerScope19 patchListener?: PatchListenerfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 20why, one step back20 immer: Immerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 21why, one step back21feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 22why, one step back22 constructor(parent: ImmerScope | undefined, immer: Immer) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 23–24why, one step back23 this.drafts = []24 this.parent = parentfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 25why, one step back25 this.immer = immerfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 26–31why, one step back2627 // Whenever the modified draft contains a draft from another scope, we28 // need to prevent auto-freezing so the unowned draft can be finalized.29 this.canAutoFreeze = true30 }31feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 32why, one step back32 usePatches(patchListener?: PatchListener) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 33–43why, one step back33 if (patchListener) {34 this.patches = []35 this.inversePatches = []36 this.patchListener = patchListener37 }38 }3940 revoke() {41 this.leave()42 this.drafts.forEach(revoke)43 // @ts-ignorefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 44why, one step back44 this.drafts = nullfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 45–52why, one step back45 }4647 leave() {48 if (this === ImmerScope.current) {49 ImmerScope.current = this.parent50 }51 }52feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 53–54why, one step back53 static enter(immer: Immer) {54 const scope = new ImmerScope(ImmerScope.current, immer)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 55–59why, one step back55 ImmerScope.current = scope56 return scope57 }58}59feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 60–67why, one step back60function revoke(draft: Drafted) {61 const state = draft[DRAFT_STATE]62 if (63 state.type === ProxyType.ProxyObject ||64 state.type === ProxyType.ProxyArray65 )66 state.revoke()67 else state.revoked = truefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 68why, one step back68}