@@ -23,7 +23,7 @@ import {
2323 type TerminalRunResult ,
2424 type TerminalTabState ,
2525} from '@sim/terminal-protocol'
26- import { sleep } from '@sim/utils/helpers'
26+ import { interruptibleSleep } from '@sim/utils/helpers'
2727import {
2828 Terminal as HeadlessTerminal ,
2929 type IBuffer ,
@@ -527,7 +527,7 @@ export class TerminalSession {
527527 async pressKeys ( keys : TerminalControlKey [ ] , signal ?: AbortSignal ) : Promise < void > {
528528 for ( let index = 0 ; index < keys . length ; index += 1 ) {
529529 if ( this . disposed || signal ?. aborted ) return
530- if ( index > 0 ) await this . settleBetweenKeystrokes ( )
530+ if ( index > 0 ) await this . settleBetweenKeystrokes ( signal )
531531 if ( signal ?. aborted ) return
532532 this . sendKey ( keys [ index ] )
533533 }
@@ -543,21 +543,24 @@ export class TerminalSession {
543543 const chunks = toInputChunks ( text )
544544 for ( let index = 0 ; index < chunks . length ; index += 1 ) {
545545 if ( this . disposed || signal ?. aborted ) return
546- if ( index > 0 ) await this . settleBetweenKeystrokes ( )
546+ if ( index > 0 ) await this . settleBetweenKeystrokes ( signal )
547547 if ( signal ?. aborted ) return
548548 this . write ( chunks [ index ] )
549549 }
550550 }
551551
552- /** Holds a gap, then lets any resulting redraw finish before the next write. */
553- private async settleBetweenKeystrokes ( ) : Promise < void > {
554- await sleep ( KEYSTROKE_GAP_MS )
552+ /**
553+ * Holds a gap, then lets any resulting redraw finish before the next write.
554+ * Ends early on Stop, so a program that redraws constantly cannot hold it.
555+ */
556+ private async settleBetweenKeystrokes ( signal ?: AbortSignal ) : Promise < void > {
557+ await interruptibleSleep ( KEYSTROKE_GAP_MS , signal )
555558 const deadline = Date . now ( ) + KEYSTROKE_SETTLE_MAX_MS
556- while ( ! this . disposed ) {
559+ while ( ! this . disposed && ! signal ?. aborted ) {
557560 const quietFor = Date . now ( ) - this . lastOutputAt
558561 const remaining = Math . min ( KEYSTROKE_GAP_MS - quietFor , deadline - Date . now ( ) )
559562 if ( remaining <= 0 ) return
560- await sleep ( remaining )
563+ await interruptibleSleep ( remaining , signal )
561564 }
562565 }
563566
0 commit comments