Skip to content

Commit 74e830d

Browse files
committed
Reworked function that groups ports by protocol
1 parent dc085a0 commit 74e830d

File tree

3 files changed

+60
-76
lines changed

3 files changed

+60
-76
lines changed

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

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,17 @@ PID: ${PID}`;
238238
}
239239

240240
// Installed ports
241-
const registerPorts = (ports: AvailablePorts) => {
241+
const registerPorts = (protocol: string, ports: AvailablePorts) => {
242242
const addresses = Object.keys(ports);
243243
if (!addresses.length) {
244244
return;
245245
}
246246

247247
// Register placeholder for protocol
248-
const [port] = ports[addresses[0]];
249-
const protocol = port.protocol;
250248
const menuPath = [...portsSubmenuPath, protocol];
251249
const placeholder = new PlaceholderMenuNode(
252250
menuPath,
253-
`${firstToUpperCase(port.protocol)} ports`
251+
`${firstToUpperCase(protocol)} ports`
254252
);
255253
this.menuModelRegistry.registerMenuNode(menuPath, placeholder);
256254
this.toDisposeBeforeMenuRebuild.push(
@@ -259,62 +257,57 @@ PID: ${PID}`;
259257
)
260258
);
261259

262-
for (const address of addresses) {
263-
if (!!ports[address]) {
264-
const [port, boards] = ports[address];
265-
if (!boards.length) {
266-
boards.push({
267-
name: '',
268-
});
269-
}
270-
for (const { name, fqbn } of boards) {
271-
const id = `arduino-select-port--${address}${fqbn ? `--${fqbn}` : ''
272-
}`;
273-
const command = { id };
274-
const handler = {
275-
execute: () => {
276-
if (
277-
!Port.equals(
278-
port,
279-
this.boardsServiceProvider.boardsConfig.selectedPort
280-
)
281-
) {
282-
this.boardsServiceProvider.boardsConfig = {
283-
selectedBoard:
284-
this.boardsServiceProvider.boardsConfig.selectedBoard,
285-
selectedPort: port,
286-
};
287-
}
288-
},
289-
isToggled: () =>
290-
Port.equals(
291-
port,
292-
this.boardsServiceProvider.boardsConfig.selectedPort
293-
),
294-
};
295-
const label = `${address}${name ? ` (${name})` : ''}`;
296-
const menuAction = {
297-
commandId: id,
298-
label,
299-
order: `1${label}`, // `1` comes after the placeholder which has order `0`
300-
};
301-
this.commandRegistry.registerCommand(command, handler);
302-
this.toDisposeBeforeMenuRebuild.push(
303-
Disposable.create(() =>
304-
this.commandRegistry.unregisterCommand(command)
305-
)
306-
);
307-
this.menuModelRegistry.registerMenuAction(menuPath, menuAction);
260+
for (const address of Object.keys(ports)) {
261+
const [port, boards] = ports[address];
262+
if (!boards.length) {
263+
boards.push({ name: '' });
264+
}
265+
for (const { name, fqbn } of boards) {
266+
const id = `arduino-select-port--${address}${fqbn ? `--${fqbn}` : ''}`;
267+
const command = { id };
268+
const handler = {
269+
execute: () => {
270+
if (!Port.equals(port, this.boardsServiceProvider.boardsConfig.selectedPort)) {
271+
this.boardsServiceProvider.boardsConfig = {
272+
selectedBoard:
273+
this.boardsServiceProvider.boardsConfig.selectedBoard,
274+
selectedPort: port,
275+
};
276+
}
277+
},
278+
isToggled: () => Port.equals(port, this.boardsServiceProvider.boardsConfig.selectedPort),
308279
}
280+
const label = `${address}${name ? ` (${name})` : ''}`;
281+
const menuAction = {
282+
commandId: id,
283+
label,
284+
order: `1${label}`, // `1` comes after the placeholder which has order `0`
285+
};
286+
this.commandRegistry.registerCommand(command, handler);
287+
this.toDisposeBeforeMenuRebuild.push(
288+
Disposable.create(() =>
289+
this.commandRegistry.unregisterCommand(command)
290+
)
291+
);
292+
this.menuModelRegistry.registerMenuAction(menuPath, menuAction);
309293
}
310294
}
311295
};
312296

313-
const { serial, network, unknown } =
314-
AvailablePorts.groupByProtocol(availablePorts);
315-
registerPorts(serial);
316-
registerPorts(network);
317-
registerPorts(unknown);
297+
const grouped = AvailablePorts.byProtocol(availablePorts);
298+
// We first show serial and network ports, then all the rest
299+
["serial", "network"].forEach(protocol => {
300+
const ports = grouped.get(protocol);
301+
if (ports) {
302+
registerPorts(protocol, ports);
303+
}
304+
});
305+
grouped.forEach((ports, protocol) => {
306+
if (protocol === "serial" || protocol === "network") {
307+
return;
308+
}
309+
registerPorts(protocol, ports);
310+
})
318311

319312
this.mainMenuManager.update();
320313
}

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)