Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Fix line-oriented callbacks #1494

Merged
merged 3 commits into from
Apr 29, 2022
Merged
Changes from all 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
29 changes: 25 additions & 4 deletions src/arduino/arduino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,28 @@ export class ArduinoApp {
}
return ret;
}
const stdoutcb = (line: string) => {

// Wrap line-oriented callbacks to accept arbitrary chunks of data.
const wrapLineCallback = (callback: (line: string) => void) => {
let buffer = "";
let startIndex = 0;
return (data: string) => {
buffer += data;
while (true) {
const pos = buffer.indexOf(os.EOL, startIndex);
if (pos < 0) {
startIndex = buffer.length;
break;
}
const line = buffer.substring(0, pos + os.EOL.length);
buffer = buffer.substring(pos + os.EOL.length);
startIndex = 0;
callback(line);
}
};
}

const stdoutcb = wrapLineCallback((line: string) => {
if (cocopa.callback) {
cocopa.callback(line);
}
Expand All @@ -764,8 +785,8 @@ export class ArduinoApp {
arduinoChannel.channel.append(line);
}
}
}
const stderrcb = (line: string) => {
});
const stderrcb = wrapLineCallback((line: string) => {
if (os.platform() === "win32") {
line = line.trim();
if (line.length <= 0) {
Expand All @@ -792,7 +813,7 @@ export class ArduinoApp {
}
}
arduinoChannel.channel.append(line);
}
});

return await util.spawn(
this._settings.commandPath,
Expand Down