Skip to content

Commit 2e7b125

Browse files
change output buffer to setTimeout
1 parent 7ba98a2 commit 2e7b125

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
7676
.filter(notEmpty)
7777
.shift() ?? error.details
7878
);
79-
this.sendResponse(
80-
error.details + '\n\n' + message,
81-
OutputMessage.Severity.Error
79+
const chunk = new TextEncoder().encode(
80+
`${error.details}'\n\n'${message}`
8281
);
82+
handler.addChunk(chunk, OutputMessage.Severity.Error);
83+
8384
reject(CoreError.VerifyFailed(message, compilerErrors));
8485
}
8586
})
@@ -181,7 +182,8 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
181182
firstToUpperCase(task),
182183
error.details
183184
);
184-
this.sendResponse(error.details, OutputMessage.Severity.Error);
185+
const chunk = new TextEncoder().encode(error.details);
186+
handler.addChunk(chunk, OutputMessage.Severity.Error);
185187
reject(
186188
errorHandler(
187189
message,
@@ -245,7 +247,8 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
245247
);
246248
reject(error);
247249
} else {
248-
this.sendResponse(error.details, OutputMessage.Severity.Error);
250+
const chunk = new TextEncoder().encode(error.details);
251+
handler.addChunk(chunk, OutputMessage.Severity.Error);
249252
reject(
250253
CoreError.BurnBootloaderFailed(
251254
nls.localize(
@@ -288,6 +291,7 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
288291
private createOnDataHandler<R extends StreamingResponse>(): Disposable & {
289292
stderr: Buffer[];
290293
onData: (response: R) => void;
294+
addChunk: (chunk: Uint8Array, severity?: OutputMessage.Severity) => void;
291295
} {
292296
const stderr: Buffer[] = [];
293297
const buffer = new SimpleBuffer((chunks) => {
@@ -301,10 +305,14 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
301305
buffer.addChunk(out);
302306
buffer.addChunk(err, OutputMessage.Severity.Error);
303307
});
308+
const addChunk = (chunk: Uint8Array, severity?: OutputMessage.Severity) =>
309+
buffer.addChunk(chunk, severity);
310+
304311
return {
305312
dispose: () => buffer.dispose(),
306313
stderr,
307314
onData,
315+
addChunk,
308316
};
309317
}
310318

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

+37-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const DEFAULT_FLUS_TIMEOUT_MS = 32;
66
export class SimpleBuffer implements Disposable {
77
private readonly chunks = Chunks.create();
88
private readonly flush: () => void;
9-
private flushInterval?: NodeJS.Timeout;
9+
private flushTimeout?: NodeJS.Timeout;
10+
private disposed = false;
1011

1112
constructor(
1213
onFlush: (chunks: Map<OutputMessage.Severity, string | undefined>) => void,
@@ -19,7 +20,8 @@ export class SimpleBuffer implements Disposable {
1920
onFlush(chunks);
2021
}
2122
};
22-
this.flushInterval = setInterval(this.flush, flushTimeout);
23+
24+
this.setTimeoutVariable(flushTimeout);
2325
}
2426

2527
addChunk(
@@ -33,11 +35,40 @@ export class SimpleBuffer implements Disposable {
3335
Chunks.clear(this.chunks);
3436
}
3537

38+
private setTimeoutVariable(flushTimeout: number): void {
39+
let isDisposed = this.disposed;
40+
if (isDisposed) {
41+
// once "isDisposed" is true we stop
42+
// creating timeouts and do one more
43+
// flush AFTER any setTimeout
44+
// callback that may be in progress
45+
this.flush();
46+
isDisposed = false;
47+
return;
48+
}
49+
50+
if (!this.flushTimeout) {
51+
const onTimeout = () => {
52+
this.flush();
53+
this.clearTimeoutVariable();
54+
};
55+
56+
this.flushTimeout = setTimeout(() => {
57+
onTimeout();
58+
this.setTimeoutVariable(flushTimeout);
59+
}, flushTimeout);
60+
}
61+
}
62+
63+
private clearTimeoutVariable(): void {
64+
if (this.flushTimeout) {
65+
clearTimeout(this.flushTimeout);
66+
this.flushTimeout = undefined;
67+
}
68+
}
69+
3670
dispose(): void {
37-
this.flush();
38-
clearInterval(this.flushInterval);
39-
this.clearChunks();
40-
this.flushInterval = undefined;
71+
this.disposed = true;
4172
}
4273
}
4374

0 commit comments

Comments
 (0)