flush on clear output buffer (#1074)

This commit is contained in:
David Simpson 2022-06-20 09:32:10 +02:00 committed by GitHub
parent 94ceefd960
commit a715da3d18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,15 +3,20 @@ export class SimpleBuffer {
private flushInterval?: NodeJS.Timeout;
private flush: () => void;
constructor(onFlush: (chunk: string) => void, flushTimeout: number) {
this.flushInterval = setInterval(() => {
const flush = () => {
if (this.chunks.length > 0) {
const chunkString = Buffer.concat(this.chunks).toString();
this.clearChunks();
onFlush(chunkString);
}
}, flushTimeout);
};
this.flush = flush;
this.flushInterval = setInterval(flush, flushTimeout);
}
public addChunk(chunk: Uint8Array): void {
@ -23,6 +28,7 @@ export class SimpleBuffer {
}
public clearFlushInterval(): void {
this.flush();
this.clearChunks();
clearInterval(this.flushInterval);