diff --git a/arduino-ide-extension/src/browser/monitor/monitor-connection.ts b/arduino-ide-extension/src/browser/monitor/monitor-connection.ts index af6396d0..13031f26 100644 --- a/arduino-ide-extension/src/browser/monitor/monitor-connection.ts +++ b/arduino-ide-extension/src/browser/monitor/monitor-connection.ts @@ -3,7 +3,7 @@ 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 { MonitorService, MonitorConfig, MonitorError, Status, MonitorReadEvent } from '../../common/protocol/monitor-service'; import { BoardsServiceClientImpl } from '../boards/boards-service-client-impl'; import { Port, Board, BoardsService, AttachedSerialBoard, AttachedBoardsChangeEvent } from '../../common/protocol/boards-service'; import { MonitorServiceClientImpl } from './monitor-service-client-impl'; @@ -44,11 +44,19 @@ export class MonitorConnection { */ protected _autoConnect: boolean = false; protected readonly onConnectionChangedEmitter = new Emitter(); - - readonly onConnectionChanged: Event = this.onConnectionChangedEmitter.event; + /** + * This emitter forwards all read events **iff** the connection is established. + */ + protected readonly onReadEmitter = new Emitter(); @postConstruct() protected init(): void { + // Forward the messages from the board **iff** connected. + this.monitorServiceClient.onRead(event => { + if (this.connected) { + this.onReadEmitter.fire(event); + } + }); this.monitorServiceClient.onError(async error => { let shouldReconnect = false; if (this.state) { @@ -140,9 +148,11 @@ export class MonitorConnection { return disconnectStatus; } } + console.info(`>>> Creating serial monitor connection for ${Board.toString(config.board)} on port ${Port.toString(config.port)}...`); const connectStatus = await this.monitorService.connect(config); if (Status.isOK(connectStatus)) { this.state = { config }; + console.info(`<<< Serial monitor connection created for ${Board.toString(config.board, { useFqbn: false })} on port ${Port.toString(config.port)}.`); } this.onConnectionChangedEmitter.fire(this.state); return Status.isOK(connectStatus); @@ -152,7 +162,7 @@ export class MonitorConnection { if (!this.state) { // XXX: we user `this.state` instead of `this.connected` to make the type checker happy. return Status.OK; } - console.log('>>> Disposing existing monitor connection before establishing a new one...'); + console.log('>>> Disposing existing monitor connection...'); const status = await this.monitorService.disconnect(); if (Status.isOK(status)) { console.log(`<<< Disposed connection. Was: ${MonitorConnection.State.toString(this.state)}`); @@ -179,6 +189,14 @@ export class MonitorConnection { }); } + get onConnectionChanged(): Event { + return this.onConnectionChangedEmitter.event; + } + + get onRead(): Event { + return this.onReadEmitter.event; + } + protected async handleBoardConfigChange(boardsConfig: BoardsConfig.Config): Promise { if (this.autoConnect) { if (this.boardsServiceClient.canUploadTo(boardsConfig, { silent: false })) { diff --git a/arduino-ide-extension/src/browser/monitor/monitor-widget.tsx b/arduino-ide-extension/src/browser/monitor/monitor-widget.tsx index ea3a2acc..7b12832a 100644 --- a/arduino-ide-extension/src/browser/monitor/monitor-widget.tsx +++ b/arduino-ide-extension/src/browser/monitor/monitor-widget.tsx @@ -81,7 +81,9 @@ export class MonitorWidget extends ReactWidget { } protected onUpdateRequest(msg: Message): void { - if (!this.closing) { + // TODO: `this.isAttached` + // See: https://github.com/eclipse-theia/theia/issues/6704#issuecomment-562574713 + if (!this.closing && this.isAttached) { super.onUpdateRequest(msg); } } @@ -160,7 +162,7 @@ export class MonitorWidget extends ReactWidget {
; @@ -251,8 +253,8 @@ export class SerialMonitorSendInput extends React.Component; } export interface State { @@ -287,7 +289,7 @@ export class SerialMonitorOutput extends React.Component { + this.props.monitorConnection.onRead(({ data }) => { chunk += data; const eolIndex = chunk.indexOf('\n'); if (eolIndex !== -1) {