Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit f37774f

Browse files
added timing to reduce latency at low frequencies
1 parent 1bb1b9d commit f37774f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/serialmonitor/outputBuffer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@
22
// Licensed under the MIT license.
33

44
import * as vscode from "vscode";
5+
import { performance } from "perf_hooks";
56

67
export class BufferedOutputChannel implements vscode.Disposable {
78
private TIMING_BUFFER: number = 300;
89

910
private _buffer: string[];
1011
private timer: NodeJS.Timer;
12+
private _lastFlushTime: number;
1113

1214
public constructor(private outputCallback: (value: string) => void) {
1315
this._buffer = [];
14-
this.timer = setInterval(() => this.flush(), this.TIMING_BUFFER);
16+
this.timer = setInterval(() => this.tryFlush(), this.TIMING_BUFFER);
17+
this._lastFlushTime = Number.NEGATIVE_INFINITY;
1518
}
1619

1720
private add(value: string) {
1821
this._buffer.push(value);
22+
this.tryFlush();
1923
}
2024

21-
private flush() {
22-
if (this._buffer && this._buffer.length > 0) {
25+
private tryFlush() {
26+
const currentTime = performance.now();
27+
if (this._buffer && this._buffer.length > 0 && currentTime-this._lastFlushTime > this.TIMING_BUFFER) {
2328
this.outputCallback(this._buffer.join(''));
29+
this._lastFlushTime = currentTime;
2430
this._buffer = [];
2531
}
2632
}

0 commit comments

Comments
 (0)