Skip to content

Commit

Permalink
dispose instead of pause on upld (code not final)
Browse files Browse the repository at this point in the history
  • Loading branch information
davegarthsimpson committed Jun 15, 2022
1 parent 1e7fbf1 commit 2d5dff2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
12 changes: 10 additions & 2 deletions arduino-ide-extension/src/node/core-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}
}
Expand Down
45 changes: 32 additions & 13 deletions arduino-ide-extension/src/node/monitor-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MonitorID, MonitorService>();
private reconnectServiceCallbacks = new Map<
MonitorID,
(status: Status) => void
>();
private isUploadInProgress: boolean;

private startMonitorPendingRequests: [
Expand Down Expand Up @@ -84,6 +88,7 @@ export class MonitorManager extends CoreClientAware {

const result = await monitor.start();
postStartCallback(result);
this.reconnectServiceCallbacks.set(monitorID, postStartCallback);
}

/**
Expand Down Expand Up @@ -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<void> {
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();
}

/**
Expand All @@ -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<Status> {
async notifyUploadFinished(
board?: Board,
port?: Port,
postStartCallback?: (status: Status) => void
): Promise<void> {
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<void> {
Expand Down Expand Up @@ -252,6 +270,7 @@ export class MonitorManager extends CoreClientAware {
monitor.onDispose(
(() => {
this.monitorServices.delete(monitorID);
this.reconnectServiceCallbacks.delete(monitorID);
}).bind(this)
);
return monitor;
Expand Down
4 changes: 2 additions & 2 deletions arduino-ide-extension/src/node/monitor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export class MonitorService extends CoreClientAware implements Disposable {
return this.webSocketProvider.getAddress().port;
}

dispose(): void {
this.stop();
async dispose(): Promise<void> {
await this.stop();
this.onDisposeEmitter.fire();
this.onWSClientsNumberChanged?.dispose();
}
Expand Down

0 comments on commit 2d5dff2

Please sign in to comment.