Skip to content

Commit 6e264a6

Browse files
clean up monitor mgr state
1 parent 99dfd4e commit 6e264a6

File tree

1 file changed

+62
-33
lines changed

1 file changed

+62
-33
lines changed

Diff for: arduino-ide-extension/src/node/monitor-manager.ts

+62-33
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111

1212
type MonitorID = string;
1313

14+
type UploadState = 'uploadInProgress' | 'pausedForUpload' | 'disposedForUpload';
15+
type MonitorIDsByUploadState = Record<UploadState, MonitorID[]>;
16+
1417
export const MonitorManagerName = 'monitor-manager';
1518

1619
@injectable()
@@ -23,15 +26,17 @@ export class MonitorManager extends CoreClientAware {
2326
// If either the board or port managed changes, a new service must
2427
// be started.
2528
private monitorServices = new Map<MonitorID, MonitorService>();
26-
private isUploadInProgress: boolean;
2729

28-
private servicesPausedForUpload: MonitorID[] = [];
29-
private servicesDisposedForUpload: MonitorID[] = [];
30+
private monitorIDsByUploadState: MonitorIDsByUploadState = {
31+
uploadInProgress: [],
32+
pausedForUpload: [],
33+
disposedForUpload: [],
34+
};
3035

31-
private startMonitorPendingRequests: [
32-
[Board, Port],
33-
(status: Status) => void
34-
][] = [];
36+
private startMonitor_PendingRequests: {
37+
requestFor: [Board, Port];
38+
postStartCallback: (status: Status) => void;
39+
}[] = [];
3540

3641
@inject(MonitorServiceFactory)
3742
private monitorServiceFactory: MonitorServiceFactory;
@@ -60,6 +65,25 @@ export class MonitorManager extends CoreClientAware {
6065
return false;
6166
}
6267

68+
uploadIsInProgress(): boolean {
69+
return this.monitorIDsByUploadState.uploadInProgress.length > 0;
70+
}
71+
72+
addToMonitorIDsByUploadState(state: UploadState, monitorID: string): void {
73+
this.monitorIDsByUploadState[state].push(monitorID);
74+
}
75+
76+
removeFromMonitorIDsByUploadState(
77+
state: UploadState,
78+
monitorID: string
79+
): void {
80+
this.monitorIDsByUploadState[state].filter((id) => id !== monitorID);
81+
}
82+
83+
monitorIDIsInUploadState(state: UploadState, monitorID: string): boolean {
84+
return this.monitorIDsByUploadState[state].includes(monitorID);
85+
}
86+
6387
/**
6488
* Start a pluggable monitor that receives and sends messages
6589
* to the specified board and port combination.
@@ -80,8 +104,11 @@ export class MonitorManager extends CoreClientAware {
80104
monitor = this.createMonitor(board, port);
81105
}
82106

83-
if (this.isUploadInProgress) {
84-
this.startMonitorPendingRequests.push([[board, port], postStartCallback]);
107+
if (this.uploadIsInProgress()) {
108+
this.startMonitor_PendingRequests.push({
109+
requestFor: [board, port],
110+
postStartCallback,
111+
});
85112
return;
86113
}
87114

@@ -130,21 +157,22 @@ export class MonitorManager extends CoreClientAware {
130157
* @param port port to monitor
131158
*/
132159
async notifyUploadStarted(board?: Board, port?: Port): Promise<void> {
133-
this.isUploadInProgress = true;
134-
135160
if (!board || !port) {
136161
// We have no way of knowing which monitor
137162
// to retrieve if we don't have this information.
138163
return;
139164
}
165+
140166
const monitorID = this.monitorID(board, port);
167+
this.addToMonitorIDsByUploadState('uploadInProgress', monitorID);
168+
141169
const monitor = this.monitorServices.get(monitorID);
142170
if (!monitor) {
143171
// There's no monitor running there, bail
144172
return;
145173
}
146174

147-
this.servicesPausedForUpload.push(monitorID);
175+
this.addToMonitorIDsByUploadState('pausedForUpload', monitorID);
148176
return monitor.pause();
149177
}
150178

@@ -157,33 +185,32 @@ export class MonitorManager extends CoreClientAware {
157185
* started or if there have been errors.
158186
*/
159187
async notifyUploadFinished(board?: Board, port?: Port): Promise<Status> {
160-
this.isUploadInProgress = false;
161188
let status: Status = Status.NOT_CONNECTED;
162189
let portDidChangeOnUpload = false;
163190

164191
// We have no way of knowing which monitor
165192
// to retrieve if we don't have this information.
166193
if (board && port) {
167194
const monitorID = this.monitorID(board, port);
195+
this.removeFromMonitorIDsByUploadState('uploadInProgress', monitorID);
196+
168197
const monitor = this.monitorServices.get(monitorID);
169198
if (monitor) {
170199
status = await monitor.start();
171200
}
172201

173-
// this monitorID will only be present in "servicesDisposedForUpload"
202+
// this monitorID will only be present in "disposedForUpload"
174203
// if the upload changed the board port
175-
portDidChangeOnUpload =
176-
this.servicesDisposedForUpload.includes(monitorID);
204+
portDidChangeOnUpload = this.monitorIDIsInUploadState(
205+
'disposedForUpload',
206+
monitorID
207+
);
177208
if (portDidChangeOnUpload) {
178-
this.servicesDisposedForUpload = this.servicesDisposedForUpload.filter(
179-
(id) => id !== monitorID
180-
);
209+
this.removeFromMonitorIDsByUploadState('disposedForUpload', monitorID);
181210
}
182211

183212
// in case a service was paused but not disposed
184-
this.servicesPausedForUpload = this.servicesPausedForUpload.filter(
185-
(id) => id !== monitorID
186-
);
213+
this.removeFromMonitorIDsByUploadState('pausedForUpload', monitorID);
187214
}
188215

189216
await this.startQueuedServices(portDidChangeOnUpload);
@@ -195,11 +222,14 @@ export class MonitorManager extends CoreClientAware {
195222
// will include a request for our "upload port', most likely at index 0.
196223
// We remove it, as this port was to be used exclusively for the upload
197224
const queued = portDidChangeOnUpload
198-
? this.startMonitorPendingRequests.slice(1)
199-
: this.startMonitorPendingRequests;
200-
this.startMonitorPendingRequests = [];
201-
202-
for (const [[board, port], onFinish] of queued) {
225+
? this.startMonitor_PendingRequests.slice(1)
226+
: this.startMonitor_PendingRequests;
227+
this.startMonitor_PendingRequests = [];
228+
229+
for (const {
230+
requestFor: [board, port],
231+
postStartCallback: onFinish,
232+
} of queued) {
203233
const boardsState = await this.boardsService.getState();
204234
const boardIsStillOnPort = Object.keys(boardsState)
205235
.map((connection: string) => {
@@ -281,13 +311,12 @@ export class MonitorManager extends CoreClientAware {
281311
// we paused it beforehand we know it was disposed
282312
// of because the upload changed the board port
283313
if (
284-
this.isUploadInProgress &&
285-
this.servicesPausedForUpload.includes(monitorID)
314+
this.uploadIsInProgress() &&
315+
this.monitorIDIsInUploadState('pausedForUpload', monitorID)
286316
) {
287-
this.servicesPausedForUpload = this.servicesPausedForUpload.filter(
288-
(id) => id !== monitorID
289-
);
290-
this.servicesDisposedForUpload.push(monitorID);
317+
this.removeFromMonitorIDsByUploadState('pausedForUpload', monitorID);
318+
319+
this.addToMonitorIDsByUploadState('disposedForUpload', monitorID);
291320
}
292321

293322
this.monitorServices.delete(monitorID);

0 commit comments

Comments
 (0)