@@ -25,6 +25,9 @@ export class MonitorManager extends CoreClientAware {
25
25
private monitorServices = new Map < MonitorID , MonitorService > ( ) ;
26
26
private isUploadInProgress : boolean ;
27
27
28
+ private servicesPausedForUpload : MonitorID [ ] = [ ] ;
29
+ private servicesDisposedForUpload : MonitorID [ ] = [ ] ;
30
+
28
31
private startMonitorPendingRequests : [
29
32
[ Board , Port ] ,
30
33
( status : Status ) => void
@@ -140,6 +143,8 @@ export class MonitorManager extends CoreClientAware {
140
143
// There's no monitor running there, bail
141
144
return ;
142
145
}
146
+
147
+ this . servicesPausedForUpload . push ( monitorID ) ;
143
148
return monitor . pause ( ) ;
144
149
}
145
150
@@ -154,35 +159,41 @@ export class MonitorManager extends CoreClientAware {
154
159
async notifyUploadFinished ( board ?: Board , port ?: Port ) : Promise < Status > {
155
160
this . isUploadInProgress = false ;
156
161
let status : Status = Status . NOT_CONNECTED ;
157
- let monitorID ;
162
+ let portDidChangeOnUpload = false ;
163
+
158
164
// We have no way of knowing which monitor
159
165
// to retrieve if we don't have this information.
160
166
if ( board && port ) {
161
- monitorID = this . monitorID ( board , port ) ;
167
+ const monitorID = this . monitorID ( board , port ) ;
162
168
const monitor = this . monitorServices . get ( monitorID ) ;
163
- // There's no monitor running there, bail
164
169
if ( monitor ) {
165
170
status = await monitor . start ( ) ;
166
171
}
172
+
173
+ // this monitorID will only be present in "servicesDisposedForUpload"
174
+ // if the upload changed the board port
175
+ portDidChangeOnUpload =
176
+ this . servicesDisposedForUpload . includes ( monitorID ) ;
177
+ if ( portDidChangeOnUpload ) {
178
+ this . servicesDisposedForUpload = this . servicesDisposedForUpload . filter (
179
+ ( id ) => id !== monitorID
180
+ ) ;
181
+ }
182
+
183
+ // in case a service was paused but not disposed
184
+ this . servicesPausedForUpload = this . servicesPausedForUpload . filter (
185
+ ( id ) => id !== monitorID
186
+ ) ;
167
187
}
168
- await this . startQueuedServices ( monitorID ) ;
188
+
189
+ await this . startQueuedServices ( portDidChangeOnUpload ) ;
169
190
return status ;
170
191
}
171
192
172
- async startQueuedServices (
173
- monitorIDRelatedToUploadBoard ?: string // when upload initially requested
174
- ) : Promise < void > {
175
- let portDidChangeOnUpload = false ;
176
- if ( monitorIDRelatedToUploadBoard ) {
177
- portDidChangeOnUpload = ! [ ...this . monitorServices . keys ( ) ] . includes (
178
- monitorIDRelatedToUploadBoard
179
- ) ;
180
- }
181
- // if we no longer have a monitor service for the "board, port"
182
- // combination with which we called "notifyUploadStarted", we know
183
- // the port changed during upload, hence in our "startMonitorPendingRequests"
184
- // we'll have our "upload port', most likely at index 0.
185
- // We remove it, as there will no longer be a board using this "upload port"
193
+ async startQueuedServices ( portDidChangeOnUpload : boolean ) : Promise < void > {
194
+ // if the port changed during upload with the monitor open, "startMonitorPendingRequests"
195
+ // will include a request for our "upload port', most likely at index 0.
196
+ // We remove it, as this port was to be used exclusively for the upload
186
197
const queued = portDidChangeOnUpload
187
198
? this . startMonitorPendingRequests . slice ( 1 )
188
199
: this . startMonitorPendingRequests ;
@@ -266,6 +277,19 @@ export class MonitorManager extends CoreClientAware {
266
277
this . monitorServices . set ( monitorID , monitor ) ;
267
278
monitor . onDispose (
268
279
( ( ) => {
280
+ // if a service is disposed during upload and
281
+ // we paused it beforehand we know it was disposed
282
+ // of because the upload changed the board port
283
+ if (
284
+ this . isUploadInProgress &&
285
+ this . servicesPausedForUpload . includes ( monitorID )
286
+ ) {
287
+ this . servicesPausedForUpload = this . servicesPausedForUpload . filter (
288
+ ( id ) => id !== monitorID
289
+ ) ;
290
+ this . servicesDisposedForUpload . push ( monitorID ) ;
291
+ }
292
+
269
293
this . monitorServices . delete ( monitorID ) ;
270
294
} ) . bind ( this )
271
295
) ;
0 commit comments