Skip to content

Commit d710f75

Browse files
Disconnect quietly when failing to connect via WebUSB
1 parent 6de2ae7 commit d710f75

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

lib/device.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export interface DeviceConnection<M extends ValueIsEvent<M>>
189189
/**
190190
* Disconnect from the device.
191191
*/
192-
disconnect(): Promise<void>;
192+
disconnect(quiet?: boolean): Promise<void>;
193193

194194
/**
195195
* Write serial data to the device.

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)