Skip to content

Commit c01107d

Browse files
Don't dispose a serial session if it's not open (#66)
* 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 c01107d

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,10 @@ class MicrobitWebUSBConnectionImpl
564564
protected eventActivated(type: string): void {
565565
switch (type as keyof SerialConnectionEventMap) {
566566
case "serialdata": {
567-
// Prevent starting serial when flashing.
568-
if (!this.flashing) {
567+
// Prevent starting serial if already started and when flashing.
568+
if (!this.addedListeners.serialdata && !this.flashing) {
569569
this.startSerialInternal();
570570
}
571-
// Allows for reinstating serial after flashing.
572571
this.addedListeners.serialdata++;
573572
break;
574573
}
@@ -578,8 +577,10 @@ class MicrobitWebUSBConnectionImpl
578577
protected async eventDeactivated(type: string) {
579578
switch (type as keyof SerialConnectionEventMap) {
580579
case "serialdata": {
581-
this.stopSerialInternal();
582580
this.addedListeners.serialdata--;
581+
if (!this.addedListeners.serialdata) {
582+
this.stopSerialInternal();
583+
}
583584
break;
584585
}
585586
}

0 commit comments

Comments
 (0)