Skip to content

Commit 3d13ed2

Browse files
author
Akos Kitta
committed
fix: move out functions to module scope
1 parent 848c891 commit 3d13ed2

File tree

1 file changed

+80
-77
lines changed

1 file changed

+80
-77
lines changed

arduino-ide-extension/src/common/protocol/board-list.ts

Lines changed: 80 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,12 @@ export interface BoardList {
349349
/**
350350
* Index of the board+port item that is currently "selected". A board list item is selected, if matches the board+port combination of `boardsConfig`.
351351
*/
352-
get selectedIndex(): number;
352+
readonly selectedIndex: number;
353353

354354
/**
355355
* Contains all boards recognized from the detected port, and an optional unrecognized one that is derived from the detected port and the `initParam#selectedBoard`.
356356
*/
357-
get boards(): readonly (BoardListItemWithBoard | InferredBoardListItem)[];
357+
readonly boards: readonly (BoardListItemWithBoard | InferredBoardListItem)[];
358358

359359
/**
360360
* If `predicate` is not defined, no ports are filtered.
@@ -370,11 +370,6 @@ export interface BoardList {
370370
Record<'serial' | 'network' | string, ReturnType<BoardList['ports']>>
371371
>;
372372

373-
/**
374-
* What's the default action when the argument item is selected in the UI.
375-
*/
376-
// defaultAction(item: BoardListItemUI): BoardListItemAction;
377-
378373
/**
379374
* For dumping the current state of board list for debugging purposes.
380375
*/
@@ -413,87 +408,25 @@ export function createBoardList(
413408
items.sort(boardListItemComparator);
414409
const selectedIndex = findSelectedIndex(boardsConfig, items);
415410
const labels = createBoardListLabels(boardsConfig, items[selectedIndex]);
416-
417-
const length = items.length;
418-
let _allPorts: DetectedPort[] | undefined;
419-
const ports = (
420-
predicate: (detectedPort: DetectedPort) => boolean = () => true
421-
) => {
422-
if (!_allPorts) {
423-
_allPorts = [];
424-
// to keep the order or the detected ports
425-
const visitedPortKeys = new Set<string>();
426-
for (let i = 0; i < length; i++) {
427-
const { port } = items[i];
428-
const portKey = Port.keyOf(port);
429-
if (!visitedPortKeys.has(portKey)) {
430-
visitedPortKeys.add(portKey);
431-
const detectedPort = detectedPorts[portKey];
432-
if (detectedPort) {
433-
_allPorts.push(detectedPort);
434-
}
435-
}
436-
}
437-
}
438-
const ports = _allPorts.filter(predicate);
439-
const matchingIndex = findMatchingPortIndex(
440-
boardsConfig.selectedPort,
441-
ports
442-
);
443-
return Object.assign(ports, { matchingIndex });
444-
};
445-
446-
let _boards: (BoardListItemWithBoard | InferredBoardListItem)[] | undefined;
411+
const boards = collectBoards(items);
412+
const allPorts = collectPorts(items, detectedPorts);
447413
return {
448414
labels,
449415
items,
450416
boardsConfig,
451-
get selectedIndex() {
452-
return selectedIndex;
453-
},
454-
get boards() {
455-
if (!_boards) {
456-
_boards = [];
457-
for (let i = 0; i < length; i++) {
458-
const item = items[i];
459-
if (isInferredBoardListItem(item)) {
460-
_boards.push(item);
461-
} else if (item.board?.fqbn) {
462-
_boards.push(<Required<BoardListItem>>item);
463-
}
464-
}
465-
}
466-
return _boards;
467-
},
417+
boards,
418+
selectedIndex,
468419
ports(predicate?: (detectedPort: DetectedPort) => boolean) {
469-
return ports(predicate);
420+
return filterPorts(allPorts, boardsConfig.selectedPort, predicate);
470421
},
471422
portsGroupedByProtocol() {
472-
const result: Record<string, DetectedPort[] & { matchingIndex: number }> =
473-
{};
474-
const allPorts = ports();
475-
for (const detectedPort of allPorts) {
476-
const protocol = detectedPort.port.protocol;
477-
if (!result[protocol]) {
478-
result[protocol] = Object.assign([], {
479-
matchingIndex: -1,
480-
});
481-
}
482-
const portsOnProtocol = result[protocol];
483-
portsOnProtocol.push(detectedPort);
484-
}
485-
const matchItem = allPorts[allPorts.matchingIndex];
486-
// the cached match index is per all ports. Here, IDE2 needs to adjust the match index per grouped protocol
487-
if (matchItem) {
488-
const matchProtocol = matchItem.port.protocol;
489-
const matchPorts = result[matchProtocol];
490-
matchPorts.matchingIndex = matchPorts.indexOf(matchItem);
491-
}
492-
return result;
423+
const _allPorts = filterPorts(allPorts, boardsConfig.selectedPort);
424+
return portsGroupedByProtocol(_allPorts);
493425
},
494426
toString() {
495427
return JSON.stringify(
496428
{
429+
labels,
497430
detectedPorts,
498431
boardsConfig,
499432
items,
@@ -507,6 +440,76 @@ export function createBoardList(
507440
};
508441
}
509442

443+
function portsGroupedByProtocol(
444+
allPorts: ReturnType<BoardList['ports']>
445+
): ReturnType<BoardList['portsGroupedByProtocol']> {
446+
const result: Record<string, DetectedPort[] & { matchingIndex: number }> = {};
447+
for (const detectedPort of allPorts) {
448+
const protocol = detectedPort.port.protocol;
449+
if (!result[protocol]) {
450+
result[protocol] = Object.assign([], {
451+
matchingIndex: -1,
452+
});
453+
}
454+
const portsOnProtocol = result[protocol];
455+
portsOnProtocol.push(detectedPort);
456+
}
457+
const matchItem = allPorts[allPorts.matchingIndex];
458+
// the cached match index is per all ports. Here, IDE2 needs to adjust the match index per grouped protocol
459+
if (matchItem) {
460+
const matchProtocol = matchItem.port.protocol;
461+
const matchPorts = result[matchProtocol];
462+
matchPorts.matchingIndex = matchPorts.indexOf(matchItem);
463+
}
464+
return result;
465+
}
466+
467+
function filterPorts(
468+
allPorts: readonly DetectedPort[],
469+
selectedPort: PortIdentifier | undefined,
470+
predicate: (detectedPort: DetectedPort) => boolean = () => true
471+
): ReturnType<BoardList['ports']> {
472+
const ports = allPorts.filter(predicate);
473+
const matchingIndex = findMatchingPortIndex(selectedPort, ports);
474+
return Object.assign(ports, { matchingIndex });
475+
}
476+
477+
function collectPorts(
478+
items: readonly BoardListItem[],
479+
detectedPorts: DetectedPorts
480+
): DetectedPort[] {
481+
const allPorts: DetectedPort[] = [];
482+
// to keep the order or the detected ports
483+
const visitedPortKeys = new Set<string>();
484+
for (let i = 0; i < items.length; i++) {
485+
const { port } = items[i];
486+
const portKey = Port.keyOf(port);
487+
if (!visitedPortKeys.has(portKey)) {
488+
visitedPortKeys.add(portKey);
489+
const detectedPort = detectedPorts[portKey];
490+
if (detectedPort) {
491+
allPorts.push(detectedPort);
492+
}
493+
}
494+
}
495+
return allPorts;
496+
}
497+
498+
function collectBoards(
499+
items: readonly BoardListItem[]
500+
): readonly (BoardListItemWithBoard | InferredBoardListItem)[] {
501+
const boards: (BoardListItemWithBoard | InferredBoardListItem)[] = [];
502+
for (let i = 0; i < items.length; i++) {
503+
const item = items[i];
504+
if (isInferredBoardListItem(item)) {
505+
boards.push(item);
506+
} else if (item.board?.fqbn) {
507+
boards.push(<Required<BoardListItem>>item);
508+
}
509+
}
510+
return boards;
511+
}
512+
510513
function findSelectedIndex(
511514
boardsConfig: BoardsConfig,
512515
items: readonly BoardListItem[]

0 commit comments

Comments
 (0)