@@ -19,7 +19,7 @@ import {
19
19
UpdateLibrariesIndexResponse ,
20
20
} from './cli-protocol/cc/arduino/cli/commands/v1/commands_pb' ;
21
21
import * as commandsGrpcPb from './cli-protocol/cc/arduino/cli/commands/v1/commands_grpc_pb' ;
22
- import { NotificationServiceServer , ProgressMessage } from '../common/protocol' ;
22
+ import { NotificationServiceServer } from '../common/protocol' ;
23
23
import { Deferred , retry } from '@theia/core/lib/common/promise-util' ;
24
24
import {
25
25
Status as RpcStatus ,
@@ -29,8 +29,10 @@ import { ConfigServiceImpl } from './config-service-impl';
29
29
import { ArduinoDaemonImpl } from './arduino-daemon-impl' ;
30
30
import { DisposableCollection } from '@theia/core/lib/common/disposable' ;
31
31
import { Disposable } from '@theia/core/shared/vscode-languageserver-protocol' ;
32
- import { v4 } from 'uuid' ;
33
- import { InstallWithProgress } from './grpc-installable' ;
32
+ import {
33
+ IndexesUpdateProgressHandler ,
34
+ InstallWithProgress ,
35
+ } from './grpc-installable' ;
34
36
import type { DefaultCliConfig } from './cli-config' ;
35
37
import { ServiceError } from './service-error' ;
36
38
@@ -242,91 +244,45 @@ export class CoreClientProvider {
242
244
private async updateIndexes (
243
245
client : CoreClientProvider . Client
244
246
) : Promise < CoreClientProvider . Client > {
245
- const progressId = v4 ( ) ;
246
- // Note: at this point, the IDE2 backend might not have any connected clients, so this notification is not delivered to anywhere
247
- // Hence, clients must handle gracefully when no `willUpdate` is received before any `didProgress`.
248
- this . notificationService . notifyIndexWillUpdate ( progressId ) ;
249
- // The index update progress responses are not much helpful from the CLI. They cannot provide a fine-grain progress.
250
- // So IDE2 could report two total works only: index update and library index update.
251
- // See here an example: https://github.com/arduino/arduino-ide/issues/906#issuecomment-1171145630
252
- // Due to this, IDE2 fakes the progress to provide slightly better UX.
253
- const totalPlatformIndexCount =
254
- ( this . configService . cliConfiguration ?. board_manager ?. additional_urls
255
- ?. length ?? 0 ) + 1 ; // +1 for the `package_index.tar.bz2` when updating the platform index.
256
- // The `library_index.json.gz` and `library_index.json.sig` when running the library index update.
257
- const totalLibraryIndexCount = 2 ;
258
- let work = {
259
- done : 0 ,
260
- total : totalPlatformIndexCount + totalLibraryIndexCount + 1 , // +1 for better UX, leave the last step complete when all subtasks are done.
261
- } ;
262
- const reportProgress = ( {
263
- progressId,
264
- message,
265
- } : Omit < ProgressMessage , 'work' > ) => {
266
- work = {
267
- ...work ,
268
- done : work . done + 1 ,
269
- } ;
270
- this . notificationService . notifyIndexUpdateDidProgress ( {
271
- message,
272
- progressId,
273
- work,
274
- } ) ;
275
- } ;
276
- const toDispose = new DisposableCollection ( ) ;
247
+ const progressHandler = this . createProgressHandler ( ) ;
277
248
try {
278
- const onDidProgressEmitter = new Emitter < ProgressMessage > ( ) ;
279
- const onDidProgress = onDidProgressEmitter . event ;
280
- toDispose . pushAll ( [
281
- onDidProgressEmitter ,
282
- onDidProgress ( ( { message, progressId } ) =>
283
- reportProgress ( { progressId, message } )
284
- ) ,
285
- ] ) ;
286
249
await Promise . all ( [
287
- this . updateIndex ( client , progressId , onDidProgressEmitter ) ,
288
- this . updateLibraryIndex ( client , progressId , onDidProgressEmitter ) ,
289
- ] ) ;
290
- this . notificationService . notifyIndexDidUpdate ( progressId ) ;
250
+ this . updateIndex ( client , progressHandler ) ,
251
+ this . updateLibraryIndex ( client , progressHandler ) ,
252
+ ] ) . then ( ( ) => progressHandler . reportEnd ( ) ) ;
291
253
} catch ( err ) {
292
- this . notificationService . notifyIndexUpdateDidFail ( {
293
- progressId,
294
- message : ServiceError . is ( err ) ? err . details : String ( err ) ,
295
- } ) ;
296
- } finally {
297
- toDispose . dispose ( ) ;
254
+ console . error ( 'Failed to update indexes' , err ) ;
255
+ progressHandler . reportError (
256
+ ServiceError . is ( err ) ? err . details : String ( err )
257
+ ) ;
298
258
}
299
259
return client ;
300
260
}
301
261
302
262
private async updateIndex (
303
263
client : CoreClientProvider . Client ,
304
- progressId : string ,
305
- onDidProgressEmitter : Emitter < ProgressMessage >
264
+ progressHandler : IndexesUpdateProgressHandler
306
265
) : Promise < void > {
307
266
return this . doUpdateIndex (
308
267
( ) =>
309
268
client . client . updateIndex (
310
269
new UpdateIndexRequest ( ) . setInstance ( client . instance )
311
270
) ,
312
- progressId ,
313
- onDidProgressEmitter ,
271
+ progressHandler ,
314
272
'platform-index'
315
273
) ;
316
274
}
317
275
318
276
private async updateLibraryIndex (
319
277
client : CoreClientProvider . Client ,
320
- progressId : string ,
321
- onDidProgressEmitter : Emitter < ProgressMessage >
278
+ progressHandler : IndexesUpdateProgressHandler
322
279
) : Promise < void > {
323
280
return this . doUpdateIndex (
324
281
( ) =>
325
282
client . client . updateLibrariesIndex (
326
283
new UpdateLibrariesIndexRequest ( ) . setInstance ( client . instance )
327
284
) ,
328
- progressId ,
329
- onDidProgressEmitter ,
285
+ progressHandler ,
330
286
'library-index'
331
287
) ;
332
288
}
@@ -338,10 +294,10 @@ export class CoreClientProvider {
338
294
| UpdateCoreLibrariesIndexResponse // not used by IDE2
339
295
> (
340
296
responseProvider : ( ) => grpc . ClientReadableStream < R > ,
341
- progressId : string ,
342
- onDidProgressEmitter : Emitter < ProgressMessage > ,
297
+ progressHandler : IndexesUpdateProgressHandler ,
343
298
task ?: string
344
299
) : Promise < void > {
300
+ const { progressId } = progressHandler ;
345
301
return retry (
346
302
( ) =>
347
303
new Promise < void > ( ( resolve , reject ) => {
@@ -350,15 +306,12 @@ export class CoreClientProvider {
350
306
'data' ,
351
307
InstallWithProgress . createDataCallback ( {
352
308
responseService : {
353
- appendToOutput : ( message ) => {
309
+ appendToOutput : ( { chunk : message } ) => {
354
310
console . log (
355
311
`core-client-provider${ task ? ` [${ task } ]` : '' } ` ,
356
- message . chunk
312
+ message
357
313
) ;
358
- onDidProgressEmitter . fire ( {
359
- message : message . chunk ,
360
- progressId,
361
- } ) ;
314
+ progressHandler . reportProgress ( message ) ;
362
315
} ,
363
316
} ,
364
317
progressId,
@@ -372,6 +325,26 @@ export class CoreClientProvider {
372
325
) ;
373
326
}
374
327
328
+ private createProgressHandler ( ) {
329
+ const additionalUrlsCount =
330
+ this . configService . cliConfiguration ?. board_manager ?. additional_urls
331
+ ?. length ?? 0 ;
332
+ const progressHandler = new IndexesUpdateProgressHandler (
333
+ additionalUrlsCount ,
334
+ ( progressMessage ) =>
335
+ this . notificationService . notifyIndexUpdateDidProgress ( progressMessage ) ,
336
+ ( { progressId, message } ) =>
337
+ this . notificationService . notifyIndexUpdateDidFail ( {
338
+ progressId,
339
+ message,
340
+ } ) ,
341
+ ( progressId ) =>
342
+ this . notificationService . notifyIndexWillUpdate ( progressId ) ,
343
+ ( progressId ) => this . notificationService . notifyIndexDidUpdate ( progressId )
344
+ ) ;
345
+ return progressHandler ;
346
+ }
347
+
375
348
private address ( port : string ) : string {
376
349
return `localhost:${ port } ` ;
377
350
}
0 commit comments