@@ -109,6 +109,12 @@ class MicrobitWebUSBConnectionImpl
109
109
*/
110
110
private connection : DAPWrapper | undefined ;
111
111
112
+ /**
113
+ * Whether the serial read loop is running.
114
+ *
115
+ * This is false even if we have serial listeners when we're disconnected or flashing.
116
+ * The serial reads interfere with the flash process.
117
+ */
112
118
private serialState : boolean = false ;
113
119
114
120
private serialStateChangeQueue = new PromiseQueue ( ) ;
@@ -165,7 +171,7 @@ class MicrobitWebUSBConnectionImpl
165
171
setTimeout ( ( ) => {
166
172
if ( this . status === ConnectionStatus . CONNECTED ) {
167
173
this . unloading = false ;
168
- if ( this . addedListeners . serialdata ) {
174
+ if ( this . hasSerialEventListeners ( ) ) {
169
175
this . startSerialInternal ( ) ;
170
176
}
171
177
}
@@ -177,10 +183,6 @@ class MicrobitWebUSBConnectionImpl
177
183
178
184
private logging : Logging ;
179
185
180
- private addedListeners : Record < string , number > = {
181
- serialdata : 0 ,
182
- } ;
183
-
184
186
constructor (
185
187
options : MicrobitWebUSBConnectionOptions = { logging : new NullLogging ( ) } ,
186
188
) {
@@ -312,7 +314,7 @@ class MicrobitWebUSBConnectionImpl
312
314
await this . disconnect ( ) ;
313
315
this . visibilityReconnect = true ;
314
316
} else {
315
- if ( this . addedListeners . serialdata ) {
317
+ if ( this . hasSerialEventListeners ( ) ) {
316
318
this . log ( "Reinstating serial after flash" ) ;
317
319
if ( this . connection . daplink ) {
318
320
await this . connection . daplink . connect ( ) ;
@@ -461,7 +463,7 @@ class MicrobitWebUSBConnectionImpl
461
463
this . connection = new DAPWrapper ( device , this . logging ) ;
462
464
}
463
465
await withTimeout ( this . connection . reconnectAsync ( ) , 10_000 ) ;
464
- if ( this . addedListeners . serialdata && ! this . flashing ) {
466
+ if ( this . hasSerialEventListeners ( ) && ! this . flashing ) {
465
467
this . startSerialInternal ( ) ;
466
468
}
467
469
this . setStatus ( ConnectionStatus . CONNECTED ) ;
@@ -483,12 +485,10 @@ class MicrobitWebUSBConnectionImpl
483
485
protected eventActivated ( type : string ) : void {
484
486
switch ( type as keyof SerialConnectionEventMap ) {
485
487
case "serialdata" : {
486
- // Prevent starting serial when flashing.
488
+ // Prevent starting serial when flashing. We'll reinstate later.
487
489
if ( ! this . flashing ) {
488
490
this . startSerialInternal ( ) ;
489
491
}
490
- // Allows for reinstating serial after flashing.
491
- this . addedListeners . serialdata ++ ;
492
492
break ;
493
493
}
494
494
}
@@ -498,11 +498,13 @@ class MicrobitWebUSBConnectionImpl
498
498
switch ( type as keyof SerialConnectionEventMap ) {
499
499
case "serialdata" : {
500
500
this . stopSerialInternal ( ) ;
501
- this . addedListeners . serialdata -- ;
502
501
break ;
503
502
}
504
503
}
505
504
}
505
+ private hasSerialEventListeners ( ) {
506
+ return this . getActiveEvents ( ) . includes ( "serialdata" ) ;
507
+ }
506
508
}
507
509
508
510
const genericErrorSuggestingReconnect = ( e : any ) =>
0 commit comments