Skip to content

Commit

Permalink
Refactor Channel API to an object graph based approach
Browse files Browse the repository at this point in the history
- Refactor Channel API to enable sending of any Javscript object over channels instead of having to receive a writeBuffer and write encoding the object into a binary format. This means we essentially move the binary encoding a level 
- Combine old  message encoder/decoders into a common BinaryMessageCodec interface.
- Adapt dependent components and services to new API 

Part of eclipse-theia#11159

Contributed on behalf of STMicroelectronics
  • Loading branch information
tortmayr committed Jun 22, 2022
1 parent 8e9f4de commit 72663ff
Show file tree
Hide file tree
Showing 36 changed files with 977 additions and 1,393 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/browser/logger-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ILogger, Logger, LoggerFactory, setRootLogger, LoggerName, rootLoggerNa
import { LoggerWatcher } from '../common/logger-watcher';
import { WebSocketConnectionProvider } from './messaging';
import { FrontendApplicationContribution } from './frontend-application';
import { EncodingError } from '../common/message-rpc/rpc-message-encoder';
import { EncodingError } from '../common/messaging/message-codec';

export const loggerFrontendModule = new ContainerModule(bind => {
bind(FrontendApplicationContribution).toDynamicValue(ctx => ({
Expand Down
23 changes: 12 additions & 11 deletions packages/core/src/browser/messaging/ws-connection-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************

import { injectable, interfaces, decorate, unmanaged } from 'inversify';
import { JsonRpcProxyFactory, JsonRpcProxy, Emitter, Event, Channel } from '../../common';
import { Endpoint } from '../endpoint';
import { AbstractConnectionProvider } from '../../common/messaging/abstract-connection-provider';
import { decorate, injectable, interfaces, unmanaged } from 'inversify';
import { io, Socket } from 'socket.io-client';
import { IWebSocket, WebSocketChannel } from '../../common/messaging/web-socket-channel';
import { Channel, Emitter, Event, JsonRpcProxy, JsonRpcProxyFactory } from '../../common';
import { AbstractConnectionProvider } from '../../common/messaging/abstract-connection-provider';
import { IWebSocket, WebSocketChannel, wsServicePath } from '../../common/messaging/web-socket-channel';
import { Endpoint } from '../endpoint';

decorate(injectable(), JsonRpcProxyFactory);
decorate(unmanaged(), JsonRpcProxyFactory, 0);

export interface WebSocketOptions {
export interface WebsocketOptions {
/**
* True by default.
*/
reconnecting?: boolean;
}

@injectable()
export class WebSocketConnectionProvider extends AbstractConnectionProvider<WebSocketOptions> {
export class WebSocketConnectionProvider extends AbstractConnectionProvider<WebsocketOptions> {

protected readonly onSocketDidOpenEmitter: Emitter<void> = new Emitter();
get onSocketDidOpen(): Event<void> {
Expand All @@ -52,7 +52,7 @@ export class WebSocketConnectionProvider extends AbstractConnectionProvider<WebS

constructor() {
super();
const url = this.createWebSocketUrl(WebSocketChannel.wsPath);
const url = this.createWebSocketUrl(wsServicePath);
this.socket = this.createWebSocket(url);
this.socket.on('connect', () => {
this.initializeMultiplexer();
Expand Down Expand Up @@ -81,14 +81,15 @@ export class WebSocketConnectionProvider extends AbstractConnectionProvider<WebS
isConnected: () => socket.connected,
onClose: cb => socket.on('disconnect', reason => cb(reason)),
onError: cb => socket.on('error', reason => cb(reason)),
onMessage: cb => socket.on('message', data => cb(data)),
onMessage: cb => socket.on('message', data => cb(new Uint8Array(data))),
send: message => socket.emit('message', message)
};
}

override async openChannel(path: string, handler: (channel: Channel) => void, options?: WebSocketOptions): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
override async openChannel(path: string, handler: (channel: Channel) => void, options?: WebsocketOptions): Promise<void> {
if (this.socket.connected) {
return super.openChannel(path, handler, options);
return super.openChannel<T>(path, handler, options);
} else {
const openChannel = () => {
this.socket.off('connect', openChannel);
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export * from './contribution-provider';
export * from './path';
export * from './logger';
export * from './messaging';
export * from './message-rpc';
export * from './message-service';
export * from './message-service-protocol';
export * from './progress-service';
Expand Down
260 changes: 0 additions & 260 deletions packages/core/src/common/message-rpc/channel.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/core/src/common/message-rpc/index.ts

This file was deleted.

Loading

0 comments on commit 72663ff

Please sign in to comment.