Skip to content
Myr

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 file

Take it

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 1–8why, one step back
1import {
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 back
9
10/** 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?: PatchListener

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 20why, one step back
20 immer: Immer

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 21why, one step back
21

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 22why, one step back
22 constructor(parent: ImmerScope | undefined, immer: Immer) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 23–24why, one step back
23 this.drafts = []
24 this.parent = parent

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 25why, one step back
25 this.immer = immer

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 26–31why, one step back
26
27 // 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 }31

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 32why, one step back
32 usePatches(patchListener?: PatchListener) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 33–43why, one step back
33 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-ignore

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 44why, one step back
44 this.drafts = null

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 45–52why, one step back
45 }
4647 leave() {48 if (this === ImmerScope.current) {49 ImmerScope.current = this.parent50 }51 }52

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 53–54why, one step back
53 static enter(immer: Immer) {
54 const scope = new ImmerScope(ImmerScope.current, immer)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 55–59why, one step back
55 ImmerScope.current = scope
56 return scope57 }58}59

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 60–67why, one step back
60function 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 = true

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 68why, one step back
68}