Cache attached boards

Signed-off-by: jbicker <jan.bicker@typefox.io>
This commit is contained in:
jbicker 2019-07-10 11:50:44 +02:00
parent a039597d40
commit 89fb2fddbd
6 changed files with 53 additions and 10 deletions

View File

@ -5,6 +5,7 @@ import { ArduinoCommands } from "./arduino-commands";
import { SketchesService, Sketch } from "../common/protocol/sketches-service"; import { SketchesService, Sketch } from "../common/protocol/sketches-service";
import { AWorkspaceService } from "./arduino-workspace-service"; import { AWorkspaceService } from "./arduino-workspace-service";
import { BoardsService, Board, AttachedSerialBoard, AttachedNetworkBoard } from "../common/protocol/boards-service"; import { BoardsService, Board, AttachedSerialBoard, AttachedNetworkBoard } from "../common/protocol/boards-service";
import { BoardFrontendService } from "./boards/board-frontend-service";
export namespace ArduinoToolbarContextMenu { export namespace ArduinoToolbarContextMenu {
export const OPEN_SKETCH_PATH: MenuPath = ['arduino-open-sketch-context-menu']; export const OPEN_SKETCH_PATH: MenuPath = ['arduino-open-sketch-context-menu'];
@ -29,6 +30,9 @@ export class ArduinoToolbarMenuContribution implements MenuContribution {
@inject(BoardsService) @inject(BoardsService)
protected readonly boardsService: BoardsService; protected readonly boardsService: BoardsService;
@inject(BoardFrontendService)
protected readonly boardFrontendService: BoardFrontendService;
constructor( constructor(
@inject(AWorkspaceService) protected readonly workspaceService: AWorkspaceService, @inject(AWorkspaceService) protected readonly workspaceService: AWorkspaceService,
@inject(MenuModelRegistry) protected readonly menuRegistry: MenuModelRegistry) { @inject(MenuModelRegistry) protected readonly menuRegistry: MenuModelRegistry) {
@ -41,7 +45,7 @@ export class ArduinoToolbarMenuContribution implements MenuContribution {
} }
protected async registerConnectedBoardsInMenu(registry: MenuModelRegistry) { protected async registerConnectedBoardsInMenu(registry: MenuModelRegistry) {
const { boards } = await this.boardsService.getAttachedBoards(); const boards = await this.boardFrontendService.getAttachedBoards();
const selectedBoard = await this.boardsService.getSelectBoard(); const selectedBoard = await this.boardsService.getSelectBoard();
const selectedPort = selectedBoard ? this.getPort(selectedBoard) : ''; const selectedPort = selectedBoard ? this.getPort(selectedBoard) : '';
boards.forEach(board => { boards.forEach(board => {

View File

@ -29,6 +29,7 @@ import { WindowService } from '@theia/core/lib/browser/window/window-service';
import { CommonCommands } from '@theia/core/lib/browser/common-frontend-contribution' import { CommonCommands } from '@theia/core/lib/browser/common-frontend-contribution'
import { BoardsToolBarItem } from './boards/boards-toolbar-item'; import { BoardsToolBarItem } from './boards/boards-toolbar-item';
import { SelectBoardsDialog } from './boards/select-board-dialog'; import { SelectBoardsDialog } from './boards/select-board-dialog';
import { BoardFrontendService } from './boards/board-frontend-service';
@injectable() @injectable()
export class ArduinoFrontendContribution implements TabBarToolbarContribution, CommandContribution { export class ArduinoFrontendContribution implements TabBarToolbarContribution, CommandContribution {
@ -39,6 +40,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
@inject(BoardsService) @inject(BoardsService)
protected readonly boardService: BoardsService; protected readonly boardService: BoardsService;
@inject(BoardFrontendService)
protected readonly boardFrontendService: BoardFrontendService;
@inject(CoreService) @inject(CoreService)
protected readonly coreService: CoreService; protected readonly coreService: CoreService;
@ -129,6 +133,7 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
ref={ref => this.boardsToolbarItem = ref} ref={ref => this.boardsToolbarItem = ref}
contextMenuRenderer={this.contextMenuRenderer} contextMenuRenderer={this.contextMenuRenderer}
boardsNotificationService={this.boardsNotificationService} boardsNotificationService={this.boardsNotificationService}
boardFrontendService={this.boardFrontendService}
boardService={this.boardService} />, boardService={this.boardService} />,
isVisible: widget => this.isArduinoToolbar(widget) isVisible: widget => this.isArduinoToolbar(widget)
}) })
@ -248,9 +253,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
} }
protected async selectBoard(board: Board) { protected async selectBoard(board: Board) {
const attached = await this.boardService.getAttachedBoards(); const boards = await this.boardFrontendService.getAttachedBoards();
if(attached.boards.length) { if (boards.length) {
board = attached.boards.find(b => b.name === board.name && b.fqbn === board.fqbn) || board; board = boards.find(b => b.name === board.name && b.fqbn === board.fqbn) || board;
} }
await this.boardService.selectBoard(board) await this.boardService.selectBoard(board)
if (this.boardsToolbarItem) { if (this.boardsToolbarItem) {

View File

@ -50,6 +50,7 @@ import { EditorWidgetFactory } from '@theia/editor/lib/browser/editor-widget-fac
import { CustomEditorWidgetFactory } from './customization/custom-editor-widget-factory'; import { CustomEditorWidgetFactory } from './customization/custom-editor-widget-factory';
import { SelectBoardsDialog, SelectBoardsDialogProps } from './boards/select-board-dialog'; import { SelectBoardsDialog, SelectBoardsDialogProps } from './boards/select-board-dialog';
import { SelectBoardDialogWidget } from './boards/select-board-dialog-widget'; import { SelectBoardDialogWidget } from './boards/select-board-dialog-widget';
import { BoardFrontendService } from './boards/board-frontend-service';
export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => { export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind) => {
// Commands and toolbar items // Commands and toolbar items
@ -86,6 +87,7 @@ export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Un
// Boards service // Boards service
bind(BoardsService).toDynamicValue(context => WebSocketConnectionProvider.createProxy(context.container, BoardsServicePath)).inSingletonScope(); bind(BoardsService).toDynamicValue(context => WebSocketConnectionProvider.createProxy(context.container, BoardsServicePath)).inSingletonScope();
bind(BoardFrontendService).toSelf().inSingletonScope();
// Boards list widget // Boards list widget
bind(BoardsListWidget).toSelf(); bind(BoardsListWidget).toSelf();

View File

@ -0,0 +1,22 @@
import { injectable, inject } from "inversify";
import { BoardsService, Board } from "../../common/protocol/boards-service";
@injectable()
export class BoardFrontendService {
@inject(BoardsService) protected readonly boardService: BoardsService;
protected attachedBoards: Board[];
async getAttachedBoards(): Promise<Board[]> {
if (this.attachedBoards) {
return this.attachedBoards;
}
await this.refreshAttachedBoards();
return this.attachedBoards;
}
async refreshAttachedBoards(): Promise<void> {
const { boards } = await this.boardService.getAttachedBoards();
this.attachedBoards = boards;
}
}

View File

@ -3,12 +3,14 @@ import { BoardsService, Board } from '../../common/protocol/boards-service';
import { ContextMenuRenderer } from '@theia/core/lib/browser'; import { ContextMenuRenderer } from '@theia/core/lib/browser';
import { ArduinoToolbarContextMenu } from '../arduino-file-menu'; import { ArduinoToolbarContextMenu } from '../arduino-file-menu';
import { BoardsNotificationService } from '../boards-notification-service'; import { BoardsNotificationService } from '../boards-notification-service';
import { BoardFrontendService } from './board-frontend-service';
export namespace BoardsToolBarItem { export namespace BoardsToolBarItem {
export interface Props { export interface Props {
readonly contextMenuRenderer: ContextMenuRenderer; readonly contextMenuRenderer: ContextMenuRenderer;
readonly boardsNotificationService: BoardsNotificationService; readonly boardsNotificationService: BoardsNotificationService;
readonly boardService: BoardsService; readonly boardService: BoardsService;
readonly boardFrontendService: BoardFrontendService;
} }
export interface State { export interface State {
@ -35,9 +37,9 @@ export class BoardsToolBarItem extends React.Component<BoardsToolBarItem.Props,
} }
protected async setAttachedBoards() { protected async setAttachedBoards() {
const { boards } = await this.props.boardService.getAttachedBoards(); const boards = await this.props.boardFrontendService.getAttachedBoards();
this.attachedBoards = boards; this.attachedBoards = boards;
if(this.attachedBoards.length){ if (this.attachedBoards.length) {
await this.props.boardService.selectBoard(this.attachedBoards[0]); await this.props.boardService.selectBoard(this.attachedBoards[0]);
this.setSelectedBoard(this.attachedBoards[0]); this.setSelectedBoard(this.attachedBoards[0]);
} }

View File

@ -4,6 +4,7 @@ import { injectable, inject } from 'inversify';
import { BoardsService, Board, BoardPackage, AttachedSerialBoard } from '../../common/protocol/boards-service'; import { BoardsService, Board, BoardPackage, AttachedSerialBoard } from '../../common/protocol/boards-service';
import { BoardsNotificationService } from '../boards-notification-service'; import { BoardsNotificationService } from '../boards-notification-service';
import { Emitter, Event } from '@theia/core'; import { Emitter, Event } from '@theia/core';
import { BoardFrontendService } from './board-frontend-service';
export interface BoardAndPortSelection { export interface BoardAndPortSelection {
board?: Board; board?: Board;
@ -93,6 +94,7 @@ export class BoardAndPortSelectionList extends React.Component<BoardAndPortSelec
export namespace BoardAndPortSelectionComponent { export namespace BoardAndPortSelectionComponent {
export interface Props { export interface Props {
boardsService: BoardsService; boardsService: BoardsService;
boardFrontendService: BoardFrontendService;
onSelect: (selection: BoardAndPortSelection) => void; onSelect: (selection: BoardAndPortSelection) => void;
} }
@ -131,7 +133,7 @@ export class BoardAndPortSelectionComponent extends React.Component<BoardAndPort
if (this.portListComponent) { if (this.portListComponent) {
this.portListComponent.reset(); this.portListComponent.reset();
} }
this.setState({selection: {}}); this.setState({ selection: {} });
} }
render(): React.ReactNode { render(): React.ReactNode {
@ -207,8 +209,8 @@ export class BoardAndPortSelectionComponent extends React.Component<BoardAndPort
protected async setPorts() { protected async setPorts() {
const ports: string[] = []; const ports: string[] = [];
const attached = await this.props.boardsService.getAttachedBoards(); const boards = await this.props.boardFrontendService.getAttachedBoards();
attached.boards.forEach(board => { boards.forEach(board => {
if (AttachedSerialBoard.is(board)) { if (AttachedSerialBoard.is(board)) {
ports.push(board.port); ports.push(board.port);
} }
@ -222,6 +224,8 @@ export class SelectBoardDialogWidget extends ReactWidget {
@inject(BoardsService) @inject(BoardsService)
protected readonly boardsService: BoardsService; protected readonly boardsService: BoardsService;
@inject(BoardFrontendService)
protected readonly boardFrontendService: BoardFrontendService;
@inject(BoardsNotificationService) @inject(BoardsNotificationService)
protected readonly boardsNotificationService: BoardsNotificationService; protected readonly boardsNotificationService: BoardsNotificationService;
@ -279,7 +283,11 @@ export class SelectBoardDialogWidget extends ReactWidget {
<p>but not to upload your sketch.</p> <p>but not to upload your sketch.</p>
</div> </div>
</div> </div>
<BoardAndPortSelectionComponent ref={ref => this.boardAndPortSelectionComponent = ref} boardsService={boardsService} onSelect={this.onSelect} /> <BoardAndPortSelectionComponent
ref={ref => this.boardAndPortSelectionComponent = ref}
boardFrontendService={this.boardFrontendService}
boardsService={boardsService}
onSelect={this.onSelect} />
</div> </div>
</React.Fragment> </React.Fragment>