@@ -3,13 +3,13 @@ import { Disposable } from '@theia/core/shared/vscode-languageserver-protocol';
33import { OutputMessage } from '../../common/protocol' ;
44
55export class AutoFlushingBuffer implements Disposable {
6- private readonly chunks = Chunks . create ( ) ;
6+ private readonly chunks : Array < [ OutputMessage . Severity , Uint8Array ] > = [ ] ;
77 private readonly toDispose ;
88 private timer ?: NodeJS . Timeout ;
99 private disposed = false ;
1010
1111 constructor (
12- onFlush : ( chunks : Map < OutputMessage . Severity , string | undefined > ) => void ,
12+ onFlush : ( chunks : Array < [ OutputMessage . Severity , string ] > ) => void ,
1313 taskTimeout : number = AutoFlushingBuffer . DEFAULT_FLUSH_TIMEOUT_MS
1414 ) {
1515 const task = ( ) => {
@@ -34,7 +34,9 @@ export class AutoFlushingBuffer implements Disposable {
3434 chunk : Uint8Array ,
3535 severity : OutputMessage . Severity = OutputMessage . Severity . Info
3636 ) : void {
37- this . chunks . get ( severity ) ?. push ( chunk ) ;
37+ if ( chunk . length ) {
38+ this . chunks . push ( [ severity , chunk ] ) ;
39+ }
3840 }
3941
4042 dispose ( ) : void {
@@ -49,32 +51,46 @@ export namespace AutoFlushingBuffer {
4951 export const DEFAULT_FLUSH_TIMEOUT_MS = 32 ;
5052}
5153
52- type Chunks = Map < OutputMessage . Severity , Uint8Array [ ] > ;
54+ type Chunks = Array < [ OutputMessage . Severity , Uint8Array ] > ;
5355namespace Chunks {
54- export function create ( ) : Chunks {
55- return new Map ( [
56- [ OutputMessage . Severity . Error , [ ] ] ,
57- [ OutputMessage . Severity . Warning , [ ] ] ,
58- [ OutputMessage . Severity . Info , [ ] ] ,
59- ] ) ;
60- }
6156 export function clear ( chunks : Chunks ) : Chunks {
62- for ( const chunk of chunks . values ( ) ) {
63- chunk . length = 0 ;
64- }
57+ chunks . length = 0 ;
6558 return chunks ;
6659 }
6760 export function isEmpty ( chunks : Chunks ) : boolean {
6861 return ! [ ...chunks . values ( ) ] . some ( ( chunk ) => Boolean ( chunk . length ) ) ;
6962 }
7063 export function toString (
7164 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 ;
7995 }
8096}
0 commit comments