@@ -26,6 +26,10 @@ import {
26
26
} from "./device.js" ;
27
27
import { TypedEventTarget } from "./events.js" ;
28
28
29
+ interface InternalConnectOptions extends ConnectOptions {
30
+ serial ?: boolean ;
31
+ }
32
+
29
33
// Temporary workaround for ChromeOS 105 bug.
30
34
// See https://bugs.chromium.org/p/chromium/issues/detail?id=1363712&q=usb&can=2
31
35
export const isChromeOS105 = ( ) : boolean => {
@@ -120,7 +124,9 @@ export class MicrobitWebUSBConnection
120
124
setTimeout ( ( ) => {
121
125
if ( this . status === ConnectionStatus . CONNECTED ) {
122
126
this . unloading = false ;
123
- this . startSerialInternal ( ) ;
127
+ if ( this . addedListeners . serialdata ) {
128
+ this . startSerialInternal ( ) ;
129
+ }
124
130
}
125
131
} , assumePageIsStayingOpenDelay ) ;
126
132
} ,
@@ -130,11 +136,26 @@ export class MicrobitWebUSBConnection
130
136
131
137
private logging : Logging ;
132
138
139
+ private _addEventListener = this . addEventListener ;
140
+ private _removeEventListener = this . removeEventListener ;
141
+
142
+ private addedListeners = {
143
+ serialdata : false ,
144
+ } ;
145
+
133
146
constructor (
134
147
options : MicrobitWebUSBConnectionOptions = { logging : new NullLogging ( ) } ,
135
148
) {
136
149
super ( ) ;
137
150
this . logging = options . logging ;
151
+ this . addEventListener = ( type , ...args ) => {
152
+ this . _addEventListener ( type , ...args ) ;
153
+ this . startNotifications ( type ) ;
154
+ } ;
155
+ this . removeEventListener = ( type , ...args ) => {
156
+ this . stopNotifications ( type ) ;
157
+ this . _removeEventListener ( type , ...args ) ;
158
+ } ;
138
159
}
139
160
140
161
private log ( v : any ) {
@@ -256,10 +277,12 @@ export class MicrobitWebUSBConnection
256
277
} else {
257
278
// This might not strictly be "reinstating". We should make this
258
279
// behaviour configurable when pulling out a library.
259
- this . log ( "Reinstating serial after flash" ) ;
260
- if ( this . connection . daplink ) {
261
- await this . connection . daplink . connect ( ) ;
262
- await this . startSerialInternal ( ) ;
280
+ if ( this . addedListeners . serialdata ) {
281
+ this . log ( "Reinstating serial after flash" ) ;
282
+ if ( this . connection . daplink ) {
283
+ await this . connection . daplink . connect ( ) ;
284
+ await this . startSerialInternal ( ) ;
285
+ }
263
286
}
264
287
}
265
288
}
@@ -387,13 +410,15 @@ export class MicrobitWebUSBConnection
387
410
this . setStatus ( ConnectionStatus . NO_AUTHORIZED_DEVICE ) ;
388
411
}
389
412
390
- private async connectInternal ( options : ConnectOptions ) : Promise < void > {
413
+ private async connectInternal (
414
+ options : InternalConnectOptions ,
415
+ ) : Promise < void > {
391
416
if ( ! this . connection ) {
392
417
const device = await this . chooseDevice ( ) ;
393
418
this . connection = new DAPWrapper ( device , this . logging ) ;
394
419
}
395
420
await withTimeout ( this . connection . reconnectAsync ( ) , 10_000 ) ;
396
- if ( options . serial === undefined || options . serial ) {
421
+ if ( this . addedListeners . serialdata && options . serial !== false ) {
397
422
this . startSerialInternal ( ) ;
398
423
}
399
424
this . setStatus ( ConnectionStatus . CONNECTED ) ;
@@ -410,6 +435,26 @@ export class MicrobitWebUSBConnection
410
435
this . dispatchTypedEvent ( "afterrequestdevice" , new AfterRequestDevice ( ) ) ;
411
436
return this . device ;
412
437
}
438
+
439
+ private async startNotifications ( type : string ) {
440
+ switch ( type as keyof DeviceConnectionEventMap ) {
441
+ case "serialdata" : {
442
+ this . startSerialInternal ( ) ;
443
+ this . addedListeners . serialdata = true ;
444
+ break ;
445
+ }
446
+ }
447
+ }
448
+
449
+ private async stopNotifications ( type : string ) {
450
+ switch ( type as keyof DeviceConnectionEventMap ) {
451
+ case "serialdata" : {
452
+ this . stopSerialInternal ( ) ;
453
+ this . addedListeners . serialdata = false ;
454
+ break ;
455
+ }
456
+ }
457
+ }
413
458
}
414
459
415
460
const genericErrorSuggestingReconnect = ( e : any ) =>
0 commit comments