@@ -109,6 +109,12 @@ class MicrobitWebUSBConnectionImpl
109109 */
110110 private connection : DAPWrapper | undefined ;
111111
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+ */
112118 private serialState : boolean = false ;
113119
114120 private serialStateChangeQueue = new PromiseQueue ( ) ;
@@ -165,7 +171,7 @@ class MicrobitWebUSBConnectionImpl
165171 setTimeout ( ( ) => {
166172 if ( this . status === ConnectionStatus . CONNECTED ) {
167173 this . unloading = false ;
168- if ( this . addedListeners . serialdata ) {
174+ if ( this . hasSerialEventListeners ( ) ) {
169175 this . startSerialInternal ( ) ;
170176 }
171177 }
@@ -177,10 +183,6 @@ class MicrobitWebUSBConnectionImpl
177183
178184 private logging : Logging ;
179185
180- private addedListeners : Record < string , number > = {
181- serialdata : 0 ,
182- } ;
183-
184186 constructor (
185187 options : MicrobitWebUSBConnectionOptions = { logging : new NullLogging ( ) } ,
186188 ) {
@@ -312,7 +314,7 @@ class MicrobitWebUSBConnectionImpl
312314 await this . disconnect ( ) ;
313315 this . visibilityReconnect = true ;
314316 } else {
315- if ( this . addedListeners . serialdata ) {
317+ if ( this . hasSerialEventListeners ( ) ) {
316318 this . log ( "Reinstating serial after flash" ) ;
317319 if ( this . connection . daplink ) {
318320 await this . connection . daplink . connect ( ) ;
@@ -461,7 +463,7 @@ class MicrobitWebUSBConnectionImpl
461463 this . connection = new DAPWrapper ( device , this . logging ) ;
462464 }
463465 await withTimeout ( this . connection . reconnectAsync ( ) , 10_000 ) ;
464- if ( this . addedListeners . serialdata && ! this . flashing ) {
466+ if ( this . hasSerialEventListeners ( ) && ! this . flashing ) {
465467 this . startSerialInternal ( ) ;
466468 }
467469 this . setStatus ( ConnectionStatus . CONNECTED ) ;
@@ -483,12 +485,10 @@ class MicrobitWebUSBConnectionImpl
483485 protected eventActivated ( type : string ) : void {
484486 switch ( type as keyof SerialConnectionEventMap ) {
485487 case "serialdata" : {
486- // Prevent starting serial when flashing.
488+ // Prevent starting serial when flashing. We'll reinstate later.
487489 if ( ! this . flashing ) {
488490 this . startSerialInternal ( ) ;
489491 }
490- // Allows for reinstating serial after flashing.
491- this . addedListeners . serialdata ++ ;
492492 break ;
493493 }
494494 }
@@ -498,11 +498,13 @@ class MicrobitWebUSBConnectionImpl
498498 switch ( type as keyof SerialConnectionEventMap ) {
499499 case "serialdata" : {
500500 this . stopSerialInternal ( ) ;
501- this . addedListeners . serialdata -- ;
502501 break ;
503502 }
504503 }
505504 }
505+ private hasSerialEventListeners ( ) {
506+ return this . getActiveEvents ( ) . includes ( "serialdata" ) ;
507+ }
506508}
507509
508510const genericErrorSuggestingReconnect = ( e : any ) =>
0 commit comments