Skip to content

Commit 74eb085

Browse files
Ignore delegate status on radio bridge disconnect
Don't dispose a serial session if it's not open Respect the number of serial listeners
1 parent 1bcf66b commit 74eb085

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/usb-radio-bridge.ts

Lines changed: 6 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 (
@@ -209,7 +212,9 @@ class MicrobitRadioBridgeConnectionImpl
209212
}
210213
this.serialSessionOpen = false;
211214
this.disconnectPromise = (async () => {
215+
this.ignoreDelegateStatus = true;
212216
await this.serialSession?.dispose(true);
217+
this.ignoreDelegateStatus = false;
213218
this.disconnectPromise = undefined;
214219
})();
215220
}

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)