Skip to content
Myr

scope.ts

This file

Path

src/scope.ts

67 lines · 1.4 KiB · TypeScript

Last changed by

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

History of this file

Take it

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 1why, one step back
1import {Patch, PatchListener, Drafted, Immer, DRAFT_STATE} from "./internal"

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

line 2why, one step back
2import {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 3–6why, one step back
3 ImmerState,
4 ProxyTypeProxyObject,5 ProxyTypeProxyArray6} from "./types-internal"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 12why, one step back
12

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 13–19why, one step back
13 patches_?: Patch[]
14 inversePatches_?: Patch[]15 canAutoFreeze_: boolean16 drafts_: any[]17 parent_?: ImmerScope18 patchListener_?: PatchListener19 immer_: Immer

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 20why, one step back
20

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 22–24why, one step back
22 this.drafts_ = []
23 this.parent_ = parent24 this.immer_ = immer

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 25–27why, one step back
25
26 // Whenever the modified draft contains a draft from another scope, we27 // need to prevent auto-freezing so the unowned draft can be finalized.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 29–30why, one step back
29 }
30

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 33–35why, one step back
33 this.patches_ = []
34 this.inversePatches_ = []35 this.patchListener_ = patchListener

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 36–38why, one step back
36 }
37 }38

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 39–41why, one step back
39 revoke_() {
40 this.leave_()41 this.drafts_.forEach(revoke)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 44–45why, one step back
44 }
45

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 46–48why, one step back
46 leave_() {
47 if (this === ImmerScope.current_) {48 ImmerScope.current_ = this.parent_

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 49–51why, one step back
49 }
50 }51

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 55–58why, one step back
55 return scope
56 }57}58

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 61why, one step back
61 if (

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 62–63why, one step back
62 state.type_ === ProxyTypeProxyObject ||
63 state.type_ === ProxyTypeProxyArray

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

line 64why, one step back
64 )

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 67why, one step back
67}