@@ -2,44 +2,45 @@ import type { Disposable, Hats, TokenHat } from "@cursorless/common";
22import { ide } from "../singletons/ide.singleton" ;
33import tokenGraphemeSplitter from "../singletons/tokenGraphemeSplitter.singleton" ;
44import { allocateHats } from "../util/allocateHats" ;
5+ import { Debouncer } from "./Debouncer" ;
56import { IndividualHatMap } from "./IndividualHatMap" ;
67
78interface Context {
89 getActiveMap ( ) : Promise < IndividualHatMap > ;
910}
1011
1112export class HatAllocator {
12- private timeoutHandle : NodeJS . Timeout | null = null ;
1313 private disposables : Disposable [ ] = [ ] ;
14+ private debouncer = new Debouncer ( ( ) => this . allocateHats ( ) ) ;
1415
1516 constructor ( private hats : Hats , private context : Context ) {
1617 ide ( ) . disposeOnExit ( this ) ;
1718
18- this . allocateHatsDebounced = this . allocateHatsDebounced . bind ( this ) ;
19-
2019 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 ) ,
2322
2423 // An event that fires when a text document opens
25- ide ( ) . onDidOpenTextDocument ( this . allocateHatsDebounced ) ,
24+ ide ( ) . onDidOpenTextDocument ( this . debouncer . run ) ,
2625 // An event that fires when a text document closes
27- ide ( ) . onDidCloseTextDocument ( this . allocateHatsDebounced ) ,
26+ ide ( ) . onDidCloseTextDocument ( this . debouncer . run ) ,
2827 // 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 ) ,
3029 // An Event which fires when the array of visible editors has changed.
31- ide ( ) . onDidChangeVisibleTextEditors ( this . allocateHatsDebounced ) ,
30+ ide ( ) . onDidChangeVisibleTextEditors ( this . debouncer . run ) ,
3231 // 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 ) ,
3433 // An Event which fires when the selection in an editor has changed.
35- ide ( ) . onDidChangeTextEditorSelection ( this . allocateHatsDebounced ) ,
34+ ide ( ) . onDidChangeTextEditorSelection ( this . debouncer . run ) ,
3635 // An Event which fires when the visible ranges of an editor has changed.
37- ide ( ) . onDidChangeTextEditorVisibleRanges ( this . allocateHatsDebounced ) ,
36+ ide ( ) . onDidChangeTextEditorVisibleRanges ( this . debouncer . run ) ,
3837 // Re-draw hats on grapheme splitting algorithm change in case they
3938 // changed their token hat splitting setting.
4039 tokenGraphemeSplitter ( ) . registerAlgorithmChangeListener (
41- this . allocateHatsDebounced ,
40+ this . debouncer . run ,
4241 ) ,
42+
43+ this . debouncer ,
4344 ) ;
4445 }
4546
@@ -75,26 +76,7 @@ export class HatAllocator {
7576 ) ;
7677 }
7778
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-
9379 dispose ( ) {
9480 this . disposables . forEach ( ( { dispose } ) => dispose ( ) ) ;
95-
96- if ( this . timeoutHandle != null ) {
97- clearTimeout ( this . timeoutHandle ) ;
98- }
9981 }
10082}
0 commit comments