Skip to content

Commit 869c6a9

Browse files
Disconnect quietly when failing to connect via WebUSB (#59)
This avoids logging disconnect events when the device wasn't connected in the first place.
1 parent 6de2ae7 commit 869c6a9

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lib/usb.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,26 +354,30 @@ class MicrobitWebUSBConnectionImpl
354354
});
355355
}
356356

357-
async disconnect(): Promise<void> {
357+
async disconnect(quiet?: boolean): Promise<void> {
358358
try {
359359
if (this.connection) {
360360
await this.stopSerialInternal();
361361
await this.connection.disconnectAsync();
362362
}
363363
} catch (e) {
364-
this.log("Error during disconnection:\r\n" + e);
365-
this.logging.event({
366-
type: "WebUSB-error",
367-
message: "error-disconnecting",
368-
});
364+
if (!quiet) {
365+
this.log("Error during disconnection:\r\n" + e);
366+
this.logging.event({
367+
type: "WebUSB-error",
368+
message: "error-disconnecting",
369+
});
370+
}
369371
} finally {
370372
this.connection = undefined;
371373
this.setStatus(ConnectionStatus.DISCONNECTED);
372-
this.logging.log("Disconnection complete");
373-
this.logging.event({
374-
type: "WebUSB-info",
375-
message: "disconnected",
376-
});
374+
if (!quiet) {
375+
this.logging.log("Disconnection complete");
376+
this.logging.event({
377+
type: "WebUSB-info",
378+
message: "disconnected",
379+
});
380+
}
377381
}
378382
}
379383

@@ -402,7 +406,7 @@ class MicrobitWebUSBConnectionImpl
402406
// Disconnect from the microbit.
403407
// Any new connection reallocates all the internals.
404408
// Use the top-level API so any listeners reflect that we're disconnected.
405-
await this.disconnect();
409+
await this.disconnect(true);
406410

407411
const enriched = enrichedError(e);
408412
// Sanitise error message, replace all special chars with '-', if last char is '-' remove it

0 commit comments

Comments
 (0)