@@ -203,21 +203,33 @@ export class MicrobitWebUSBConnection
203
203
this . emit ( EVENT_SERIAL_DATA , data ) ;
204
204
} ;
205
205
206
+ private flashing : boolean = false ;
207
+ private disconnectAfterFlash : boolean = false ;
206
208
private visibilityReconnect : boolean = false ;
207
209
private visibilityChangeListener = ( ) => {
208
210
if ( document . visibilityState === "visible" ) {
209
211
if (
210
212
this . visibilityReconnect &&
211
213
this . status !== ConnectionStatus . CONNECTED
212
214
) {
215
+ this . disconnectAfterFlash = false ;
213
216
this . visibilityReconnect = false ;
214
- this . connect ( ) ;
217
+ if ( ! this . flashing ) {
218
+ this . log ( "Reconnecting visible tab" ) ;
219
+ this . connect ( ) ;
220
+ }
215
221
}
216
222
} else {
217
223
if ( ! this . unloading && this . status === ConnectionStatus . CONNECTED ) {
218
- this . disconnect ( ) . then ( ( ) => {
219
- this . visibilityReconnect = true ;
220
- } ) ;
224
+ if ( ! this . flashing ) {
225
+ this . log ( "Disconnecting hidden tab" ) ;
226
+ this . disconnect ( ) . then ( ( ) => {
227
+ this . visibilityReconnect = true ;
228
+ } ) ;
229
+ } else {
230
+ this . log ( "Scheduling disconnect of hidden tab for after flash" ) ;
231
+ this . disconnectAfterFlash = true ;
232
+ }
221
233
}
222
234
}
223
235
} ;
@@ -311,20 +323,25 @@ export class MicrobitWebUSBConnection
311
323
progress : ( percentage : number | undefined ) => void ;
312
324
}
313
325
) : Promise < void > {
314
- const startTime = new Date ( ) . getTime ( ) ;
326
+ this . flashing = true ;
327
+ try {
328
+ const startTime = new Date ( ) . getTime ( ) ;
315
329
316
- await this . withEnrichedErrors ( ( ) =>
317
- this . flashInternal ( dataSource , options )
318
- ) ;
330
+ await this . withEnrichedErrors ( ( ) =>
331
+ this . flashInternal ( dataSource , options )
332
+ ) ;
319
333
320
- const flashTime = new Date ( ) . getTime ( ) - startTime ;
321
- this . logging . event ( {
322
- type : "WebUSB-time" ,
323
- detail : {
324
- flashTime,
325
- } ,
326
- } ) ;
327
- this . logging . log ( "Flash complete" ) ;
334
+ const flashTime = new Date ( ) . getTime ( ) - startTime ;
335
+ this . logging . event ( {
336
+ type : "WebUSB-time" ,
337
+ detail : {
338
+ flashTime,
339
+ } ,
340
+ } ) ;
341
+ this . logging . log ( "Flash complete" ) ;
342
+ } finally {
343
+ this . flashing = false ;
344
+ }
328
345
}
329
346
330
347
private async flashInternal (
@@ -356,12 +373,19 @@ export class MicrobitWebUSBConnection
356
373
} finally {
357
374
progress ( undefined ) ;
358
375
359
- // This might not strictly be "reinstating". We should make this
360
- // behaviour configurable when pulling out a library.
361
- this . log ( "Reinstating serial after flash" ) ;
362
- if ( this . connection . daplink ) {
363
- await this . connection . daplink . connect ( ) ;
364
- await this . startSerialInternal ( ) ;
376
+ if ( this . disconnectAfterFlash ) {
377
+ this . log ( "Disconnecting after flash due to tab visibility" ) ;
378
+ this . disconnectAfterFlash = false ;
379
+ await this . disconnect ( ) ;
380
+ this . visibilityReconnect = true ;
381
+ } else {
382
+ // This might not strictly be "reinstating". We should make this
383
+ // behaviour configurable when pulling out a library.
384
+ this . log ( "Reinstating serial after flash" ) ;
385
+ if ( this . connection . daplink ) {
386
+ await this . connection . daplink . connect ( ) ;
387
+ await this . startSerialInternal ( ) ;
388
+ }
365
389
}
366
390
}
367
391
}
0 commit comments