Skip to content

Commit d567109

Browse files
create buffer provider
1 parent 3c56008 commit d567109

File tree

2 files changed

+79
-41
lines changed

2 files changed

+79
-41
lines changed

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

+51-41
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { firstToUpperCase, firstToLowerCase } from '../common/utils';
2525
import { Port } from './cli-protocol/cc/arduino/cli/commands/v1/port_pb';
2626
import { nls } from '@theia/core';
2727
import { MonitorManager } from './monitor-manager';
28+
import { OutputPanelBufferProvider } from './output-panel-buffer-provider';
2829

2930
@injectable()
3031
export class CoreServiceImpl extends CoreClientAware implements CoreService {
@@ -39,25 +40,8 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
3940

4041
protected uploading = false;
4142

42-
private outputString: string;
43-
4443
private FLUSH_OUTPUT_MESSAGES_TIMEOUT_MS = 32;
4544

46-
private flushOutputMessagesInterval?: NodeJS.Timeout;
47-
48-
setOutputMessagesInterval(): void {
49-
this.flushOutputMessagesInterval = setInterval(() => {
50-
this.responseService.appendToOutput({
51-
chunk: this.outputString,
52-
});
53-
}, this.FLUSH_OUTPUT_MESSAGES_TIMEOUT_MS);
54-
}
55-
56-
clearOutputMessagesInterval(): void {
57-
clearInterval(this.flushOutputMessagesInterval);
58-
this.flushOutputMessagesInterval = undefined;
59-
}
60-
6145
async compile(
6246
options: CoreService.Compile.Options & {
6347
exportBinaries?: boolean;
@@ -92,18 +76,25 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
9276
this.mergeSourceOverrides(compileReq, options);
9377

9478
const result = client.compile(compileReq);
79+
80+
const compileBuffer = new OutputPanelBufferProvider(
81+
this.flushOutputPanelMessages,
82+
this.FLUSH_OUTPUT_MESSAGES_TIMEOUT_MS
83+
);
9584
try {
9685
await new Promise<void>((resolve, reject) => {
9786
result.on('data', (cr: CompileResponse) => {
98-
this.responseService.appendToOutput({
99-
chunk: Buffer.from(cr.getOutStream_asU8()).toString(),
100-
});
101-
this.responseService.appendToOutput({
102-
chunk: Buffer.from(cr.getErrStream_asU8()).toString(),
103-
});
87+
compileBuffer.addChunk(cr.getOutStream_asU8());
88+
compileBuffer.addChunk(cr.getErrStream_asU8());
89+
});
90+
result.on('error', (error) => {
91+
compileBuffer.clearFlushInterval();
92+
reject(error);
93+
});
94+
result.on('end', () => {
95+
compileBuffer.clearFlushInterval();
96+
resolve();
10497
});
105-
result.on('error', (error) => reject(error));
106-
result.on('end', () => resolve());
10798
});
10899
this.responseService.appendToOutput({
109100
chunk: '\n--------------------------\nCompilation complete.\n',
@@ -193,19 +184,22 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
193184

194185
const result = responseHandler(client, req);
195186

196-
this.setOutputMessagesInterval();
197-
187+
const uploadBuffer = new OutputPanelBufferProvider(
188+
this.flushOutputPanelMessages,
189+
this.FLUSH_OUTPUT_MESSAGES_TIMEOUT_MS
190+
);
198191
try {
199192
await new Promise<void>((resolve, reject) => {
200193
result.on('data', (resp: UploadResponse) => {
201-
this.outputString =
202-
this.outputString +
203-
Buffer.from(resp.getOutStream_asU8()).toString() +
204-
Buffer.from(resp.getErrStream_asU8()).toString();
194+
uploadBuffer.addChunk(resp.getOutStream_asU8());
195+
uploadBuffer.addChunk(resp.getErrStream_asU8());
196+
});
197+
result.on('error', (error) => {
198+
uploadBuffer.clearFlushInterval();
199+
reject(error);
205200
});
206-
result.on('error', (error) => reject(error));
207201
result.on('end', () => {
208-
this.clearOutputMessagesInterval();
202+
uploadBuffer.clearFlushInterval();
209203
resolve();
210204
});
211205
});
@@ -260,18 +254,25 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
260254
burnReq.setVerify(options.verify);
261255
burnReq.setVerbose(options.verbose);
262256
const result = client.burnBootloader(burnReq);
257+
258+
const bootloaderBuffer = new OutputPanelBufferProvider(
259+
this.flushOutputPanelMessages,
260+
this.FLUSH_OUTPUT_MESSAGES_TIMEOUT_MS
261+
);
263262
try {
264263
await new Promise<void>((resolve, reject) => {
265264
result.on('data', (resp: BurnBootloaderResponse) => {
266-
this.responseService.appendToOutput({
267-
chunk: Buffer.from(resp.getOutStream_asU8()).toString(),
268-
});
269-
this.responseService.appendToOutput({
270-
chunk: Buffer.from(resp.getErrStream_asU8()).toString(),
271-
});
265+
bootloaderBuffer.addChunk(resp.getOutStream_asU8());
266+
bootloaderBuffer.addChunk(resp.getErrStream_asU8());
267+
});
268+
result.on('error', (error) => {
269+
bootloaderBuffer.clearFlushInterval();
270+
reject(error);
271+
});
272+
result.on('end', () => {
273+
bootloaderBuffer.clearFlushInterval();
274+
resolve();
272275
});
273-
result.on('error', (error) => reject(error));
274-
result.on('end', () => resolve());
275276
});
276277
} catch (e) {
277278
const errorMessage = nls.localize(
@@ -303,4 +304,13 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
303304
}
304305
}
305306
}
307+
308+
private flushOutputPanelMessages(chunk: string): void {
309+
this.responseService.appendToOutput({
310+
chunk,
311+
});
312+
this.responseService.appendToOutput({
313+
chunk,
314+
});
315+
}
306316
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export class OutputPanelBufferProvider {
2+
chunks: Uint8Array[] = [];
3+
4+
private flushInterval?: NodeJS.Timeout;
5+
6+
constructor(onFlush: (chunk: string) => void, flushTimeout: number) {
7+
this.flushInterval = setInterval(() => {
8+
const chunkString = Buffer.concat(this.chunks).toString();
9+
onFlush(chunkString);
10+
this.clearChunks();
11+
}, flushTimeout);
12+
}
13+
14+
public addChunk(chunk: Uint8Array): void {
15+
this.chunks.push(chunk);
16+
}
17+
18+
private clearChunks() {
19+
this.chunks = [];
20+
}
21+
22+
public clearFlushInterval(): void {
23+
this.clearChunks();
24+
25+
clearInterval(this.flushInterval);
26+
this.flushInterval = undefined;
27+
}
28+
}

0 commit comments

Comments
 (0)