@@ -142,62 +142,6 @@ class MicrobitWebUSBConnectionImpl
142142 } ;
143143
144144 private flashing : boolean = false ;
145- private disconnectAfterFlash : boolean = false ;
146- private visibilityReconnect : boolean = false ;
147- private visibilityChangeListener = ( ) => {
148- if ( document . visibilityState === "visible" ) {
149- if (
150- this . visibilityReconnect &&
151- this . status !== ConnectionStatus . CONNECTED
152- ) {
153- this . disconnectAfterFlash = false ;
154- this . visibilityReconnect = false ;
155- if ( ! this . flashing ) {
156- this . log ( "Reconnecting visible tab" ) ;
157- this . connect ( ) ;
158- }
159- }
160- } else {
161- if ( ! this . unloading && this . status === ConnectionStatus . CONNECTED ) {
162- if ( ! this . flashing ) {
163- this . log ( "Disconnecting hidden tab" ) ;
164- this . disconnect ( ) . then ( ( ) => {
165- this . visibilityReconnect = true ;
166- } ) ;
167- } else {
168- this . log ( "Scheduling disconnect of hidden tab for after flash" ) ;
169- this . disconnectAfterFlash = true ;
170- }
171- }
172- }
173- } ;
174-
175- private unloading = false ;
176-
177- private beforeUnloadListener = ( ) => {
178- // If serial is in progress when the page unloads with V1 DAPLink 0254 or V2 0255
179- // then it'll fail to reconnect with mismatched command/response errors.
180- // Try hard to disconnect as a workaround.
181- // https://github.com/microbit-foundation/python-editor-v3/issues/89
182- this . unloading = true ;
183- this . stopSerialInternal ( ) ;
184- // The user might stay on the page if they have unsaved changes and there's another beforeunload listener.
185- window . addEventListener (
186- "focus" ,
187- ( ) => {
188- const assumePageIsStayingOpenDelay = 1000 ;
189- setTimeout ( ( ) => {
190- if ( this . status === ConnectionStatus . CONNECTED ) {
191- this . unloading = false ;
192- if ( this . addedListeners . serialdata ) {
193- this . startSerialInternal ( ) ;
194- }
195- }
196- } , assumePageIsStayingOpenDelay ) ;
197- } ,
198- { once : true } ,
199- ) ;
200- } ;
201145
202146 private logging : Logging ;
203147 private deviceSelectionMode : DeviceSelectionMode ;
@@ -221,30 +165,12 @@ class MicrobitWebUSBConnectionImpl
221165 if ( navigator . usb ) {
222166 navigator . usb . addEventListener ( "disconnect" , this . handleDisconnect ) ;
223167 }
224- if ( typeof window !== "undefined" ) {
225- window . addEventListener ( "beforeunload" , this . beforeUnloadListener ) ;
226- if ( window . document ) {
227- window . document . addEventListener (
228- "visibilitychange" ,
229- this . visibilityChangeListener ,
230- ) ;
231- }
232- }
233168 }
234169
235170 dispose ( ) {
236171 if ( navigator . usb ) {
237172 navigator . usb . removeEventListener ( "disconnect" , this . handleDisconnect ) ;
238173 }
239- if ( typeof window !== "undefined" ) {
240- window . removeEventListener ( "beforeunload" , this . beforeUnloadListener ) ;
241- if ( window . document ) {
242- window . document . removeEventListener (
243- "visibilitychange" ,
244- this . visibilityChangeListener ,
245- ) ;
246- }
247- }
248174 }
249175
250176 setRequestDeviceExclusionFilters ( exclusionFilters : USBDeviceFilter [ ] ) {
@@ -331,18 +257,11 @@ class MicrobitWebUSBConnectionImpl
331257 } finally {
332258 progress ( undefined , wasPartial ) ;
333259
334- if ( this . disconnectAfterFlash ) {
335- this . log ( "Disconnecting after flash due to tab visibility" ) ;
336- this . disconnectAfterFlash = false ;
337- await this . disconnect ( ) ;
338- this . visibilityReconnect = true ;
339- } else {
340- if ( this . addedListeners . serialdata ) {
341- this . log ( "Reinstating serial after flash" ) ;
342- if ( this . connection . daplink ) {
343- await this . connection . daplink . connect ( ) ;
344- await this . startSerialInternal ( ) ;
345- }
260+ if ( this . addedListeners . serialdata ) {
261+ this . log ( "Reinstating serial after flash" ) ;
262+ if ( this . connection . daplink ) {
263+ await this . connection . daplink . connect ( ) ;
264+ await this . startSerialInternal ( ) ;
346265 }
347266 }
348267 }
@@ -408,7 +327,6 @@ class MicrobitWebUSBConnectionImpl
408327
409328 private setStatus ( newStatus : ConnectionStatus ) {
410329 this . status = newStatus ;
411- this . visibilityReconnect = false ;
412330 this . log ( "USB connection status " + newStatus ) ;
413331 this . dispatchTypedEvent ( "status" , new ConnectionStatusEvent ( newStatus ) ) ;
414332 }
0 commit comments