@@ -23,6 +23,7 @@ import {
2323 FlashDataSource ,
2424 HexGenerationError ,
2525 MicrobitWebUSBConnectionOptions ,
26+ SerialOption ,
2627 WebUSBError ,
2728} from "./device" ;
2829
@@ -78,7 +79,11 @@ export class MicrobitWebUSBConnection
7879 this . visibilityReconnect = false ;
7980 if ( ! this . flashing ) {
8081 this . log ( "Reconnecting visible tab" ) ;
81- this . connect ( ) ;
82+ this . connect ( {
83+ // If any other connection status change occurs then visibilitReconnect is set to false, so
84+ // it's likely the same program at this point.
85+ serial : SerialOption . NoReset ,
86+ } ) ;
8287 }
8388 }
8489 } else {
@@ -113,7 +118,7 @@ export class MicrobitWebUSBConnection
113118 setTimeout ( ( ) => {
114119 if ( this . status === ConnectionStatus . CONNECTED ) {
115120 this . unloading = false ;
116- this . startSerialInternal ( ) ;
121+ this . startSerialInternal ( SerialOption . NoReset ) ;
117122 }
118123 } , assumePageIsStayingOpenDelay ) ;
119124 } ,
@@ -165,7 +170,9 @@ export class MicrobitWebUSBConnection
165170 }
166171 }
167172
168- async connect ( options : ConnectOptions = { } ) : Promise < ConnectionStatus > {
173+ async connect (
174+ options : ConnectOptions = { serial : SerialOption . Reset }
175+ ) : Promise < ConnectionStatus > {
169176 return this . withEnrichedErrors ( async ( ) => {
170177 await this . connectInternal ( options ) ;
171178 return this . status ;
@@ -217,7 +224,7 @@ export class MicrobitWebUSBConnection
217224 await this . stopSerialInternal ( ) ;
218225 this . log ( "Reconnecting before flash" ) ;
219226 await this . connectInternal ( {
220- serial : false ,
227+ serial : SerialOption . None ,
221228 } ) ;
222229 if ( ! this . connection ) {
223230 throw new Error ( "Must be connected now" ) ;
@@ -249,13 +256,13 @@ export class MicrobitWebUSBConnection
249256 this . log ( "Reinstating serial after flash" ) ;
250257 if ( this . connection . daplink ) {
251258 await this . connection . daplink . connect ( ) ;
252- await this . startSerialInternal ( ) ;
259+ await this . startSerialInternal ( SerialOption . Reset ) ;
253260 }
254261 }
255262 }
256263 }
257264
258- private async startSerialInternal ( ) {
265+ private async startSerialInternal ( option : SerialOption ) {
259266 if ( ! this . connection ) {
260267 // As connecting then starting serial are async we could disconnect between them,
261268 // so handle this gracefully.
@@ -264,6 +271,12 @@ export class MicrobitWebUSBConnection
264271 if ( this . serialReadInProgress ) {
265272 await this . stopSerialInternal ( ) ;
266273 }
274+ if ( option === SerialOption . None || option === SerialOption . Reset ) {
275+ this . emit ( EVENT_SERIAL_RESET , { } ) ;
276+ }
277+ if ( option === SerialOption . None ) {
278+ return ;
279+ }
267280 // This is async but won't return until we stop serial so we error handle with an event.
268281 this . serialReadInProgress = this . connection
269282 . startSerial ( this . serialListener )
@@ -278,7 +291,6 @@ export class MicrobitWebUSBConnection
278291 this . connection . stopSerial ( this . serialListener ) ;
279292 await this . serialReadInProgress ;
280293 this . serialReadInProgress = undefined ;
281- this . emit ( EVENT_SERIAL_RESET , { } ) ;
282294 }
283295 }
284296
@@ -384,9 +396,7 @@ export class MicrobitWebUSBConnection
384396 this . connection = new DAPWrapper ( device , this . logging ) ;
385397 }
386398 await withTimeout ( this . connection . reconnectAsync ( ) , 10_000 ) ;
387- if ( options . serial === undefined || options . serial ) {
388- this . startSerialInternal ( ) ;
389- }
399+ this . startSerialInternal ( options . serial ) ;
390400 this . setStatus ( ConnectionStatus . CONNECTED ) ;
391401 }
392402
0 commit comments