@@ -3,13 +3,13 @@ import { Disposable } from '@theia/core/shared/vscode-languageserver-protocol';
3
3
import { OutputMessage } from '../../common/protocol' ;
4
4
5
5
export class AutoFlushingBuffer implements Disposable {
6
- private readonly chunks = Chunks . create ( ) ;
6
+ private readonly chunks : Array < [ OutputMessage . Severity , Uint8Array ] > = [ ] ;
7
7
private readonly toDispose ;
8
8
private timer ?: NodeJS . Timeout ;
9
9
private disposed = false ;
10
10
11
11
constructor (
12
- onFlush : ( chunks : Map < OutputMessage . Severity , string | undefined > ) => void ,
12
+ onFlush : ( chunks : Array < [ OutputMessage . Severity , string ] > ) => void ,
13
13
taskTimeout : number = AutoFlushingBuffer . DEFAULT_FLUSH_TIMEOUT_MS
14
14
) {
15
15
const task = ( ) => {
@@ -34,7 +34,9 @@ export class AutoFlushingBuffer implements Disposable {
34
34
chunk : Uint8Array ,
35
35
severity : OutputMessage . Severity = OutputMessage . Severity . Info
36
36
) : void {
37
- this . chunks . get ( severity ) ?. push ( chunk ) ;
37
+ if ( chunk . length ) {
38
+ this . chunks . push ( [ severity , chunk ] ) ;
39
+ }
38
40
}
39
41
40
42
dispose ( ) : void {
@@ -49,32 +51,46 @@ export namespace AutoFlushingBuffer {
49
51
export const DEFAULT_FLUSH_TIMEOUT_MS = 32 ;
50
52
}
51
53
52
- type Chunks = Map < OutputMessage . Severity , Uint8Array [ ] > ;
54
+ type Chunks = Array < [ OutputMessage . Severity , Uint8Array ] > ;
53
55
namespace Chunks {
54
- export function create ( ) : Chunks {
55
- return new Map ( [
56
- [ OutputMessage . Severity . Error , [ ] ] ,
57
- [ OutputMessage . Severity . Warning , [ ] ] ,
58
- [ OutputMessage . Severity . Info , [ ] ] ,
59
- ] ) ;
60
- }
61
56
export function clear ( chunks : Chunks ) : Chunks {
62
- for ( const chunk of chunks . values ( ) ) {
63
- chunk . length = 0 ;
64
- }
57
+ chunks . length = 0 ;
65
58
return chunks ;
66
59
}
67
60
export function isEmpty ( chunks : Chunks ) : boolean {
68
61
return ! [ ...chunks . values ( ) ] . some ( ( chunk ) => Boolean ( chunk . length ) ) ;
69
62
}
70
63
export function toString (
71
64
chunks : Chunks
72
- ) : Map < OutputMessage . Severity , string | undefined > {
73
- return new Map (
74
- Array . from ( chunks . entries ( ) ) . map ( ( [ severity , buffers ] ) => [
75
- severity ,
76
- buffers . length ? Buffer . concat ( buffers ) . toString ( ) : undefined ,
77
- ] )
78
- ) ;
65
+ ) : Array < [ OutputMessage . Severity , string ] > {
66
+ const result : Array < [ OutputMessage . Severity , string ] > = [ ] ;
67
+ let current :
68
+ | { severity : OutputMessage . Severity ; buffers : Uint8Array [ ] }
69
+ | undefined = undefined ;
70
+ const appendToResult = ( ) => {
71
+ if ( current && current . buffers ) {
72
+ result . push ( [
73
+ current . severity ,
74
+ Buffer . concat ( current . buffers ) . toString ( 'utf-8' ) ,
75
+ ] ) ;
76
+ }
77
+ } ;
78
+ for ( const [ severity , buffer ] of chunks ) {
79
+ if ( ! buffer . length ) {
80
+ continue ;
81
+ }
82
+ if ( ! current ) {
83
+ current = { severity, buffers : [ buffer ] } ;
84
+ } else {
85
+ if ( current . severity === severity ) {
86
+ current . buffers . push ( buffer ) ;
87
+ } else {
88
+ appendToResult ( ) ;
89
+ current = { severity, buffers : [ buffer ] } ;
90
+ }
91
+ }
92
+ }
93
+ appendToResult ( ) ;
94
+ return result ;
79
95
}
80
96
}
0 commit comments