Skip to content

Commit

Permalink
write callback and promise
Browse files Browse the repository at this point in the history
  • Loading branch information
jerch committed Jul 11, 2019
1 parent 8000e33 commit 8cb0104
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp

// user input states
public writeBuffer: string[];
public writeBufferCallback: (() => void)[] = [];
public writeBufferUtf8: Uint8Array[];
private _writeInProgress: boolean;
/**
Expand Down Expand Up @@ -1275,7 +1276,7 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
* Writes text to the terminal.
* @param data The text to write to the terminal.
*/
public write(data: string): void {
public write(data: string, cb?: () => void): void {
// Ensure the terminal isn't disposed
if (this._isDisposed) {
return;
Expand All @@ -1296,6 +1297,7 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
this._writeBuffersPendingSize += data.length;

this.writeBuffer.push(data);
this.writeBufferCallback.push(cb);

if (!this._writeInProgress && this.writeBuffer.length > 0) {
// Kick off a write which will write all data in sequence recursively
Expand All @@ -1307,6 +1309,12 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
}
}

private _stringExecutor = (data: string) => (resolve: () => void) => this.write(data, resolve);

public writePromise(data: string): Promise<void> {
return new Promise(this._stringExecutor(data));
}

protected _innerWrite(bufferOffset: number = 0): void {
// Ensure the terminal isn't disposed
if (this._isDisposed) {
Expand All @@ -1316,13 +1324,17 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
const startTime = Date.now();
while (this.writeBuffer.length > bufferOffset) {
const data = this.writeBuffer[bufferOffset];
const cb = this.writeBufferCallback[bufferOffset];
bufferOffset++;

this._refreshStart = this.buffer.y;
this._refreshEnd = this.buffer.y;

this._inputHandler.parse(data);
this._writeBuffersPendingSize -= data.length;
if (cb) {
cb();
}

this.updateRange(this.buffer.y);
this.refresh(this._refreshStart, this._refreshEnd);
Expand All @@ -1336,12 +1348,14 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
// trim already processed chunks if we are above threshold
if (bufferOffset > WRITE_BUFFER_LENGTH_THRESHOLD) {
this.writeBuffer = this.writeBuffer.slice(bufferOffset);
this.writeBufferCallback = this.writeBufferCallback.slice(bufferOffset);
bufferOffset = 0;
}
setTimeout(() => this._innerWrite(bufferOffset), 0);
} else {
this._writeInProgress = false;
this.writeBuffer = [];
this.writeBufferCallback = [];
}
}

Expand Down

0 comments on commit 8cb0104

Please sign in to comment.