1+ import { DisposableCollection } from '@theia/core' ;
12import { Disposable } from '@theia/core/shared/vscode-languageserver-protocol' ;
23import { OutputMessage } from '../../common/protocol' ;
34
4- const DEFAULT_FLUS_TIMEOUT_MS = 32 ;
5-
6- export class SimpleBuffer implements Disposable {
5+ export class AutoFlushingBuffer implements Disposable {
76 private readonly chunks = Chunks . create ( ) ;
8- private readonly flush : ( ) => void ;
9- private flushInterval ?: NodeJS . Timeout ;
7+ private readonly toDispose ;
8+ private timer ?: NodeJS . Timeout ;
9+ private disposed = false ;
1010
1111 constructor (
1212 onFlush : ( chunks : Map < OutputMessage . Severity , string | undefined > ) => void ,
13- flushTimeout : number = DEFAULT_FLUS_TIMEOUT_MS
13+ taskTimeout : number = AutoFlushingBuffer . DEFAULT_FLUSH_TIMEOUT_MS
1414 ) {
15- this . flush = ( ) => {
15+ const task = ( ) => {
1616 if ( ! Chunks . isEmpty ( this . chunks ) ) {
1717 const chunks = Chunks . toString ( this . chunks ) ;
18- this . clearChunks ( ) ;
18+ Chunks . clear ( this . chunks ) ;
1919 onFlush ( chunks ) ;
2020 }
21+ if ( ! this . disposed ) {
22+ this . timer = setTimeout ( task , taskTimeout ) ;
23+ }
2124 } ;
22- this . flushInterval = setInterval ( this . flush , flushTimeout ) ;
25+ this . timer = setTimeout ( task , taskTimeout ) ;
26+ this . toDispose = new DisposableCollection (
27+ Disposable . create ( ( ) => ( this . disposed = true ) ) ,
28+ Disposable . create ( ( ) => clearTimeout ( this . timer ) ) ,
29+ Disposable . create ( ( ) => task ( ) )
30+ ) ;
2331 }
2432
2533 addChunk (
@@ -29,17 +37,17 @@ export class SimpleBuffer implements Disposable {
2937 this . chunks . get ( severity ) ?. push ( chunk ) ;
3038 }
3139
32- private clearChunks ( ) : void {
33- Chunks . clear ( this . chunks ) ;
34- }
35-
3640 dispose ( ) : void {
37- this . flush ( ) ;
38- clearInterval ( this . flushInterval ) ;
39- this . clearChunks ( ) ;
40- this . flushInterval = undefined ;
41+ this . toDispose . dispose ( ) ;
4142 }
4243}
44+ export namespace AutoFlushingBuffer {
45+ /**
46+ * _"chunking and sending every 16ms (60hz) is the best for small amount of data
47+ * To be able to crunch more data without the cpu going to high, I opted for a 30fps refresh rate, hence the 32msec"_
48+ */
49+ export const DEFAULT_FLUSH_TIMEOUT_MS = 32 ;
50+ }
4351
4452type Chunks = Map < OutputMessage . Severity , Uint8Array [ ] > ;
4553namespace Chunks {
0 commit comments