Skip to content
Myr

scope.ts

This file

Path

src/scope.ts

70 lines · 1.4 KiB · TypeScript

Last changed by

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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"

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 9why, one step back
9import {ImmerState} from "./types-internal"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 10–11why, one step back
10
11/** Each scope represents a `produce` call. */

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 12why, one step back
12// TODO: non-class?

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 13why, one step back
13export class ImmerScope {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 14why, one step back
14 static current_?: ImmerScope

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 15why, one step back
15

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 16–22why, one step back
16 patches_?: Patch[]
17 inversePatches_?: Patch[]18 canAutoFreeze_: boolean19 drafts_: any[]20 parent_?: ImmerScope21 patchListener_?: PatchListener22 immer_: Immer

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 23why, one step back
23

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 25–27why, one step back
25 this.drafts_ = []
26 this.parent_ = parent27 this.immer_ = immer

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 28–30why, one step back
28
29 // Whenever the modified draft contains a draft from another scope, we30 // need to prevent auto-freezing so the unowned draft can be finalized.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 31why, one step back
31 this.canAutoFreeze_ = true

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 32–33why, one step back
32 }
33

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 34why, one step back
34 usePatches_(patchListener?: PatchListener) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 35why, one step back
35 if (patchListener) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 36–38why, one step back
36 this.patches_ = []
37 this.inversePatches_ = []38 this.patchListener_ = patchListener

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 39–41why, one step back
39 }
40 }41

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 42–44why, one step back
42 revoke_() {
43 this.leave_()44 this.drafts_.forEach(revoke)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 45why, one step back
45 // @ts-ignore

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 46why, one step back
46 this.drafts_ = null

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 47–48why, one step back
47 }
48

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 49–51why, one step back
49 leave_() {
50 if (this === ImmerScope.current_) {51 ImmerScope.current_ = this.parent_

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 52–54why, one step back
52 }
53 }54

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 55–57why, one step back
55 static enter_(immer: Immer) {
56 const scope = new ImmerScope(ImmerScope.current_, immer)57 ImmerScope.current_ = scope

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 58–61why, one step back
58 return scope
59 }60}61

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

line 62why, one step back
62function revoke(draft: Drafted) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 63why, one step back
63 const state: ImmerState = draft[DRAFT_STATE]

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

line 64why, one step back
64 if (

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 65–66why, one step back
65 state.type_ === ProxyType.ProxyObject ||
66 state.type_ === ProxyType.ProxyArray

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

line 67why, one step back
67 )

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 68–69why, one step back
68 state.revoke_()
69 else state.revoked_ = true

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 70why, one step back
70}