Skip to content
Myr

scope.ts

This file

Path

src/scope.ts

68 lines · 1.5 KiB · TypeScript

Last changed by

Optimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8

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

Optimization: stop recursing if there are no drafts leftMichel Weststratebefore adoptionebda6f8

line 20why, one step back
20 unfinalizedDrafts_ = 0

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) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 26–28why, 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.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

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

line 62why, one step back
62 if (

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

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

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

line 65why, one step back
65 )

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 68why, one step back
68}