1
+ import { DisposableCollection } from '@theia/core' ;
1
2
import { Disposable } from '@theia/core/shared/vscode-languageserver-protocol' ;
2
3
import { OutputMessage } from '../../common/protocol' ;
3
4
4
- const DEFAULT_FLUS_TIMEOUT_MS = 32 ;
5
-
6
- export class SimpleBuffer implements Disposable {
5
+ export class AutoFlushingBuffer implements Disposable {
7
6
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 ;
10
10
11
11
constructor (
12
12
onFlush : ( chunks : Map < OutputMessage . Severity , string | undefined > ) => void ,
13
- flushTimeout : number = DEFAULT_FLUS_TIMEOUT_MS
13
+ taskTimeout : number = AutoFlushingBuffer . DEFAULT_FLUSH_TIMEOUT_MS
14
14
) {
15
- this . flush = ( ) => {
15
+ const task = ( ) => {
16
16
if ( ! Chunks . isEmpty ( this . chunks ) ) {
17
17
const chunks = Chunks . toString ( this . chunks ) ;
18
- this . clearChunks ( ) ;
18
+ Chunks . clear ( this . chunks ) ;
19
19
onFlush ( chunks ) ;
20
20
}
21
+ if ( ! this . disposed ) {
22
+ this . timer = setTimeout ( task , taskTimeout ) ;
23
+ }
21
24
} ;
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
+ ) ;
23
31
}
24
32
25
33
addChunk (
@@ -29,17 +37,17 @@ export class SimpleBuffer implements Disposable {
29
37
this . chunks . get ( severity ) ?. push ( chunk ) ;
30
38
}
31
39
32
- private clearChunks ( ) : void {
33
- Chunks . clear ( this . chunks ) ;
34
- }
35
-
36
40
dispose ( ) : void {
37
- this . flush ( ) ;
38
- clearInterval ( this . flushInterval ) ;
39
- this . clearChunks ( ) ;
40
- this . flushInterval = undefined ;
41
+ this . toDispose . dispose ( ) ;
41
42
}
42
43
}
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
+ }
43
51
44
52
type Chunks = Map < OutputMessage . Severity , Uint8Array [ ] > ;
45
53
namespace Chunks {
0 commit comments