Skip to content

Commit c8da135

Browse files
silvanocerzaAlberto Iannaccone
authored and
Alberto Iannaccone
committed
Reworked function that groups ports by protocol
1 parent 67b5fcb commit c8da135

File tree

3 files changed

+70
-76
lines changed

3 files changed

+70
-76
lines changed

arduino-ide-extension/src/browser/contributions/board-selection.ts

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,17 @@ PID: ${PID}`;
247247
}
248248

249249
// Installed ports
250-
const registerPorts = (ports: AvailablePorts) => {
250+
const registerPorts = (protocol: string, ports: AvailablePorts) => {
251251
const addresses = Object.keys(ports);
252252
if (!addresses.length) {
253253
return;
254254
}
255255

256256
// Register placeholder for protocol
257-
const [port] = ports[addresses[0]];
258-
const protocol = port.protocol;
259257
const menuPath = [...portsSubmenuPath, protocol];
260258
const placeholder = new PlaceholderMenuNode(
261259
menuPath,
262-
`${firstToUpperCase(port.protocol)} ports`
260+
`${firstToUpperCase(protocol)} ports`
263261
);
264262
this.menuModelRegistry.registerMenuNode(menuPath, placeholder);
265263
this.toDisposeBeforeMenuRebuild.push(
@@ -268,63 +266,68 @@ PID: ${PID}`;
268266
)
269267
);
270268

271-
for (const address of addresses) {
272-
if (!!ports[address]) {
273-
const [port, boards] = ports[address];
274-
if (!boards.length) {
275-
boards.push({
276-
name: '',
277-
});
278-
}
279-
for (const { name, fqbn } of boards) {
280-
const id = `arduino-select-port--${address}${
281-
fqbn ? `--${fqbn}` : ''
282-
}`;
283-
const command = { id };
284-
const handler = {
285-
execute: () => {
286-
if (
287-
!Port.equals(
288-
port,
289-
this.boardsServiceProvider.boardsConfig.selectedPort
290-
)
291-
) {
292-
this.boardsServiceProvider.boardsConfig = {
293-
selectedBoard:
294-
this.boardsServiceProvider.boardsConfig.selectedBoard,
295-
selectedPort: port,
296-
};
297-
}
298-
},
299-
isToggled: () =>
300-
Port.equals(
269+
for (const address of Object.keys(ports)) {
270+
const [port, boards] = ports[address];
271+
if (!boards.length) {
272+
boards.push({ name: '' });
273+
}
274+
for (const { name, fqbn } of boards) {
275+
const id = `arduino-select-port--${address}${
276+
fqbn ? `--${fqbn}` : ''
277+
}`;
278+
const command = { id };
279+
const handler = {
280+
execute: () => {
281+
if (
282+
!Port.equals(
301283
port,
302284
this.boardsServiceProvider.boardsConfig.selectedPort
303-
),
304-
};
305-
const label = `${address}${name ? ` (${name})` : ''}`;
306-
const menuAction = {
307-
commandId: id,
308-
label,
309-
order: `1${label}`, // `1` comes after the placeholder which has order `0`
310-
};
311-
this.commandRegistry.registerCommand(command, handler);
312-
this.toDisposeBeforeMenuRebuild.push(
313-
Disposable.create(() =>
314-
this.commandRegistry.unregisterCommand(command)
315-
)
316-
);
317-
this.menuModelRegistry.registerMenuAction(menuPath, menuAction);
318-
}
285+
)
286+
) {
287+
this.boardsServiceProvider.boardsConfig = {
288+
selectedBoard:
289+
this.boardsServiceProvider.boardsConfig.selectedBoard,
290+
selectedPort: port,
291+
};
292+
}
293+
},
294+
isToggled: () =>
295+
Port.equals(
296+
port,
297+
this.boardsServiceProvider.boardsConfig.selectedPort
298+
),
299+
};
300+
const label = `${address}${name ? ` (${name})` : ''}`;
301+
const menuAction = {
302+
commandId: id,
303+
label,
304+
order: `1${label}`, // `1` comes after the placeholder which has order `0`
305+
};
306+
this.commandRegistry.registerCommand(command, handler);
307+
this.toDisposeBeforeMenuRebuild.push(
308+
Disposable.create(() =>
309+
this.commandRegistry.unregisterCommand(command)
310+
)
311+
);
312+
this.menuModelRegistry.registerMenuAction(menuPath, menuAction);
319313
}
320314
}
321315
};
322316

323-
const { serial, network, unknown } =
324-
AvailablePorts.groupByProtocol(availablePorts);
325-
registerPorts(serial);
326-
registerPorts(network);
327-
registerPorts(unknown);
317+
const grouped = AvailablePorts.byProtocol(availablePorts);
318+
// We first show serial and network ports, then all the rest
319+
['serial', 'network'].forEach((protocol) => {
320+
const ports = grouped.get(protocol);
321+
if (ports) {
322+
registerPorts(protocol, ports);
323+
}
324+
});
325+
grouped.forEach((ports, protocol) => {
326+
if (protocol === 'serial' || protocol === 'network') {
327+
return;
328+
}
329+
registerPorts(protocol, ports);
330+
});
328331

329332
this.mainMenuManager.update();
330333
}

arduino-ide-extension/src/common/protocol/boards-service.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,18 @@ import { ArduinoComponent } from './arduino-component';
66

77
export type AvailablePorts = Record<string, [Port, Array<Board>]>;
88
export namespace AvailablePorts {
9-
export function groupByProtocol(availablePorts: AvailablePorts): {
10-
serial: AvailablePorts;
11-
network: AvailablePorts;
12-
unknown: AvailablePorts;
13-
} {
14-
const serial: AvailablePorts = {};
15-
const network: AvailablePorts = {};
16-
const unknown: AvailablePorts = {};
17-
for (const key of Object.keys(availablePorts)) {
18-
const [port, boards] = availablePorts[key];
19-
const { protocol } = port;
20-
if (protocol === 'serial') {
21-
serial[key] = [port, boards];
22-
} else if (protocol === 'network') {
23-
network[key] = [port, boards];
24-
} else {
25-
unknown[key] = [port, boards];
9+
export function byProtocol(availablePorts: AvailablePorts): Map<string, AvailablePorts> {
10+
const grouped = new Map<string, AvailablePorts>();
11+
for (const address of Object.keys(availablePorts)) {
12+
const [port, boards] = availablePorts[address];
13+
let ports = grouped.get(port.protocol);
14+
if (!ports) {
15+
ports = {} as AvailablePorts;
2616
}
17+
ports[address] = [port, boards];
18+
grouped.set(port.protocol, ports);
2719
}
28-
return { serial, network, unknown };
20+
return grouped;
2921
}
3022
}
3123

arduino-ide-extension/src/node/board-discovery.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class BoardDiscovery extends CoreClientAware {
108108
}
109109

110110
if (eventType === 'add') {
111-
if (newState[port.address] !== undefined) {
111+
if (newState[port.address]) {
112112
const [, knownBoards] = newState[port.address];
113113
console.warn(
114114
`Port '${port.address}' was already available. Known boards before override: ${JSON.stringify(
@@ -118,7 +118,7 @@ export class BoardDiscovery extends CoreClientAware {
118118
}
119119
newState[port.address] = [port, boards];
120120
} else if (eventType === 'remove') {
121-
if (newState[port.address] === undefined) {
121+
if (!newState[port.address]) {
122122
console.warn(`Port '${port.address}' was not available. Skipping`);
123123
return;
124124
}
@@ -159,7 +159,6 @@ export class BoardDiscovery extends CoreClientAware {
159159
getAvailablePorts(state: AvailablePorts = this.state): Port[] {
160160
const availablePorts: Port[] = [];
161161
for (const address of Object.keys(state)) {
162-
// tslint:disable-next-line: whitespace
163162
const [port] = state[address];
164163
availablePorts.push(port);
165164
}

0 commit comments

Comments
 (0)