1
1
import * as grpc from '@grpc/grpc-js' ;
2
2
import { inject , injectable , postConstruct } from 'inversify' ;
3
- import { Event , Emitter } from '@theia/core/lib/common/event' ;
4
- import { DisposableCollection } from '@theia/core/lib/common/disposable' ;
5
3
import { GrpcClientProvider } from './grpc-client-provider' ;
6
4
import { ArduinoCoreServiceClient } from './cli-protocol/cc/arduino/cli/commands/v1/commands_grpc_pb' ;
7
5
import { Instance } from './cli-protocol/cc/arduino/cli/commands/v1/common_pb' ;
8
6
import { CreateRequest , CreateResponse , InitRequest , InitResponse , UpdateIndexRequest , UpdateIndexResponse , UpdateLibrariesIndexRequest , UpdateLibrariesIndexResponse } from './cli-protocol/cc/arduino/cli/commands/v1/commands_pb' ;
9
7
import * as commandsGrpcPb from './cli-protocol/cc/arduino/cli/commands/v1/commands_grpc_pb' ;
10
8
import { NotificationServiceServer } from '../common/protocol' ;
9
+ import { Deferred } from '@theia/core/lib/common/promise-util' ;
11
10
12
11
@injectable ( )
13
12
export class CoreClientProvider extends GrpcClientProvider < CoreClientProvider . Client > {
14
13
15
14
@inject ( NotificationServiceServer )
16
15
protected readonly notificationService : NotificationServiceServer ;
17
16
18
- protected readonly onClientReadyEmitter = new Emitter < void > ( ) ;
17
+ protected _created = new Deferred < void > ( ) ;
18
+ protected _initialized = new Deferred < void > ( ) ;
19
19
20
- get onClientReady ( ) : Event < void > {
21
- return this . onClientReadyEmitter . event ;
20
+ get created ( ) : Promise < void > {
21
+ return this . _created . promise ;
22
+ }
23
+
24
+ get initialized ( ) : Promise < void > {
25
+ return this . _initialized . promise
22
26
}
23
27
24
28
close ( client : CoreClientProvider . Client ) : void {
25
29
client . client . close ( ) ;
30
+ this . _created . reject ( ) ;
31
+ this . _initialized . reject ( ) ;
32
+ this . _created = new Deferred < void > ( ) ;
33
+ this . _initialized = new Deferred < void > ( ) ;
26
34
}
27
35
28
36
@postConstruct ( )
@@ -33,13 +41,14 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
33
41
// and notify client is ready.
34
42
// TODO: Creation failure should probably be handled here
35
43
await this . reconcileClient ( cliConfig ? cliConfig . daemon . port : undefined )
36
- . then ( ( ) => { this . onClientReadyEmitter . fire ( ) ; } ) ;
44
+ . then ( ( ) => { this . _created . resolve ( ) } ) ;
37
45
38
46
// If client has been created correctly update indexes and initialize
39
47
// its instance by loading platforms and cores.
40
48
if ( this . _client && ! ( this . _client instanceof Error ) ) {
41
49
await this . updateIndexes ( this . _client )
42
- . then ( this . initInstance ) ;
50
+ . then ( this . initInstance )
51
+ . then ( ( ) => { this . _initialized . resolve ( ) ; } ) ;
43
52
}
44
53
} ) ;
45
54
@@ -219,29 +228,13 @@ export abstract class CoreClientAware {
219
228
protected readonly coreClientProvider : CoreClientProvider ;
220
229
221
230
protected async coreClient ( ) : Promise < CoreClientProvider . Client > {
222
- const coreClient = await new Promise < CoreClientProvider . Client > ( async ( resolve , reject ) => {
223
- const handle = ( c : CoreClientProvider . Client | Error ) => {
224
- if ( c instanceof Error ) {
225
- reject ( c ) ;
226
- } else {
227
- resolve ( c ) ;
228
- }
231
+ return await new Promise < CoreClientProvider . Client > ( async ( resolve , reject ) => {
232
+ const client = await this . coreClientProvider . client ( )
233
+ if ( client && client instanceof Error ) {
234
+ reject ( client )
235
+ } else if ( client ) {
236
+ return resolve ( client ) ;
229
237
}
230
- const client = await this . coreClientProvider . client ( ) ;
231
- if ( client ) {
232
- handle ( client ) ;
233
- return ;
234
- }
235
- const toDispose = new DisposableCollection ( ) ;
236
- toDispose . push ( this . coreClientProvider . onClientReady ( async ( ) => {
237
- const client = await this . coreClientProvider . client ( ) ;
238
- if ( client ) {
239
- handle ( client ) ;
240
- }
241
- toDispose . dispose ( ) ;
242
- } ) ) ;
243
238
} ) ;
244
- return coreClient ;
245
239
}
246
-
247
240
}
0 commit comments