Skip to content

Commit 9a050a5

Browse files
Don't dispose a serial session if it's not open
Respect the number of serial listeners and only start and stop serial when required.
1 parent 1bcf66b commit 9a050a5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/usb-radio-bridge.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ class MicrobitRadioBridgeConnectionImpl
8383
const currentStatus = this.status;
8484
if (e.status !== ConnectionStatus.CONNECTED) {
8585
this.setStatus(e.status);
86-
this.serialSession?.dispose();
86+
if (this.serialSessionOpen) {
87+
// If the session is already closed we don't need to dispose.
88+
this.serialSession?.dispose();
89+
}
8790
} else {
8891
this.status = ConnectionStatus.DISCONNECTED;
8992
if (

lib/usb.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,10 @@ class MicrobitWebUSBConnectionImpl
564564
protected eventActivated(type: string): void {
565565
switch (type as keyof SerialConnectionEventMap) {
566566
case "serialdata": {
567+
if (this.addedListeners.serialdata) {
568+
this.addedListeners.serialdata++;
569+
break;
570+
}
567571
// Prevent starting serial when flashing.
568572
if (!this.flashing) {
569573
this.startSerialInternal();
@@ -578,8 +582,10 @@ class MicrobitWebUSBConnectionImpl
578582
protected async eventDeactivated(type: string) {
579583
switch (type as keyof SerialConnectionEventMap) {
580584
case "serialdata": {
581-
this.stopSerialInternal();
582585
this.addedListeners.serialdata--;
586+
if (!this.addedListeners.serialdata) {
587+
this.stopSerialInternal();
588+
}
583589
break;
584590
}
585591
}

0 commit comments

Comments
 (0)