@@ -45,6 +45,8 @@ export class MicrobitRadioBridgeConnection
45
45
private logging : Logging ;
46
46
private serialSession : RadioBridgeSerialSession | undefined ;
47
47
private remoteDeviceId : number | undefined ;
48
+ private disconnectPromise : Promise < void > | undefined ;
49
+ private serialSessionOpen = false ;
48
50
49
51
private delegateStatusListner = ( e : ConnectionStatusEvent ) => {
50
52
const currentStatus = this . status ;
@@ -53,7 +55,10 @@ export class MicrobitRadioBridgeConnection
53
55
this . serialSession ?. dispose ( ) ;
54
56
} else {
55
57
this . status = ConnectionStatus . NOT_CONNECTED ;
56
- if ( currentStatus === ConnectionStatus . NOT_CONNECTED ) {
58
+ if (
59
+ currentStatus === ConnectionStatus . NOT_CONNECTED &&
60
+ this . serialSessionOpen
61
+ ) {
57
62
this . serialSession ?. connect ( ) ;
58
63
}
59
64
}
@@ -96,6 +101,9 @@ export class MicrobitRadioBridgeConnection
96
101
}
97
102
98
103
async connect ( ) : Promise < ConnectionStatus > {
104
+ if ( this . disconnectPromise ) {
105
+ await this . disconnectPromise ;
106
+ }
99
107
// TODO: previously this skipped overlapping connect attempts but that seems awkward
100
108
// can we... just not do that? or wait?
101
109
@@ -108,7 +116,10 @@ export class MicrobitRadioBridgeConnection
108
116
message : "Serial connect start" ,
109
117
} ) ;
110
118
111
- await this . delegate . connect ( ) ;
119
+ if ( this . delegate . status !== ConnectionStatus . CONNECTED ) {
120
+ await this . delegate . connect ( ) ;
121
+ }
122
+
112
123
try {
113
124
this . serialSession = new RadioBridgeSerialSession (
114
125
this . logging ,
@@ -137,6 +148,7 @@ export class MicrobitRadioBridgeConnection
137
148
) ;
138
149
139
150
await this . serialSession . connect ( ) ;
151
+ this . serialSessionOpen = true ;
140
152
141
153
this . logging . event ( {
142
154
type : "Connect" ,
@@ -154,7 +166,14 @@ export class MicrobitRadioBridgeConnection
154
166
}
155
167
156
168
async disconnect ( ) : Promise < void > {
157
- await this . serialSession ?. dispose ( ) ;
169
+ if ( this . disconnectPromise ) {
170
+ return this . disconnectPromise ;
171
+ }
172
+ this . disconnectPromise = ( async ( ) => {
173
+ this . serialSessionOpen = false ;
174
+ await this . serialSession ?. dispose ( ) ;
175
+ this . disconnectPromise = undefined ;
176
+ } ) ( ) ;
158
177
}
159
178
160
179
private setStatus ( status : ConnectionStatus ) {
@@ -260,7 +279,6 @@ class RadioBridgeSerialSession {
260
279
this . delegate . addEventListener ( "serialerror" , this . serialErrorListener ) ;
261
280
262
281
try {
263
- await this . delegate . softwareReset ( ) ;
264
282
await this . handshake ( ) ;
265
283
this . onStatusChanged ( ConnectionStatus . CONNECTED ) ;
266
284
@@ -318,6 +336,7 @@ class RadioBridgeSerialSession {
318
336
this . responseMap . clear ( ) ;
319
337
this . delegate . removeEventListener ( "serialdata" , this . serialDataListener ) ;
320
338
this . delegate . removeEventListener ( "serialerror" , this . serialErrorListener ) ;
339
+ await this . delegate . softwareReset ( ) ;
321
340
322
341
this . onStatusChanged ( ConnectionStatus . NOT_CONNECTED ) ;
323
342
}
0 commit comments