From 2d5dff2a2d85754467470b5973b7ecac8017aa58 Mon Sep 17 00:00:00 2001 From: David Simpson <45690499+davegarthsimpson@users.noreply.github.com> Date: Wed, 15 Jun 2022 19:38:59 +0200 Subject: [PATCH] dispose instead of pause on upld (code not final) --- .../src/node/core-service-impl.ts | 12 ++++- .../src/node/monitor-manager.ts | 45 +++++++++++++------ .../src/node/monitor-service.ts | 4 +- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/arduino-ide-extension/src/node/core-service-impl.ts b/arduino-ide-extension/src/node/core-service-impl.ts index 14b6d3007..f8df42fdb 100644 --- a/arduino-ide-extension/src/node/core-service-impl.ts +++ b/arduino-ide-extension/src/node/core-service-impl.ts @@ -180,8 +180,12 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService { req.getUserFieldsMap().set(e.name, e.value); }); + let reconnectMonitorCallback; try { - await this.monitorManager.notifyUploadStarted(board, port); + reconnectMonitorCallback = await this.monitorManager.notifyUploadStarted( + board, + port + ); const result = responseHandler(client, req); @@ -224,7 +228,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService { throw new Error(errorMessage); } finally { this.uploading = false; - await this.monitorManager.notifyUploadFinished(board, port); + await this.monitorManager.notifyUploadFinished( + board, + port, + reconnectMonitorCallback + ); await this.monitorManager.startQueuedServices(); } } diff --git a/arduino-ide-extension/src/node/monitor-manager.ts b/arduino-ide-extension/src/node/monitor-manager.ts index 4adad2881..3636ae5b0 100644 --- a/arduino-ide-extension/src/node/monitor-manager.ts +++ b/arduino-ide-extension/src/node/monitor-manager.ts @@ -23,6 +23,10 @@ export class MonitorManager extends CoreClientAware { // If either the board or port managed changes, a new service must // be started. private monitorServices = new Map(); + private reconnectServiceCallbacks = new Map< + MonitorID, + (status: Status) => void + >(); private isUploadInProgress: boolean; private startMonitorPendingRequests: [ @@ -84,6 +88,7 @@ export class MonitorManager extends CoreClientAware { const result = await monitor.start(); postStartCallback(result); + this.reconnectServiceCallbacks.set(monitorID, postStartCallback); } /** @@ -126,21 +131,29 @@ export class MonitorManager extends CoreClientAware { * @param board board connected to port * @param port port to monitor */ - async notifyUploadStarted(board?: Board, port?: Port): Promise { + async notifyUploadStarted( + board?: Board, + port?: Port + ): Promise<((status: Status) => void) | undefined> { this.isUploadInProgress = true; if (!board || !port) { // We have no way of knowing which monitor // to retrieve if we don't have this information. - return; + return undefined; } const monitorID = this.monitorID(board, port); const monitor = this.monitorServices.get(monitorID); if (!monitor) { // There's no monitor running there, bail - return; + return undefined; + } + + const reconnectCallback = this.reconnectServiceCallbacks.get(monitorID); + await monitor.dispose(); + if (reconnectCallback) { + return reconnectCallback; } - return monitor.pause(); } /** @@ -151,22 +164,27 @@ export class MonitorManager extends CoreClientAware { * @returns a Status object to know if the process has been * started or if there have been errors. */ - async notifyUploadFinished(board?: Board, port?: Port): Promise { + async notifyUploadFinished( + board?: Board, + port?: Port, + postStartCallback?: (status: Status) => void + ): Promise { this.isUploadInProgress = false; if (!board || !port) { // We have no way of knowing which monitor // to retrieve if we don't have this information. - return Status.NOT_CONNECTED; - } - const monitorID = this.monitorID(board, port); - const monitor = this.monitorServices.get(monitorID); - if (!monitor) { - // There's no monitor running there, bail - return Status.NOT_CONNECTED; + return; } - return monitor.start(); + const monitor = this.createMonitor(board, port); + const restartServiceResult = await monitor.start(); + if (postStartCallback) { + postStartCallback(restartServiceResult); + + const monitorID = this.monitorID(board, port); + this.reconnectServiceCallbacks.set(monitorID, postStartCallback); + } } async startQueuedServices(): Promise { @@ -252,6 +270,7 @@ export class MonitorManager extends CoreClientAware { monitor.onDispose( (() => { this.monitorServices.delete(monitorID); + this.reconnectServiceCallbacks.delete(monitorID); }).bind(this) ); return monitor; diff --git a/arduino-ide-extension/src/node/monitor-service.ts b/arduino-ide-extension/src/node/monitor-service.ts index a0ab86f6f..774ae5d2f 100644 --- a/arduino-ide-extension/src/node/monitor-service.ts +++ b/arduino-ide-extension/src/node/monitor-service.ts @@ -117,8 +117,8 @@ export class MonitorService extends CoreClientAware implements Disposable { return this.webSocketProvider.getAddress().port; } - dispose(): void { - this.stop(); + async dispose(): Promise { + await this.stop(); this.onDisposeEmitter.fire(); this.onWSClientsNumberChanged?.dispose(); }