@@ -155,51 +155,37 @@ namespace ts.server {
155
155
156
156
var logger = createLoggerFromEnv ( ) ;
157
157
158
- var messagesToWrite : string [ ] = [ ] ;
159
- function addMessage ( message : string ) {
160
- messagesToWrite . push ( message ) ;
161
- // If the current message list has more than 1 messages, that means
162
- // the current writing is not ended yet, so don't start new writeNext
163
- // as it may interfere with ongoing writing sessions.
164
- if ( messagesToWrite . length === 1 ) {
165
- startWrite ( ) ;
158
+ let pending : string [ ] = [ ] ;
159
+ function queueMessage ( s : string ) {
160
+ pending . push ( s ) ;
161
+ if ( pending . length === 1 ) {
162
+ drain ( ) ;
166
163
}
167
164
}
168
165
169
- function startWrite ( ) {
170
- if ( messagesToWrite . length === 0 ) {
171
- return ;
172
- }
173
-
174
- let messageToWrite = messagesToWrite [ 0 ] ;
175
- let buffer = new Buffer ( messageToWrite , "utf8" ) ;
176
- write ( buffer ) ;
177
- }
178
-
179
- function writeNext ( ) {
180
- if ( messagesToWrite . length > 0 ) {
181
- messagesToWrite = copyListRemovingItem ( messagesToWrite [ 0 ] , messagesToWrite ) ;
182
- }
183
- startWrite ( ) ;
166
+ function drain ( ) {
167
+ Debug . assert ( pending . length > 0 ) ;
168
+ writeBuffer ( new Buffer ( pending [ 0 ] , "utf8" ) , 0 ) ;
184
169
}
185
170
186
- function write ( buffer : any , offset = 0 ) {
187
- let toWrite = buffer . length - offset ;
188
- fs . write ( 1 , buffer , offset , toWrite , /*position*/ undefined , function ( err : any , written : number , buffer : any ) {
189
- offset += written ;
171
+ function writeBuffer ( buffer : Buffer , offset : number ) {
172
+ const toWrite = buffer . length - offset ;
173
+ fs . write ( 1 , buffer , offset , toWrite , undefined , ( err , written , buffer ) => {
190
174
if ( toWrite > written ) {
191
- // there are some content left that still need to be written
192
- write ( buffer , offset ) ;
175
+ writeBuffer ( buffer , offset + written ) ;
193
176
}
194
177
else {
195
- // ready to write the next string
196
- writeNext ( ) ;
178
+ Debug . assert ( pending . length > 0 ) ;
179
+ pending . shift ( ) ;
180
+ if ( pending . length > 0 ) {
181
+ drain ( ) ;
182
+ }
197
183
}
198
- } )
184
+ } ) ;
199
185
}
200
186
201
187
// Override sys.write because fs.writeSync is not reliable on Node 4
202
- ts . sys . write = ( s : string ) => addMessage ( s ) ;
188
+ ts . sys . write = ( s : string ) => queueMessage ( s ) ;
203
189
204
190
var ioSession = new IOSession ( ts . sys , logger ) ;
205
191
process . on ( 'uncaughtException' , function ( err : Error ) {
0 commit comments