From d710f753a3b0c1be83ee2113759c4a6ec3af8c3a Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Wed, 25 Jun 2025 17:15:22 +0100 Subject: [PATCH] Disconnect quietly when failing to connect via WebUSB --- lib/device.ts | 2 +- lib/usb.ts | 28 ++++++++++++++++------------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/device.ts b/lib/device.ts index beaebdc..f066e30 100644 --- a/lib/device.ts +++ b/lib/device.ts @@ -189,7 +189,7 @@ export interface DeviceConnection> /** * Disconnect from the device. */ - disconnect(): Promise; + disconnect(quiet?: boolean): Promise; /** * Write serial data to the device. diff --git a/lib/usb.ts b/lib/usb.ts index 7265593..da1ea7e 100644 --- a/lib/usb.ts +++ b/lib/usb.ts @@ -354,26 +354,30 @@ class MicrobitWebUSBConnectionImpl }); } - async disconnect(): Promise { + async disconnect(quiet?: boolean): Promise { try { if (this.connection) { await this.stopSerialInternal(); await this.connection.disconnectAsync(); } } catch (e) { - this.log("Error during disconnection:\r\n" + e); - this.logging.event({ - type: "WebUSB-error", - message: "error-disconnecting", - }); + if (!quiet) { + this.log("Error during disconnection:\r\n" + e); + this.logging.event({ + type: "WebUSB-error", + message: "error-disconnecting", + }); + } } finally { this.connection = undefined; this.setStatus(ConnectionStatus.DISCONNECTED); - this.logging.log("Disconnection complete"); - this.logging.event({ - type: "WebUSB-info", - message: "disconnected", - }); + if (!quiet) { + this.logging.log("Disconnection complete"); + this.logging.event({ + type: "WebUSB-info", + message: "disconnected", + }); + } } } @@ -402,7 +406,7 @@ class MicrobitWebUSBConnectionImpl // Disconnect from the microbit. // Any new connection reallocates all the internals. // Use the top-level API so any listeners reflect that we're disconnected. - await this.disconnect(); + await this.disconnect(true); const enriched = enrichedError(e); // Sanitise error message, replace all special chars with '-', if last char is '-' remove it