Use microtask to flush the write buffer after input

Fixes #4158
This commit is contained in:
Daniel Imms
2022-09-28 09:06:07 -07:00
parent 51c580e7a4
commit 44388e5e6c
2 changed files with 15 additions and 1 deletions
+1
View File
@@ -132,6 +132,7 @@ export abstract class CoreTerminal extends Disposable implements ICoreTerminal {
this.register(forwardEvent(this._bufferService.onResize, this._onResize));
this.register(forwardEvent(this.coreService.onData, this._onData));
this.register(forwardEvent(this.coreService.onBinary, this._onBinary));
this.register(this.coreService.onUserInput(() => this._writeBuffer.handleUserInput()));
this.register(this.optionsService.onOptionChange(key => this._updateOptions(key)));
this.register(this._bufferService.onScroll(event => {
this._onScroll.fire({ position: this._bufferService.buffer.ydisp, source: ScrollSource.TERMINAL });
+14 -1
View File
@@ -40,11 +40,16 @@ export class WriteBuffer {
private _bufferOffset = 0;
private _isSyncWriting = false;
private _syncCalls = 0;
private _didUserInput = false;
public get onWriteParsed(): IEvent<void> { return this._onWriteParsed.event; }
private _onWriteParsed = new EventEmitter<void>();
constructor(private _action: (data: string | Uint8Array, promiseResult?: boolean) => void | Promise<boolean>) { }
public handleUserInput(): void {
this._didUserInput = true;
}
/**
* @deprecated Unreliable, to be removed soon.
*/
@@ -99,7 +104,15 @@ export class WriteBuffer {
// schedule chunk processing for next event loop run
if (!this._writeBuffer.length) {
this._bufferOffset = 0;
queueMicrotask(() => this._innerWrite());
// If this is the first write call after the user has done some input,
// parse it immediately in an upcoming microtask to minimize reduce input,
// otherwise schedule for the next event
if (this._didUserInput) {
this._didUserInput = false;
queueMicrotask(() => this._innerWrite());
} else {
setTimeout(() => this._innerWrite());
}
}
this._pendingData += data.length;