Skip to content

Commit 2d3c1af

Browse files
Alt approach, respect number of serial listeners
1 parent 6373a63 commit 2d3c1af

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
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: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class MicrobitWebUSBConnectionImpl
189189
setTimeout(() => {
190190
if (this.status === ConnectionStatus.CONNECTED) {
191191
this.unloading = false;
192-
if (this.listeningToSerialData) {
192+
if (this.eventListeners.serialdata) {
193193
this.startSerialInternal();
194194
}
195195
}
@@ -202,7 +202,9 @@ class MicrobitWebUSBConnectionImpl
202202
private logging: Logging;
203203
private deviceSelectionMode: DeviceSelectionMode;
204204

205-
private listeningToSerialData = false;
205+
private eventListeners: Record<string, number> = {
206+
serialdata: 0,
207+
};
206208

207209
constructor(options: MicrobitWebUSBConnectionOptions = {}) {
208210
super();
@@ -335,7 +337,7 @@ class MicrobitWebUSBConnectionImpl
335337
await this.disconnect();
336338
this.visibilityReconnect = true;
337339
} else {
338-
if (this.listeningToSerialData) {
340+
if (this.eventListeners.serialdata) {
339341
this.log("Reinstating serial after flash");
340342
if (this.connection.daplink) {
341343
await this.connection.daplink.connect();
@@ -491,7 +493,7 @@ class MicrobitWebUSBConnectionImpl
491493
} else {
492494
await withTimeout(this.connection.reconnectAsync(), 10_000);
493495
}
494-
if (this.listeningToSerialData && !this.flashing) {
496+
if (this.eventListeners.serialdata && !this.flashing) {
495497
this.startSerialInternal();
496498
}
497499
this.setStatus(ConnectionStatus.CONNECTED);
@@ -562,12 +564,16 @@ class MicrobitWebUSBConnectionImpl
562564
protected eventActivated(type: string): void {
563565
switch (type as keyof SerialConnectionEventMap) {
564566
case "serialdata": {
567+
if (this.eventListeners.serialdata) {
568+
this.eventListeners.serialdata++;
569+
break;
570+
}
565571
// Prevent starting serial when flashing.
566572
if (!this.flashing) {
567573
this.startSerialInternal();
568574
}
569575
// Allows for reinstating serial after flashing.
570-
this.listeningToSerialData = true;
576+
this.eventListeners.serialdata++;
571577
break;
572578
}
573579
}
@@ -576,8 +582,10 @@ class MicrobitWebUSBConnectionImpl
576582
protected async eventDeactivated(type: string) {
577583
switch (type as keyof SerialConnectionEventMap) {
578584
case "serialdata": {
579-
this.stopSerialInternal();
580-
this.listeningToSerialData = false;
585+
this.eventListeners.serialdata--;
586+
if (!this.eventListeners.serialdata) {
587+
this.stopSerialInternal();
588+
}
581589
break;
582590
}
583591
}

0 commit comments

Comments
 (0)