@@ -148,15 +148,10 @@ class MicrobitWebUSBConnectionImpl
148148
149149 private flashing : boolean = false ;
150150 private disconnectAfterFlash : boolean = false ;
151- private visibilityReconnect : boolean = false ;
152151 private visibilityChangeListener = ( ) => {
153152 if ( document . visibilityState === "visible" ) {
154- if (
155- this . visibilityReconnect &&
156- this . status !== ConnectionStatus . CONNECTED
157- ) {
153+ if ( this . status === ConnectionStatus . PAUSED ) {
158154 this . disconnectAfterFlash = false ;
159- this . visibilityReconnect = false ;
160155 if ( ! this . flashing ) {
161156 this . log ( "Reconnecting visible tab" ) ;
162157 this . connect ( ) ;
@@ -165,10 +160,9 @@ class MicrobitWebUSBConnectionImpl
165160 } else {
166161 if ( ! this . unloading && this . status === ConnectionStatus . CONNECTED ) {
167162 if ( ! this . flashing ) {
168- this . log ( "Disconnecting hidden tab" ) ;
169- this . disconnect ( ) . then ( ( ) => {
170- this . visibilityReconnect = true ;
171- } ) ;
163+ this . log ( "Pausing connection for hidden tab" ) ;
164+ // Transition to PAUSED not DISCONNECTED
165+ this . disconnect ( false , ConnectionStatus . PAUSED ) ;
172166 } else {
173167 this . log ( "Scheduling disconnect of hidden tab for after flash" ) ;
174168 this . disconnectAfterFlash = true ;
@@ -346,8 +340,7 @@ class MicrobitWebUSBConnectionImpl
346340 if ( this . disconnectAfterFlash ) {
347341 this . log ( "Disconnecting after flash due to tab visibility" ) ;
348342 this . disconnectAfterFlash = false ;
349- await this . disconnect ( ) ;
350- this . visibilityReconnect = true ;
343+ await this . disconnect ( false , ConnectionStatus . PAUSED ) ;
351344 } else {
352345 if ( this . addedListeners . serialdata ) {
353346 this . log ( "Reinstating serial after flash" ) ;
@@ -391,7 +384,10 @@ class MicrobitWebUSBConnectionImpl
391384 } ) ;
392385 }
393386
394- async disconnect ( quiet ?: boolean ) : Promise < void > {
387+ async disconnect (
388+ quiet ?: boolean ,
389+ finalStatus : ConnectionStatus = ConnectionStatus . DISCONNECTED ,
390+ ) : Promise < void > {
395391 try {
396392 if ( this . connection ) {
397393 await this . stopSerialInternal ( ) ;
@@ -407,7 +403,7 @@ class MicrobitWebUSBConnectionImpl
407403 }
408404 } finally {
409405 this . connection = undefined ;
410- this . setStatus ( ConnectionStatus . DISCONNECTED ) ;
406+ this . setStatus ( finalStatus ) ;
411407 if ( ! quiet ) {
412408 this . logging . log ( "Disconnection complete" ) ;
413409 this . logging . event ( {
@@ -420,7 +416,6 @@ class MicrobitWebUSBConnectionImpl
420416
421417 private setStatus ( newStatus : ConnectionStatus ) {
422418 this . status = newStatus ;
423- this . visibilityReconnect = false ;
424419 this . log ( "USB connection status " + newStatus ) ;
425420 this . dispatchTypedEvent ( "status" , new ConnectionStatusEvent ( newStatus ) ) ;
426421 }
0 commit comments