@@ -2,44 +2,45 @@ import type { Disposable, Hats, TokenHat } from "@cursorless/common";
2
2
import { ide } from "../singletons/ide.singleton" ;
3
3
import tokenGraphemeSplitter from "../singletons/tokenGraphemeSplitter.singleton" ;
4
4
import { allocateHats } from "../util/allocateHats" ;
5
+ import { Debouncer } from "./Debouncer" ;
5
6
import { IndividualHatMap } from "./IndividualHatMap" ;
6
7
7
8
interface Context {
8
9
getActiveMap ( ) : Promise < IndividualHatMap > ;
9
10
}
10
11
11
12
export class HatAllocator {
12
- private timeoutHandle : NodeJS . Timeout | null = null ;
13
13
private disposables : Disposable [ ] = [ ] ;
14
+ private debouncer = new Debouncer ( ( ) => this . allocateHats ( ) ) ;
14
15
15
16
constructor ( private hats : Hats , private context : Context ) {
16
17
ide ( ) . disposeOnExit ( this ) ;
17
18
18
- this . allocateHatsDebounced = this . allocateHatsDebounced . bind ( this ) ;
19
-
20
19
this . disposables . push (
21
- this . hats . onDidChangeEnabledHatStyles ( this . allocateHatsDebounced ) ,
22
- this . hats . onDidChangeIsEnabled ( this . allocateHatsDebounced ) ,
20
+ this . hats . onDidChangeEnabledHatStyles ( this . debouncer . run ) ,
21
+ this . hats . onDidChangeIsEnabled ( this . debouncer . run ) ,
23
22
24
23
// An event that fires when a text document opens
25
- ide ( ) . onDidOpenTextDocument ( this . allocateHatsDebounced ) ,
24
+ ide ( ) . onDidOpenTextDocument ( this . debouncer . run ) ,
26
25
// An event that fires when a text document closes
27
- ide ( ) . onDidCloseTextDocument ( this . allocateHatsDebounced ) ,
26
+ ide ( ) . onDidCloseTextDocument ( this . debouncer . run ) ,
28
27
// An Event which fires when the active editor has changed. Note that the event also fires when the active editor changes to undefined.
29
- ide ( ) . onDidChangeActiveTextEditor ( this . allocateHatsDebounced ) ,
28
+ ide ( ) . onDidChangeActiveTextEditor ( this . debouncer . run ) ,
30
29
// An Event which fires when the array of visible editors has changed.
31
- ide ( ) . onDidChangeVisibleTextEditors ( this . allocateHatsDebounced ) ,
30
+ ide ( ) . onDidChangeVisibleTextEditors ( this . debouncer . run ) ,
32
31
// An event that is emitted when a text document is changed. This usually happens when the contents changes but also when other things like the dirty-state changes.
33
- ide ( ) . onDidChangeTextDocument ( this . allocateHatsDebounced ) ,
32
+ ide ( ) . onDidChangeTextDocument ( this . debouncer . run ) ,
34
33
// An Event which fires when the selection in an editor has changed.
35
- ide ( ) . onDidChangeTextEditorSelection ( this . allocateHatsDebounced ) ,
34
+ ide ( ) . onDidChangeTextEditorSelection ( this . debouncer . run ) ,
36
35
// An Event which fires when the visible ranges of an editor has changed.
37
- ide ( ) . onDidChangeTextEditorVisibleRanges ( this . allocateHatsDebounced ) ,
36
+ ide ( ) . onDidChangeTextEditorVisibleRanges ( this . debouncer . run ) ,
38
37
// Re-draw hats on grapheme splitting algorithm change in case they
39
38
// changed their token hat splitting setting.
40
39
tokenGraphemeSplitter ( ) . registerAlgorithmChangeListener (
41
- this . allocateHatsDebounced ,
40
+ this . debouncer . run ,
42
41
) ,
42
+
43
+ this . debouncer ,
43
44
) ;
44
45
}
45
46
@@ -75,26 +76,7 @@ export class HatAllocator {
75
76
) ;
76
77
}
77
78
78
- allocateHatsDebounced ( ) {
79
- if ( this . timeoutHandle != null ) {
80
- clearTimeout ( this . timeoutHandle ) ;
81
- }
82
-
83
- const decorationDebounceDelayMs = ide ( ) . configuration . getOwnConfiguration (
84
- "decorationDebounceDelayMs" ,
85
- ) ;
86
-
87
- this . timeoutHandle = setTimeout ( ( ) => {
88
- this . allocateHats ( ) ;
89
- this . timeoutHandle = null ;
90
- } , decorationDebounceDelayMs ) ;
91
- }
92
-
93
79
dispose ( ) {
94
80
this . disposables . forEach ( ( { dispose } ) => dispose ( ) ) ;
95
-
96
- if ( this . timeoutHandle != null ) {
97
- clearTimeout ( this . timeoutHandle ) ;
98
- }
99
81
}
100
82
}
0 commit comments