Skip to content

Commit 67da669

Browse files
committed
Made CLI initialization partly asynchronous
1 parent ade802b commit 67da669

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

Diff for: arduino-ide-extension/src/node/core-client-provider.ts

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as grpc from '@grpc/grpc-js';
2-
import { inject, injectable } from 'inversify';
2+
import { inject, injectable, postConstruct } from 'inversify';
33
import { Event, Emitter } from '@theia/core/lib/common/event';
44
import { DisposableCollection } from '@theia/core/lib/common/disposable';
55
import { GrpcClientProvider } from './grpc-client-provider';
@@ -25,27 +25,39 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
2525
client.client.close();
2626
}
2727

28-
protected async reconcileClient(port: string | number | undefined): Promise<void> {
29-
if (port && port === this._port) {
30-
// No need to create a new gRPC client, but we have to update the indexes.
28+
@postConstruct()
29+
protected async init(): Promise<void> {
30+
this.daemon.ready.then(async () => {
31+
const cliConfig = this.configService.cliConfiguration;
32+
// First create the client and the instance synchronously
33+
// and notify client is ready.
34+
// TODO: Creation failure should probably be handled here
35+
await this.reconcileClient(cliConfig ? cliConfig.daemon.port : undefined)
36+
.then(() => { this.onClientReadyEmitter.fire(); });
37+
38+
// If client has been created correctly update indexes and initialize
39+
// its instance by loading platforms and cores.
3140
if (this._client && !(this._client instanceof Error)) {
32-
await this.updateIndexes(this._client);
33-
// We must initialize the client again to get the updated indexes.
34-
await this.initInstance(this._client);
35-
this.onClientReadyEmitter.fire();
41+
await this.updateIndexes(this._client)
42+
.then(this.initInstance);
3643
}
37-
} else {
38-
await super.reconcileClient(port);
39-
this.onClientReadyEmitter.fire();
40-
}
44+
});
45+
46+
this.daemon.onDaemonStopped(() => {
47+
if (this._client && !(this._client instanceof Error)) {
48+
this.close(this._client);
49+
}
50+
this._client = undefined;
51+
this._port = undefined;
52+
})
4153
}
4254

4355
protected async createClient(port: string | number): Promise<CoreClientProvider.Client> {
4456
// https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/master/doc/grpcjs_support.md#usage
4557
// @ts-ignore
4658
const ArduinoCoreServiceClient = grpc.makeClientConstructor(commandsGrpcPb['cc.arduino.cli.commands.v1.ArduinoCoreService'], 'ArduinoCoreServiceService') as any;
4759
const client = new ArduinoCoreServiceClient(`localhost:${port}`, grpc.credentials.createInsecure(), this.channelOptions) as ArduinoCoreServiceClient;
48-
60+
4961
const createRes = await new Promise<CreateResponse>((resolve, reject) => {
5062
client.create(new CreateRequest(), (err, res: CreateResponse) => {
5163
if (err) {
@@ -61,12 +73,6 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
6173
throw new Error('Could not retrieve instance from the initialize response.');
6274
}
6375

64-
// Update indexes before init
65-
await this.updateIndexes({ instance, client });
66-
67-
// Initialize instance by loading indexes, platforms and libraries
68-
await this.initInstance({instance, client})
69-
7076
return { instance, client };
7177
}
7278

@@ -100,7 +106,7 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
100106
})
101107
}
102108

103-
protected async updateIndexes({ client, instance }: CoreClientProvider.Client): Promise<void> {
109+
protected async updateIndexes({ client, instance }: CoreClientProvider.Client): Promise<CoreClientProvider.Client> {
104110
// in a separate promise, try and update the index
105111
let indexUpdateSucceeded = true;
106112
for (let i = 0; i < 10; i++) {
@@ -133,6 +139,7 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
133139
if (indexUpdateSucceeded && libIndexUpdateSucceeded) {
134140
this.notificationService.notifyIndexUpdated();
135141
}
142+
return { client, instance }
136143
}
137144

138145
protected async updateLibraryIndex({ client, instance }: CoreClientProvider.Client): Promise<void> {

0 commit comments

Comments
 (0)