From 557ec2ae429602dd9e1bde2551b0ce6c6560091f Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Thu, 5 Dec 2019 17:14:54 +0100 Subject: [PATCH] Wait until the boards config has been reset from the local storage, then start the monitor connection. Signed-off-by: Akos Kitta --- .../src/browser/arduino-frontend-module.ts | 1 + .../boards/boards-service-client-impl.ts | 21 ++++++++------ .../src/browser/monitor/monitor-connection.ts | 12 ++++++-- .../monitor/monitor-view-contribution.tsx | 4 +-- .../src/browser/monitor/monitor-widget.tsx | 28 +++++++++---------- 5 files changed, 38 insertions(+), 28 deletions(-) diff --git a/arduino-ide-extension/src/browser/arduino-frontend-module.ts b/arduino-ide-extension/src/browser/arduino-frontend-module.ts index 22a2baec..98402f8e 100644 --- a/arduino-ide-extension/src/browser/arduino-frontend-module.ts +++ b/arduino-ide-extension/src/browser/arduino-frontend-module.ts @@ -119,6 +119,7 @@ export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Un }).inSingletonScope(); // Boards service client to receive and delegate notifications from the backend. bind(BoardsServiceClientImpl).toSelf().inSingletonScope(); + bind(FrontendApplicationContribution).toService(BoardsServiceClientImpl); bind(BoardsServiceClient).toDynamicValue(context => { const client = context.container.get(BoardsServiceClientImpl); WebSocketConnectionProvider.createProxy(context.container, BoardsServicePath, client); diff --git a/arduino-ide-extension/src/browser/boards/boards-service-client-impl.ts b/arduino-ide-extension/src/browser/boards/boards-service-client-impl.ts index 96739719..60309f9c 100644 --- a/arduino-ide-extension/src/browser/boards/boards-service-client-impl.ts +++ b/arduino-ide-extension/src/browser/boards/boards-service-client-impl.ts @@ -1,14 +1,15 @@ -import { injectable, inject, postConstruct } from 'inversify'; +import { injectable, inject } from 'inversify'; import { Emitter } from '@theia/core/lib/common/event'; import { ILogger } from '@theia/core/lib/common/logger'; +import { MessageService } from '@theia/core/lib/common/message-service'; import { LocalStorageService } from '@theia/core/lib/browser/storage-service'; +import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application'; import { RecursiveRequired } from '../../common/types'; import { BoardsServiceClient, AttachedBoardsChangeEvent, BoardInstalledEvent, AttachedSerialBoard, Board, Port, BoardUninstalledEvent } from '../../common/protocol/boards-service'; import { BoardsConfig } from './boards-config'; -import { MessageService } from '@theia/core'; @injectable() -export class BoardsServiceClientImpl implements BoardsServiceClient { +export class BoardsServiceClientImpl implements BoardsServiceClient, FrontendApplicationContribution { @inject(ILogger) protected logger: ILogger; @@ -39,9 +40,8 @@ export class BoardsServiceClientImpl implements BoardsServiceClient { readonly onBoardUninstalled = this.onBoardUninstalledEmitter.event; readonly onBoardsConfigChanged = this.onSelectedBoardsConfigChangedEmitter.event; - @postConstruct() - protected init(): void { - this.loadState(); + async onStart(): Promise { + return this.loadState(); } notifyAttachedBoardsChanged(event: AttachedBoardsChangeEvent): void { @@ -124,7 +124,7 @@ export class BoardsServiceClientImpl implements BoardsServiceClient { if (!config.selectedBoard) { if (!options.silent) { - this.messageService.warn('No boards selected.'); + this.messageService.warn('No boards selected.', { timeout: 3000 }); } return false; } @@ -146,14 +146,14 @@ export class BoardsServiceClientImpl implements BoardsServiceClient { const { name } = config.selectedBoard; if (!config.selectedPort) { if (!options.silent) { - this.messageService.warn(`No ports selected for board: '${name}'.`); + this.messageService.warn(`No ports selected for board: '${name}'.`, { timeout: 3000 }); } return false; } if (!config.selectedBoard.fqbn) { if (!options.silent) { - this.messageService.warn(`The FQBN is not available for the selected board ${name}. Do you have the corresponding core installed?`); + this.messageService.warn(`The FQBN is not available for the selected board ${name}. Do you have the corresponding core installed?`, { timeout: 3000 }); } return false; } @@ -169,6 +169,9 @@ export class BoardsServiceClientImpl implements BoardsServiceClient { const storedValidBoardsConfig = await this.storageService.getData>('latest-valid-boards-config'); if (storedValidBoardsConfig) { this.latestValidBoardsConfig = storedValidBoardsConfig; + if (this.canUploadTo(this.latestValidBoardsConfig)) { + this.boardsConfig = this.latestValidBoardsConfig; + } } } diff --git a/arduino-ide-extension/src/browser/monitor/monitor-connection.ts b/arduino-ide-extension/src/browser/monitor/monitor-connection.ts index b6d39dce..208dd515 100644 --- a/arduino-ide-extension/src/browser/monitor/monitor-connection.ts +++ b/arduino-ide-extension/src/browser/monitor/monitor-connection.ts @@ -2,6 +2,7 @@ import { injectable, inject, postConstruct } from 'inversify'; import { Emitter, Event } from '@theia/core/lib/common/event'; // import { ConnectionStatusService } from '@theia/core/lib/browser/connection-status-service'; import { MessageService } from '@theia/core/lib/common/message-service'; +import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state'; import { MonitorService, MonitorConfig, MonitorError, Status } from '../../common/protocol/monitor-service'; import { BoardsServiceClientImpl } from '../boards/boards-service-client-impl'; import { Port, Board, BoardsService, AttachedSerialBoard, AttachedBoardsChangeEvent } from '../../common/protocol/boards-service'; @@ -33,6 +34,9 @@ export class MonitorConnection { // @inject(ConnectionStatusService) // protected readonly connectionStatusService: ConnectionStatusService; + @inject(FrontendApplicationStateService) + protected readonly applicationState: FrontendApplicationStateService; + protected state: MonitorConnection.State | undefined; /** * Note: The idea is to toggle this property from the UI (`Monitor` view) @@ -120,8 +124,12 @@ export class MonitorConnection { this._autoConnect = value; // When we enable the auto-connect, we have to connect if (!oldValue && value) { - const { boardsConfig } = this.boardsServiceClient; - this.handleBoardConfigChange(boardsConfig); + // We have to make sure the previous boards config has been restored. + // Otherwise, we might start the auto-connection without configured boards. + this.applicationState.reachedState('started_contributions').then(() => { + const { boardsConfig } = this.boardsServiceClient; + this.handleBoardConfigChange(boardsConfig); + }); } } diff --git a/arduino-ide-extension/src/browser/monitor/monitor-view-contribution.tsx b/arduino-ide-extension/src/browser/monitor/monitor-view-contribution.tsx index 1231bd22..2067799b 100644 --- a/arduino-ide-extension/src/browser/monitor/monitor-view-contribution.tsx +++ b/arduino-ide-extension/src/browser/monitor/monitor-view-contribution.tsx @@ -59,13 +59,13 @@ export class MonitorViewContribution extends AbstractViewContribution this.renderAutoScrollButton(), isVisible: widget => widget instanceof MonitorWidget, - onDidChange: this.model.onChange as any // TODO: https://github.com/eclipse-theia/theia/pull/6696/ + onDidChange: this.model.onChange as any // XXX: it's a hack. See: https://github.com/eclipse-theia/theia/pull/6696/ }); registry.registerItem({ id: 'monitor-timestamp', render: () => this.renderTimestampButton(), isVisible: widget => widget instanceof MonitorWidget, - onDidChange: this.model.onChange as any // TODO: https://github.com/eclipse-theia/theia/pull/6696/ + onDidChange: this.model.onChange as any // XXX: it's a hack. See: https://github.com/eclipse-theia/theia/pull/6696/ }); registry.registerItem({ id: SerialMonitor.Commands.CLEAR_OUTPUT.id, diff --git a/arduino-ide-extension/src/browser/monitor/monitor-widget.tsx b/arduino-ide-extension/src/browser/monitor/monitor-widget.tsx index 82bf6716..23dd168e 100644 --- a/arduino-ide-extension/src/browser/monitor/monitor-widget.tsx +++ b/arduino-ide-extension/src/browser/monitor/monitor-widget.tsx @@ -169,7 +169,7 @@ export namespace SerialMonitorSendInput { readonly resolveFocus: (element: HTMLElement | undefined) => void; } export interface State { - value: string; + text: string; } } @@ -177,23 +177,21 @@ export class SerialMonitorSendInput extends React.Component) { super(props); - this.state = { value: '' }; + this.state = { text: '' }; this.onChange = this.onChange.bind(this); this.onSend = this.onSend.bind(this); this.onKeyDown = this.onKeyDown.bind(this); } render(): React.ReactNode { - return - - + return } protected get placeholder(): string { @@ -212,12 +210,12 @@ export class SerialMonitorSendInput extends React.Component): void { - this.setState({ value: event.target.value }); + this.setState({ text: event.target.value }); } protected onSend(): void { - this.props.onSend(this.state.value); - this.setState({ value: '' }); + this.props.onSend(this.state.text); + this.setState({ text: '' }); } protected onKeyDown(event: React.KeyboardEvent): void {