Skip to content

Commit 990c1a1

Browse files
hlovdaladiazulay
authored andcommitted
Add cleanup expression
1 parent 0678c9a commit 990c1a1

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/arduino/arduino.ts

+24-11
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@ export class ArduinoApp {
137137
arduinoChannel.show();
138138
arduinoChannel.start(`Upload sketch - ${dc.sketch}`);
139139

140-
const serialMonitor = SerialMonitor.getInstance();
141-
142-
const needRestore = await serialMonitor.closeSerialMonitor(dc.port);
143-
UsbDetector.getInstance().pauseListening();
144140
await vscode.workspace.saveAll(false);
145141

146142
if (!await this.runPreBuildCommand(dc)) {
@@ -205,17 +201,28 @@ export class ArduinoApp {
205201
const msg = "Output path is not specified. Unable to reuse previously compiled files. Upload could be slow. See README.";
206202
arduinoChannel.warning(msg);
207203
}
204+
205+
// stop serial monitor when everything is prepared and good
206+
// what makes restoring of its previous state easier
207+
const restoreSerialMonitor = await SerialMonitor.getInstance().closeSerialMonitor(dc.port);
208+
UsbDetector.getInstance().pauseListening();
209+
210+
const cleanup = async () => {
211+
UsbDetector.getInstance().resumeListening();
212+
if (restoreSerialMonitor) {
213+
await SerialMonitor.getInstance().openSerialMonitor();
214+
}
215+
}
216+
208217
await util.spawn(
209218
this._settings.commandPath,
210219
arduinoChannel.channel,
211220
args,
212221
).then(async () => {
213-
UsbDetector.getInstance().resumeListening();
214-
if (needRestore) {
215-
await serialMonitor.openSerialMonitor();
216-
}
222+
await cleanup();
217223
arduinoChannel.end(`Uploaded the sketch: ${dc.sketch}${os.EOL}`);
218-
}, (reason) => {
224+
}, async (reason) => {
225+
await cleanup();
219226
const msg = reason.code ?
220227
`Exit with code=${reason.code}${os.EOL}` :
221228
reason.message ?
@@ -292,16 +299,22 @@ export class ArduinoApp {
292299
let success = false;
293300
const compilerParserContext = makeCompilerParserContext(dc);
294301

302+
const cleanup = async () => {
303+
await Promise.resolve();
304+
}
305+
295306
await util.spawn(
296307
this._settings.commandPath,
297308
arduinoChannel.channel,
298309
args,
299310
undefined,
300311
compilerParserContext.callback,
301-
).then(() => {
312+
).then(async () => {
313+
await cleanup();
302314
arduinoChannel.end(`Finished verifying sketch - ${dc.sketch}${os.EOL}`);
303315
success = true;
304-
}, (reason) => {
316+
}, async (reason) => {
317+
await cleanup();
305318
const msg = reason.code ?
306319
`Exit with code=${reason.code}${os.EOL}` :
307320
reason.message ?

0 commit comments

Comments
 (0)