@@ -23,6 +23,10 @@ export class MonitorManager extends CoreClientAware {
2323 // If either the board or port managed changes, a new service must
2424 // be started.
2525 private monitorServices = new Map < MonitorID , MonitorService > ( ) ;
26+ private reconnectServiceCallbacks = new Map <
27+ MonitorID ,
28+ ( status : Status ) => void
29+ > ( ) ;
2630 private isUploadInProgress : boolean ;
2731
2832 private startMonitorPendingRequests : [
@@ -84,6 +88,7 @@ export class MonitorManager extends CoreClientAware {
8488
8589 const result = await monitor . start ( ) ;
8690 postStartCallback ( result ) ;
91+ this . reconnectServiceCallbacks . set ( monitorID , postStartCallback ) ;
8792 }
8893
8994 /**
@@ -126,21 +131,29 @@ export class MonitorManager extends CoreClientAware {
126131 * @param board board connected to port
127132 * @param port port to monitor
128133 */
129- async notifyUploadStarted ( board ?: Board , port ?: Port ) : Promise < void > {
134+ async notifyUploadStarted (
135+ board ?: Board ,
136+ port ?: Port
137+ ) : Promise < ( ( status : Status ) => void ) | undefined > {
130138 this . isUploadInProgress = true ;
131139
132140 if ( ! board || ! port ) {
133141 // We have no way of knowing which monitor
134142 // to retrieve if we don't have this information.
135- return ;
143+ return undefined ;
136144 }
137145 const monitorID = this . monitorID ( board , port ) ;
138146 const monitor = this . monitorServices . get ( monitorID ) ;
139147 if ( ! monitor ) {
140148 // There's no monitor running there, bail
141- return ;
149+ return undefined ;
150+ }
151+
152+ const reconnectCallback = this . reconnectServiceCallbacks . get ( monitorID ) ;
153+ await monitor . dispose ( ) ;
154+ if ( reconnectCallback ) {
155+ return reconnectCallback ;
142156 }
143- return monitor . pause ( ) ;
144157 }
145158
146159 /**
@@ -151,22 +164,27 @@ export class MonitorManager extends CoreClientAware {
151164 * @returns a Status object to know if the process has been
152165 * started or if there have been errors.
153166 */
154- async notifyUploadFinished ( board ?: Board , port ?: Port ) : Promise < Status > {
167+ async notifyUploadFinished (
168+ board ?: Board ,
169+ port ?: Port ,
170+ postStartCallback ?: ( status : Status ) => void
171+ ) : Promise < void > {
155172 this . isUploadInProgress = false ;
156173
157174 if ( ! board || ! port ) {
158175 // We have no way of knowing which monitor
159176 // to retrieve if we don't have this information.
160- return Status . NOT_CONNECTED ;
161- }
162- const monitorID = this . monitorID ( board , port ) ;
163- const monitor = this . monitorServices . get ( monitorID ) ;
164- if ( ! monitor ) {
165- // There's no monitor running there, bail
166- return Status . NOT_CONNECTED ;
177+ return ;
167178 }
168179
169- return monitor . start ( ) ;
180+ const monitor = this . createMonitor ( board , port ) ;
181+ const restartServiceResult = await monitor . start ( ) ;
182+ if ( postStartCallback ) {
183+ postStartCallback ( restartServiceResult ) ;
184+
185+ const monitorID = this . monitorID ( board , port ) ;
186+ this . reconnectServiceCallbacks . set ( monitorID , postStartCallback ) ;
187+ }
170188 }
171189
172190 async startQueuedServices ( ) : Promise < void > {
@@ -252,6 +270,7 @@ export class MonitorManager extends CoreClientAware {
252270 monitor . onDispose (
253271 ( ( ) => {
254272 this . monitorServices . delete ( monitorID ) ;
273+ this . reconnectServiceCallbacks . delete ( monitorID ) ;
255274 } ) . bind ( this )
256275 ) ;
257276 return monitor ;
0 commit comments