1
1
import * as grpc from '@grpc/grpc-js' ;
2
- import { inject , injectable } from 'inversify' ;
2
+ import { inject , injectable , postConstruct } from 'inversify' ;
3
3
import { Event , Emitter } from '@theia/core/lib/common/event' ;
4
4
import { DisposableCollection } from '@theia/core/lib/common/disposable' ;
5
5
import { GrpcClientProvider } from './grpc-client-provider' ;
@@ -25,27 +25,39 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
25
25
client . client . close ( ) ;
26
26
}
27
27
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.
31
40
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 ) ;
36
43
}
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
+ } )
41
53
}
42
54
43
55
protected async createClient ( port : string | number ) : Promise < CoreClientProvider . Client > {
44
56
// https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/master/doc/grpcjs_support.md#usage
45
57
// @ts -ignore
46
58
const ArduinoCoreServiceClient = grpc . makeClientConstructor ( commandsGrpcPb [ 'cc.arduino.cli.commands.v1.ArduinoCoreService' ] , 'ArduinoCoreServiceService' ) as any ;
47
59
const client = new ArduinoCoreServiceClient ( `localhost:${ port } ` , grpc . credentials . createInsecure ( ) , this . channelOptions ) as ArduinoCoreServiceClient ;
48
-
60
+
49
61
const createRes = await new Promise < CreateResponse > ( ( resolve , reject ) => {
50
62
client . create ( new CreateRequest ( ) , ( err , res : CreateResponse ) => {
51
63
if ( err ) {
@@ -61,12 +73,6 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
61
73
throw new Error ( 'Could not retrieve instance from the initialize response.' ) ;
62
74
}
63
75
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
-
70
76
return { instance, client } ;
71
77
}
72
78
@@ -100,7 +106,7 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
100
106
} )
101
107
}
102
108
103
- protected async updateIndexes ( { client, instance } : CoreClientProvider . Client ) : Promise < void > {
109
+ protected async updateIndexes ( { client, instance } : CoreClientProvider . Client ) : Promise < CoreClientProvider . Client > {
104
110
// in a separate promise, try and update the index
105
111
let indexUpdateSucceeded = true ;
106
112
for ( let i = 0 ; i < 10 ; i ++ ) {
@@ -133,6 +139,7 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
133
139
if ( indexUpdateSucceeded && libIndexUpdateSucceeded ) {
134
140
this . notificationService . notifyIndexUpdated ( ) ;
135
141
}
142
+ return { client, instance }
136
143
}
137
144
138
145
protected async updateLibraryIndex ( { client, instance } : CoreClientProvider . Client ) : Promise < void > {
0 commit comments