Skip to content

change output buffer to setTimeout instead of setInterval #1123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions arduino-ide-extension/src/node/core-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
.filter(notEmpty)
.shift() ?? error.details
);
this.sendResponse(
error.details + '\n\n' + message,
OutputMessage.Severity.Error
const chunk = new TextEncoder().encode(
`${error.details}'\n\n'${message}`
);
handler.addChunk(chunk, OutputMessage.Severity.Error);

reject(CoreError.VerifyFailed(message, compilerErrors));
}
})
Expand Down Expand Up @@ -181,7 +182,8 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
firstToUpperCase(task),
error.details
);
this.sendResponse(error.details, OutputMessage.Severity.Error);
const chunk = new TextEncoder().encode(error.details);
handler.addChunk(chunk, OutputMessage.Severity.Error);
reject(
errorHandler(
message,
Expand Down Expand Up @@ -245,7 +247,8 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
);
reject(error);
} else {
this.sendResponse(error.details, OutputMessage.Severity.Error);
const chunk = new TextEncoder().encode(error.details);
handler.addChunk(chunk, OutputMessage.Severity.Error);
reject(
CoreError.BurnBootloaderFailed(
nls.localize(
Expand Down Expand Up @@ -288,6 +291,7 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
private createOnDataHandler<R extends StreamingResponse>(): Disposable & {
stderr: Buffer[];
onData: (response: R) => void;
addChunk: (chunk: Uint8Array, severity?: OutputMessage.Severity) => void;
} {
const stderr: Buffer[] = [];
const buffer = new SimpleBuffer((chunks) => {
Expand All @@ -301,10 +305,14 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
buffer.addChunk(out);
buffer.addChunk(err, OutputMessage.Severity.Error);
});
const addChunk = (chunk: Uint8Array, severity?: OutputMessage.Severity) =>
buffer.addChunk(chunk, severity);

return {
dispose: () => buffer.dispose(),
stderr,
onData,
addChunk,
};
}

Expand Down
42 changes: 36 additions & 6 deletions arduino-ide-extension/src/node/utils/simple-buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const DEFAULT_FLUS_TIMEOUT_MS = 32;
export class SimpleBuffer implements Disposable {
private readonly chunks = Chunks.create();
private readonly flush: () => void;
private flushInterval?: NodeJS.Timeout;
private flushTimeout?: NodeJS.Timeout;
private disposed = false;

constructor(
onFlush: (chunks: Map<OutputMessage.Severity, string | undefined>) => void,
Expand All @@ -19,7 +20,8 @@ export class SimpleBuffer implements Disposable {
onFlush(chunks);
}
};
this.flushInterval = setInterval(this.flush, flushTimeout);

this.setTimeoutVariable(flushTimeout);
}

addChunk(
Expand All @@ -33,11 +35,39 @@ export class SimpleBuffer implements Disposable {
Chunks.clear(this.chunks);
}

private setTimeoutVariable(flushTimeout: number): void {
const isDisposed = this.disposed;
if (isDisposed) {
// once "isDisposed" is true we stop
// creating timeouts and do one more
// flush AFTER any setTimeout
// callback that may be in progress
this.flush();
return;
}

if (!this.flushTimeout) {
const onTimeout = () => {
this.flush();
this.clearTimeoutVariable();
};

this.flushTimeout = setTimeout(() => {
onTimeout();
this.setTimeoutVariable(flushTimeout);
}, flushTimeout);
}
}

private clearTimeoutVariable(): void {
if (this.flushTimeout) {
clearTimeout(this.flushTimeout);
this.flushTimeout = undefined;
}
}

dispose(): void {
this.flush();
clearInterval(this.flushInterval);
this.clearChunks();
this.flushInterval = undefined;
this.disposed = true;
}
}

Expand Down