@@ -65,7 +65,7 @@ export class PtySession {
6565 this . transcript += data ;
6666 const { replyBytes } = this . terminal . feed ( data ) ;
6767 if ( replyBytes && replyBytes . length > 0 ) {
68- this . pty . write ( Buffer . from ( replyBytes ) . toString ( "utf8" ) ) ;
68+ this . writeBytes ( replyBytes ) ;
6969 }
7070 this . noteUpdate ( ) ;
7171 } ) ;
@@ -94,8 +94,7 @@ export class PtySession {
9494
9595 pressKey ( key : SpecialKey ) : void {
9696 this . ensureOpen ( ) ;
97- const bytes = this . terminal . encodeSpecialKey ( key ) ;
98- this . pty . write ( Buffer . from ( bytes ) . toString ( "latin1" ) ) ;
97+ this . writeBytes ( this . terminal . encodeSpecialKey ( key ) ) ;
9998 }
10099
101100 resize ( cols : number , rows : number ) : void {
@@ -151,10 +150,7 @@ export class PtySession {
151150 if ( this . exitedAt ) {
152151 throw this . buildWaitError ( description , snapshot , new Error ( "process exited before condition was satisfied" ) ) ;
153152 }
154- if ( this . revision !== seen ) {
155- continue ;
156- }
157- await waitForUpdate ( this . events , controller . signal ) ;
153+ await waitForUpdate ( this . events , controller . signal , undefined , ( ) => this . revision !== seen ) ;
158154 }
159155 } catch ( error ) {
160156 if ( controller . signal . aborted ) {
@@ -189,10 +185,7 @@ export class PtySession {
189185 if ( this . exitedAt ) {
190186 return snapshot ;
191187 }
192- if ( this . revision !== seen ) {
193- continue ;
194- }
195- await waitForUpdate ( this . events , controller . signal , stableForMs ) ;
188+ await waitForUpdate ( this . events , controller . signal , stableForMs , ( ) => this . revision !== seen ) ;
196189 }
197190 } catch ( error ) {
198191 if ( controller . signal . aborted ) {
@@ -213,13 +206,7 @@ export class PtySession {
213206 try {
214207 while ( ! this . exitedAt ) {
215208 const seen = this . revision ;
216- if ( this . exitedAt ) {
217- break ;
218- }
219- if ( this . revision !== seen ) {
220- continue ;
221- }
222- await waitForUpdate ( this . events , controller . signal ) ;
209+ await waitForUpdate ( this . events , controller . signal , undefined , ( ) => this . revision !== seen || Boolean ( this . exitedAt ) ) ;
223210 }
224211 return this . status ( ) ;
225212 } catch ( error ) {
@@ -284,6 +271,13 @@ export class PtySession {
284271 this . events . emit ( "update" ) ;
285272 }
286273
274+ private writeBytes ( bytes : Uint8Array ) : void {
275+ if ( bytes . length === 0 ) {
276+ return ;
277+ }
278+ this . pty . write ( Buffer . from ( bytes ) . toString ( "utf8" ) ) ;
279+ }
280+
287281 private ensureOpen ( ) : void {
288282 if ( this . closed ) {
289283 throw new Error ( "session already closed" ) ;
@@ -356,7 +350,12 @@ function createWaitController(options?: WaitOptions): { signal: AbortSignal; cle
356350 } ;
357351}
358352
359- async function waitForUpdate ( events : EventEmitter , signal : AbortSignal , timeoutMs ?: number ) : Promise < void > {
353+ async function waitForUpdate (
354+ events : EventEmitter ,
355+ signal : AbortSignal ,
356+ timeoutMs ?: number ,
357+ alreadyChanged ?: ( ) => boolean ,
358+ ) : Promise < void > {
360359 if ( signal . aborted ) {
361360 throw abortReason ( signal ) ;
362361 }
@@ -381,6 +380,11 @@ async function waitForUpdate(events: EventEmitter, signal: AbortSignal, timeoutM
381380
382381 events . once ( "update" , onUpdate ) ;
383382 signal . addEventListener ( "abort" , onAbort , { once : true } ) ;
383+ if ( alreadyChanged ?.( ) ) {
384+ cleanup ( ) ;
385+ resolve ( ) ;
386+ return ;
387+ }
384388 if ( timeoutMs !== undefined ) {
385389 timer = setTimeout ( ( ) => {
386390 cleanup ( ) ;
0 commit comments