forward messages only when connected.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta 2019-12-06 15:47:17 +01:00
parent 729588770e
commit 1b95242ad1
2 changed files with 28 additions and 8 deletions

View File

@ -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<MonitorConnection.State | undefined>();
readonly onConnectionChanged: Event<MonitorConnection.State | undefined> = this.onConnectionChangedEmitter.event;
/**
* This emitter forwards all read events **iff** the connection is established.
*/
protected readonly onReadEmitter = new Emitter<MonitorReadEvent>();
@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<MonitorConnection.State | undefined> {
return this.onConnectionChangedEmitter.event;
}
get onRead(): Event<MonitorReadEvent> {
return this.onReadEmitter.event;
}
protected async handleBoardConfigChange(boardsConfig: BoardsConfig.Config): Promise<void> {
if (this.autoConnect) {
if (this.boardsServiceClient.canUploadTo(boardsConfig, { silent: false })) {

View File

@ -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 {
<div className='body'>
<SerialMonitorOutput
monitorModel={this.monitorModel}
monitorServiceClient={this.monitorServiceClient}
monitorConnection={this.monitorConnection}
clearConsoleEvent={this.clearOutputEmitter.event} />
</div>
</div>;
@ -251,8 +253,8 @@ export class SerialMonitorSendInput extends React.Component<SerialMonitorSendInp
export namespace SerialMonitorOutput {
export interface Props {
readonly monitorServiceClient: MonitorServiceClientImpl;
readonly monitorModel: MonitorModel;
readonly monitorConnection: MonitorConnection;
readonly clearConsoleEvent: Event<void>;
}
export interface State {
@ -287,7 +289,7 @@ export class SerialMonitorOutput extends React.Component<SerialMonitorOutput.Pro
this.scrollToBottom();
let chunk = '';
this.toDisposeBeforeUnmount.pushAll([
this.props.monitorServiceClient.onRead(({ data }) => {
this.props.monitorConnection.onRead(({ data }) => {
chunk += data;
const eolIndex = chunk.indexOf('\n');
if (eolIndex !== -1) {