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 { AWorkspaceService } from "./arduino-workspace-service";
import { BoardsService, Board, AttachedSerialBoard, AttachedNetworkBoard } from "../common/protocol/boards-service";
import { BoardFrontendService } from "./boards/board-frontend-service";
export namespace ArduinoToolbarContextMenu {
export const OPEN_SKETCH_PATH: MenuPath = ['arduino-open-sketch-context-menu'];
@ -29,6 +30,9 @@ export class ArduinoToolbarMenuContribution implements MenuContribution {
@inject(BoardsService)
protected readonly boardsService: BoardsService;
@inject(BoardFrontendService)
protected readonly boardFrontendService: BoardFrontendService;
constructor(
@inject(AWorkspaceService) protected readonly workspaceService: AWorkspaceService,
@inject(MenuModelRegistry) protected readonly menuRegistry: MenuModelRegistry) {
@ -41,7 +45,7 @@ export class ArduinoToolbarMenuContribution implements MenuContribution {
}
protected async registerConnectedBoardsInMenu(registry: MenuModelRegistry) {
const { boards } = await this.boardsService.getAttachedBoards();
const boards = await this.boardFrontendService.getAttachedBoards();
const selectedBoard = await this.boardsService.getSelectBoard();
const selectedPort = selectedBoard ? this.getPort(selectedBoard) : '';
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 { BoardsToolBarItem } from './boards/boards-toolbar-item';
import { SelectBoardsDialog } from './boards/select-board-dialog';
import { BoardFrontendService } from './boards/board-frontend-service';
@injectable()
export class ArduinoFrontendContribution implements TabBarToolbarContribution, CommandContribution {
@ -39,6 +40,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
@inject(BoardsService)
protected readonly boardService: BoardsService;
@inject(BoardFrontendService)
protected readonly boardFrontendService: BoardFrontendService;
@inject(CoreService)
protected readonly coreService: CoreService;
@ -129,6 +133,7 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
ref={ref => this.boardsToolbarItem = ref}
contextMenuRenderer={this.contextMenuRenderer}
boardsNotificationService={this.boardsNotificationService}
boardFrontendService={this.boardFrontendService}
boardService={this.boardService} />,
isVisible: widget => this.isArduinoToolbar(widget)
})
@ -248,9 +253,9 @@ export class ArduinoFrontendContribution implements TabBarToolbarContribution, C
}
protected async selectBoard(board: Board) {
const attached = await this.boardService.getAttachedBoards();
if(attached.boards.length) {
board = attached.boards.find(b => b.name === board.name && b.fqbn === board.fqbn) || board;
const boards = await this.boardFrontendService.getAttachedBoards();
if (boards.length) {
board = boards.find(b => b.name === board.name && b.fqbn === board.fqbn) || board;
}
await this.boardService.selectBoard(board)
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 { SelectBoardsDialog, SelectBoardsDialogProps } from './boards/select-board-dialog';
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) => {
// Commands and toolbar items
@ -86,6 +87,7 @@ export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Un
// Boards service
bind(BoardsService).toDynamicValue(context => WebSocketConnectionProvider.createProxy(context.container, BoardsServicePath)).inSingletonScope();
bind(BoardFrontendService).toSelf().inSingletonScope();
// Boards list widget
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 { ArduinoToolbarContextMenu } from '../arduino-file-menu';
import { BoardsNotificationService } from '../boards-notification-service';
import { BoardFrontendService } from './board-frontend-service';
export namespace BoardsToolBarItem {
export interface Props {
readonly contextMenuRenderer: ContextMenuRenderer;
readonly boardsNotificationService: BoardsNotificationService;
readonly boardService: BoardsService;
readonly boardFrontendService: BoardFrontendService;
}
export interface State {
@ -35,9 +37,9 @@ export class BoardsToolBarItem extends React.Component<BoardsToolBarItem.Props,
}
protected async setAttachedBoards() {
const { boards } = await this.props.boardService.getAttachedBoards();
const boards = await this.props.boardFrontendService.getAttachedBoards();
this.attachedBoards = boards;
if(this.attachedBoards.length){
if (this.attachedBoards.length) {
await this.props.boardService.selectBoard(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 { BoardsNotificationService } from '../boards-notification-service';
import { Emitter, Event } from '@theia/core';
import { BoardFrontendService } from './board-frontend-service';
export interface BoardAndPortSelection {
board?: Board;
@ -93,6 +94,7 @@ export class BoardAndPortSelectionList extends React.Component<BoardAndPortSelec
export namespace BoardAndPortSelectionComponent {
export interface Props {
boardsService: BoardsService;
boardFrontendService: BoardFrontendService;
onSelect: (selection: BoardAndPortSelection) => void;
}
@ -131,7 +133,7 @@ export class BoardAndPortSelectionComponent extends React.Component<BoardAndPort
if (this.portListComponent) {
this.portListComponent.reset();
}
this.setState({selection: {}});
this.setState({ selection: {} });
}
render(): React.ReactNode {
@ -207,8 +209,8 @@ export class BoardAndPortSelectionComponent extends React.Component<BoardAndPort
protected async setPorts() {
const ports: string[] = [];
const attached = await this.props.boardsService.getAttachedBoards();
attached.boards.forEach(board => {
const boards = await this.props.boardFrontendService.getAttachedBoards();
boards.forEach(board => {
if (AttachedSerialBoard.is(board)) {
ports.push(board.port);
}
@ -222,6 +224,8 @@ export class SelectBoardDialogWidget extends ReactWidget {
@inject(BoardsService)
protected readonly boardsService: BoardsService;
@inject(BoardFrontendService)
protected readonly boardFrontendService: BoardFrontendService;
@inject(BoardsNotificationService)
protected readonly boardsNotificationService: BoardsNotificationService;
@ -279,7 +283,11 @@ export class SelectBoardDialogWidget extends ReactWidget {
<p>but not to upload your sketch.</p>
</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>
</React.Fragment>