@@ -215,11 +215,18 @@ export class ExecuteBash {
215215 const stdoutBuffer : string [ ] = [ ]
216216 const stderrBuffer : string [ ] = [ ]
217217
218+ // Use a closure boolean value firstChunk and a function to get and set its value atomically
219+ let isFirstChunk = true
220+ const getAndSetFirstChunk = ( newValue : boolean ) : boolean => {
221+ const oldValue = isFirstChunk
222+ isFirstChunk = newValue
223+ return oldValue
224+ }
225+
218226 // Use a queue to maintain chronological order of chunks
219227 // This ensures that the output is processed in the exact order it was generated by the child process.
220228 const outputQueue : TimestampedChunk [ ] = [ ]
221229 let processingQueue = false
222- const firstChunk = new AtomicBoolean ( true )
223230
224231 // Process the queue in order
225232 const processQueue = ( ) => {
@@ -249,8 +256,8 @@ export class ExecuteBash {
249256 } ,
250257 collect : false ,
251258 waitForStreams : true ,
252- onStdout : ( chunk : string ) => {
253- const isFirst = firstChunk . getAndSet ( false )
259+ onStdout : async ( chunk : string ) => {
260+ const isFirst = getAndSetFirstChunk ( false )
254261 const timestamp = Date . now ( )
255262 outputQueue . push ( {
256263 timestamp,
@@ -260,8 +267,8 @@ export class ExecuteBash {
260267 } )
261268 processQueue ( )
262269 } ,
263- onStderr : ( chunk : string ) => {
264- const isFirst = firstChunk . getAndSet ( false )
270+ onStderr : async ( chunk : string ) => {
271+ const isFirst = getAndSetFirstChunk ( false )
265272 const timestamp = Date . now ( )
266273 outputQueue . push ( {
267274 timestamp,
@@ -365,17 +372,3 @@ export class ExecuteBash {
365372 updates . end ( )
366373 }
367374}
368-
369- class AtomicBoolean {
370- private value : boolean
371-
372- constructor ( initialValue : boolean ) {
373- this . value = initialValue
374- }
375-
376- public getAndSet ( newValue : boolean ) : boolean {
377- const oldValue = this . value
378- this . value = newValue
379- return oldValue
380- }
381- }
0 commit comments