1
1
import * as grpc from '@grpc/grpc-js' ;
2
2
import { inject , injectable } from 'inversify' ;
3
3
import { Event , Emitter } from '@theia/core/lib/common/event' ;
4
+ import { DisposableCollection } from '@theia/core/lib/common/disposable' ;
4
5
import { GrpcClientProvider } from './grpc-client-provider' ;
5
6
import { ArduinoCoreClient } from './cli-protocol/commands/commands_grpc_pb' ;
6
7
import * as commandsGrpcPb from './cli-protocol/commands/commands_grpc_pb' ;
@@ -27,7 +28,7 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
27
28
protected async reconcileClient ( port : string | undefined ) : Promise < void > {
28
29
if ( port && port === this . _port ) {
29
30
// No need to create a new gRPC client, but we have to update the indexes.
30
- if ( this . _client ) {
31
+ if ( this . _client && ! ( this . _client instanceof Error ) ) {
31
32
await this . updateIndexes ( this . _client ) ;
32
33
this . onClientReadyEmitter . fire ( ) ;
33
34
}
@@ -48,7 +49,7 @@ export class CoreClientProvider extends GrpcClientProvider<CoreClientProvider.Cl
48
49
let resp : InitResp | undefined = undefined ;
49
50
const stream = client . init ( initReq ) ;
50
51
stream . on ( 'data' , ( data : InitResp ) => resp = data ) ;
51
- stream . on ( 'end' , ( ) => resolve ( resp ) ) ;
52
+ stream . on ( 'end' , ( ) => resolve ( resp ! ) ) ;
52
53
stream . on ( 'error' , err => reject ( err ) ) ;
53
54
} ) ;
54
55
@@ -175,20 +176,27 @@ export abstract class CoreClientAware {
175
176
protected readonly coreClientProvider : CoreClientProvider ;
176
177
177
178
protected async coreClient ( ) : Promise < CoreClientProvider . Client > {
178
- const coreClient = await new Promise < CoreClientProvider . Client > ( async resolve => {
179
+ const coreClient = await new Promise < CoreClientProvider . Client > ( async ( resolve , reject ) => {
180
+ const handle = ( c : CoreClientProvider . Client | Error ) => {
181
+ if ( c instanceof Error ) {
182
+ reject ( c ) ;
183
+ } else {
184
+ resolve ( c ) ;
185
+ }
186
+ }
179
187
const client = await this . coreClientProvider . client ( ) ;
180
188
if ( client ) {
181
- resolve ( client ) ;
189
+ handle ( client ) ;
182
190
return ;
183
191
}
184
- const toDispose = this . coreClientProvider . onClientReady ( async ( ) => {
192
+ const toDispose = new DisposableCollection ( ) ;
193
+ toDispose . push ( this . coreClientProvider . onClientReady ( async ( ) => {
185
194
const client = await this . coreClientProvider . client ( ) ;
186
195
if ( client ) {
187
- toDispose . dispose ( ) ;
188
- resolve ( client ) ;
189
- return ;
196
+ handle ( client ) ;
190
197
}
191
- } ) ;
198
+ toDispose . dispose ( ) ;
199
+ } ) ) ;
192
200
} ) ;
193
201
return coreClient ;
194
202
}
0 commit comments