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

Commit b844da1

Browse files
authored
Merge pull request #1494 from nya3jp/linecb
Fix line-oriented callbacks
2 parents a11a313 + 81ba0ab commit b844da1

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/arduino/arduino.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,28 @@ export class ArduinoApp {
752752
}
753753
return ret;
754754
}
755-
const stdoutcb = (line: string) => {
755+
756+
// Wrap line-oriented callbacks to accept arbitrary chunks of data.
757+
const wrapLineCallback = (callback: (line: string) => void) => {
758+
let buffer = "";
759+
let startIndex = 0;
760+
return (data: string) => {
761+
buffer += data;
762+
while (true) {
763+
const pos = buffer.indexOf(os.EOL, startIndex);
764+
if (pos < 0) {
765+
startIndex = buffer.length;
766+
break;
767+
}
768+
const line = buffer.substring(0, pos + os.EOL.length);
769+
buffer = buffer.substring(pos + os.EOL.length);
770+
startIndex = 0;
771+
callback(line);
772+
}
773+
};
774+
}
775+
776+
const stdoutcb = wrapLineCallback((line: string) => {
756777
if (cocopa.callback) {
757778
cocopa.callback(line);
758779
}
@@ -764,8 +785,8 @@ export class ArduinoApp {
764785
arduinoChannel.channel.append(line);
765786
}
766787
}
767-
}
768-
const stderrcb = (line: string) => {
788+
});
789+
const stderrcb = wrapLineCallback((line: string) => {
769790
if (os.platform() === "win32") {
770791
line = line.trim();
771792
if (line.length <= 0) {
@@ -792,7 +813,7 @@ export class ArduinoApp {
792813
}
793814
}
794815
arduinoChannel.channel.append(line);
795-
}
816+
});
796817

797818
return await util.spawn(
798819
this._settings.commandPath,

0 commit comments

Comments
 (0)