@@ -25,6 +25,9 @@ export class MonitorManager extends CoreClientAware {
2525 private monitorServices = new Map < MonitorID , MonitorService > ( ) ;
2626 private isUploadInProgress : boolean ;
2727
28+ private servicesPausedForUpload : MonitorID [ ] = [ ] ;
29+ private servicesDisposedForUpload : MonitorID [ ] = [ ] ;
30+
2831 private startMonitorPendingRequests : [
2932 [ Board , Port ] ,
3033 ( status : Status ) => void
@@ -140,6 +143,8 @@ export class MonitorManager extends CoreClientAware {
140143 // There's no monitor running there, bail
141144 return ;
142145 }
146+
147+ this . servicesPausedForUpload . push ( monitorID ) ;
143148 return monitor . pause ( ) ;
144149 }
145150
@@ -154,35 +159,41 @@ export class MonitorManager extends CoreClientAware {
154159 async notifyUploadFinished ( board ?: Board , port ?: Port ) : Promise < Status > {
155160 this . isUploadInProgress = false ;
156161 let status : Status = Status . NOT_CONNECTED ;
157- let monitorID ;
162+ let portDidChangeOnUpload = false ;
163+
158164 // We have no way of knowing which monitor
159165 // to retrieve if we don't have this information.
160166 if ( board && port ) {
161- monitorID = this . monitorID ( board , port ) ;
167+ const monitorID = this . monitorID ( board , port ) ;
162168 const monitor = this . monitorServices . get ( monitorID ) ;
163- // There's no monitor running there, bail
164169 if ( monitor ) {
165170 status = await monitor . start ( ) ;
166171 }
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+ ) ;
167187 }
168- await this . startQueuedServices ( monitorID ) ;
188+
189+ await this . startQueuedServices ( portDidChangeOnUpload ) ;
169190 return status ;
170191 }
171192
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
186197 const queued = portDidChangeOnUpload
187198 ? this . startMonitorPendingRequests . slice ( 1 )
188199 : this . startMonitorPendingRequests ;
@@ -266,6 +277,19 @@ export class MonitorManager extends CoreClientAware {
266277 this . monitorServices . set ( monitorID , monitor ) ;
267278 monitor . onDispose (
268279 ( ( ) => {
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+
269293 this . monitorServices . delete ( monitorID ) ;
270294 } ) . bind ( this )
271295 ) ;
0 commit comments