@@ -23,6 +23,7 @@ import {
23
23
FlashDataSource ,
24
24
HexGenerationError ,
25
25
MicrobitWebUSBConnectionOptions ,
26
+ SerialOption ,
26
27
WebUSBError ,
27
28
} from "./device" ;
28
29
@@ -78,7 +79,11 @@ export class MicrobitWebUSBConnection
78
79
this . visibilityReconnect = false ;
79
80
if ( ! this . flashing ) {
80
81
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
+ } ) ;
82
87
}
83
88
}
84
89
} else {
@@ -113,7 +118,7 @@ export class MicrobitWebUSBConnection
113
118
setTimeout ( ( ) => {
114
119
if ( this . status === ConnectionStatus . CONNECTED ) {
115
120
this . unloading = false ;
116
- this . startSerialInternal ( ) ;
121
+ this . startSerialInternal ( SerialOption . NoReset ) ;
117
122
}
118
123
} , assumePageIsStayingOpenDelay ) ;
119
124
} ,
@@ -165,7 +170,9 @@ export class MicrobitWebUSBConnection
165
170
}
166
171
}
167
172
168
- async connect ( options : ConnectOptions = { } ) : Promise < ConnectionStatus > {
173
+ async connect (
174
+ options : ConnectOptions = { serial : SerialOption . Reset }
175
+ ) : Promise < ConnectionStatus > {
169
176
return this . withEnrichedErrors ( async ( ) => {
170
177
await this . connectInternal ( options ) ;
171
178
return this . status ;
@@ -217,7 +224,7 @@ export class MicrobitWebUSBConnection
217
224
await this . stopSerialInternal ( ) ;
218
225
this . log ( "Reconnecting before flash" ) ;
219
226
await this . connectInternal ( {
220
- serial : false ,
227
+ serial : SerialOption . None ,
221
228
} ) ;
222
229
if ( ! this . connection ) {
223
230
throw new Error ( "Must be connected now" ) ;
@@ -249,13 +256,13 @@ export class MicrobitWebUSBConnection
249
256
this . log ( "Reinstating serial after flash" ) ;
250
257
if ( this . connection . daplink ) {
251
258
await this . connection . daplink . connect ( ) ;
252
- await this . startSerialInternal ( ) ;
259
+ await this . startSerialInternal ( SerialOption . Reset ) ;
253
260
}
254
261
}
255
262
}
256
263
}
257
264
258
- private async startSerialInternal ( ) {
265
+ private async startSerialInternal ( option : SerialOption ) {
259
266
if ( ! this . connection ) {
260
267
// As connecting then starting serial are async we could disconnect between them,
261
268
// so handle this gracefully.
@@ -264,6 +271,12 @@ export class MicrobitWebUSBConnection
264
271
if ( this . serialReadInProgress ) {
265
272
await this . stopSerialInternal ( ) ;
266
273
}
274
+ if ( option === SerialOption . None || option === SerialOption . Reset ) {
275
+ this . emit ( EVENT_SERIAL_RESET , { } ) ;
276
+ }
277
+ if ( option === SerialOption . None ) {
278
+ return ;
279
+ }
267
280
// This is async but won't return until we stop serial so we error handle with an event.
268
281
this . serialReadInProgress = this . connection
269
282
. startSerial ( this . serialListener )
@@ -278,7 +291,6 @@ export class MicrobitWebUSBConnection
278
291
this . connection . stopSerial ( this . serialListener ) ;
279
292
await this . serialReadInProgress ;
280
293
this . serialReadInProgress = undefined ;
281
- this . emit ( EVENT_SERIAL_RESET , { } ) ;
282
294
}
283
295
}
284
296
@@ -384,9 +396,7 @@ export class MicrobitWebUSBConnection
384
396
this . connection = new DAPWrapper ( device , this . logging ) ;
385
397
}
386
398
await withTimeout ( this . connection . reconnectAsync ( ) , 10_000 ) ;
387
- if ( options . serial === undefined || options . serial ) {
388
- this . startSerialInternal ( ) ;
389
- }
399
+ this . startSerialInternal ( options . serial ) ;
390
400
this . setStatus ( ConnectionStatus . CONNECTED ) ;
391
401
}
392
402
0 commit comments