Skip to content

Commit 2d5dff2

Browse files
dispose instead of pause on upld (code not final)
1 parent 1e7fbf1 commit 2d5dff2

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

arduino-ide-extension/src/node/core-service-impl.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,12 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
180180
req.getUserFieldsMap().set(e.name, e.value);
181181
});
182182

183+
let reconnectMonitorCallback;
183184
try {
184-
await this.monitorManager.notifyUploadStarted(board, port);
185+
reconnectMonitorCallback = await this.monitorManager.notifyUploadStarted(
186+
board,
187+
port
188+
);
185189

186190
const result = responseHandler(client, req);
187191

@@ -224,7 +228,11 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService {
224228
throw new Error(errorMessage);
225229
} finally {
226230
this.uploading = false;
227-
await this.monitorManager.notifyUploadFinished(board, port);
231+
await this.monitorManager.notifyUploadFinished(
232+
board,
233+
port,
234+
reconnectMonitorCallback
235+
);
228236
await this.monitorManager.startQueuedServices();
229237
}
230238
}

arduino-ide-extension/src/node/monitor-manager.ts

+32-13
Original file line numberDiff line numberDiff line change
@@ -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;

arduino-ide-extension/src/node/monitor-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ export class MonitorService extends CoreClientAware implements Disposable {
117117
return this.webSocketProvider.getAddress().port;
118118
}
119119

120-
dispose(): void {
121-
this.stop();
120+
async dispose(): Promise<void> {
121+
await this.stop();
122122
this.onDisposeEmitter.fire();
123123
this.onWSClientsNumberChanged?.dispose();
124124
}

0 commit comments

Comments
 (0)