arduino-ide/arduino-ide-extension/src/node/grpc-client-provider.ts
Akos Kitta 6ce4143d49 [ci]: Made various changes for the electron app:
- Support for multiple electron targe per platform.
 - Removed packager CLI. Changed the logic we calculate the app name.
 - Fixed various OS-specific tests: stubbed `os`.
 - Restructured the final ZIP formats for Windows and Linux.
 - Added packager tests.
 - Switched from `@grpc/grpc-js` to native `grpc`.
 - Updated the version from 0.0.5 to 0.0.6.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
2020-04-09 08:55:43 +02:00

80 lines
2.4 KiB
TypeScript

import * as grpc from 'grpc';
import { inject, injectable, postConstruct } from 'inversify';
import { ILogger } from '@theia/core/lib/common/logger';
import { MaybePromise } from '@theia/core/lib/common/types';
import { ConfigServiceImpl } from './config-service-impl';
import { ArduinoDaemonImpl } from './arduino-daemon-impl';
@injectable()
export abstract class GrpcClientProvider<C> {
@inject(ILogger)
protected readonly logger: ILogger;
@inject(ArduinoDaemonImpl)
protected readonly daemon: ArduinoDaemonImpl;
@inject(ConfigServiceImpl)
protected readonly configService: ConfigServiceImpl;
protected _port: string | number | undefined;
protected _client: C | undefined;
@postConstruct()
protected init(): void {
const updateClient = () => {
const cliConfig = this.configService.cliConfiguration;
this.reconcileClient(cliConfig ? cliConfig.daemon.port : undefined);
}
this.configService.onConfigChange(updateClient);
this.daemon.ready.then(updateClient);
this.daemon.onDaemonStopped(() => {
if (this._client) {
this.close(this._client);
}
this._client = undefined;
this._port = undefined;
})
}
async client(): Promise<C | undefined> {
try {
await this.daemon.ready;
return this._client;
} catch (error) {
return undefined;
}
}
protected async reconcileClient(port: string | number | undefined): Promise<void> {
if (this._port === port) {
return; // Nothing to do.
}
this._port = port;
if (this._client) {
this.close(this._client);
this._client = undefined;
}
if (this._port) {
try {
const client = await this.createClient(this._port);
this._client = client;
} catch (error) {
this.logger.error('Could create client for gRPC.', error)
}
}
}
protected abstract createClient(port: string | number): MaybePromise<C>;
protected abstract close(client: C): void;
protected get channelOptions(): grpc.CallOptions {
return {
'grpc.max_send_message_length': 512 * 1024 * 1024,
'grpc.max_receive_message_length': 512 * 1024 * 1024
};
}
}