Skip to content

Commit 0b0958c

Browse files
change output buffer to setTimeout instead of setInterval (arduino#1123)
* change output buffer to setTimeout * remove unnec. code * dispose buffer on end, not 'finally' * revert core-service changes * refactor, disposable pattern * newline
1 parent 06acd7f commit 0b0958c

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

arduino-ide-extension/src/node/core-service-impl.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { ArduinoCoreServiceClient } from './cli-protocol/cc/arduino/cli/commands
2828
import { Port as GrpcPort } from './cli-protocol/cc/arduino/cli/commands/v1/port_pb';
2929
import { ApplicationError, Disposable, nls } from '@theia/core';
3030
import { MonitorManager } from './monitor-manager';
31-
import { SimpleBuffer } from './utils/simple-buffer';
31+
import { AutoFlushingBuffer } from './utils/buffers';
3232
import { tryParseError } from './cli-error-parser';
3333
import { Instance } from './cli-protocol/cc/arduino/cli/commands/v1/common_pb';
3434
import { firstToUpperCase, notEmpty } from '../common/utils';
@@ -290,7 +290,7 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
290290
onData: (response: R) => void;
291291
} {
292292
const stderr: Buffer[] = [];
293-
const buffer = new SimpleBuffer((chunks) => {
293+
const buffer = new AutoFlushingBuffer((chunks) => {
294294
Array.from(chunks.entries()).forEach(([severity, chunk]) => {
295295
if (chunk) {
296296
this.sendResponse(chunk, severity);

arduino-ide-extension/src/node/utils/simple-buffer.ts renamed to arduino-ide-extension/src/node/utils/buffers.ts

+25-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1+
import { DisposableCollection } from '@theia/core';
12
import { Disposable } from '@theia/core/shared/vscode-languageserver-protocol';
23
import { OutputMessage } from '../../common/protocol';
34

4-
const DEFAULT_FLUS_TIMEOUT_MS = 32;
5-
6-
export class SimpleBuffer implements Disposable {
5+
export class AutoFlushingBuffer implements Disposable {
76
private readonly chunks = Chunks.create();
8-
private readonly flush: () => void;
9-
private flushInterval?: NodeJS.Timeout;
7+
private readonly toDispose;
8+
private timer?: NodeJS.Timeout;
9+
private disposed = false;
1010

1111
constructor(
1212
onFlush: (chunks: Map<OutputMessage.Severity, string | undefined>) => void,
13-
flushTimeout: number = DEFAULT_FLUS_TIMEOUT_MS
13+
taskTimeout: number = AutoFlushingBuffer.DEFAULT_FLUSH_TIMEOUT_MS
1414
) {
15-
this.flush = () => {
15+
const task = () => {
1616
if (!Chunks.isEmpty(this.chunks)) {
1717
const chunks = Chunks.toString(this.chunks);
18-
this.clearChunks();
18+
Chunks.clear(this.chunks);
1919
onFlush(chunks);
2020
}
21+
if (!this.disposed) {
22+
this.timer = setTimeout(task, taskTimeout);
23+
}
2124
};
22-
this.flushInterval = setInterval(this.flush, flushTimeout);
25+
this.timer = setTimeout(task, taskTimeout);
26+
this.toDispose = new DisposableCollection(
27+
Disposable.create(() => (this.disposed = true)),
28+
Disposable.create(() => clearTimeout(this.timer)),
29+
Disposable.create(() => task())
30+
);
2331
}
2432

2533
addChunk(
@@ -29,17 +37,17 @@ export class SimpleBuffer implements Disposable {
2937
this.chunks.get(severity)?.push(chunk);
3038
}
3139

32-
private clearChunks(): void {
33-
Chunks.clear(this.chunks);
34-
}
35-
3640
dispose(): void {
37-
this.flush();
38-
clearInterval(this.flushInterval);
39-
this.clearChunks();
40-
this.flushInterval = undefined;
41+
this.toDispose.dispose();
4142
}
4243
}
44+
export namespace AutoFlushingBuffer {
45+
/**
46+
* _"chunking and sending every 16ms (60hz) is the best for small amount of data
47+
* To be able to crunch more data without the cpu going to high, I opted for a 30fps refresh rate, hence the 32msec"_
48+
*/
49+
export const DEFAULT_FLUSH_TIMEOUT_MS = 32;
50+
}
4351

4452
type Chunks = Map<OutputMessage.Severity, Uint8Array[]>;
4553
namespace Chunks {

0 commit comments

Comments
 (0)