|
| 1 | +import { Progress } from '@theia/core/lib/common/message-service-protocol'; |
| 2 | +import { ProgressService } from '@theia/core/lib/common/progress-service'; |
| 3 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 4 | +import { ProgressMessage } from '../../common/protocol'; |
| 5 | +import { NotificationCenter } from '../notification-center'; |
| 6 | +import { Contribution } from './contribution'; |
| 7 | + |
| 8 | +@injectable() |
| 9 | +export class IndexesUpdateProgress extends Contribution { |
| 10 | + @inject(NotificationCenter) |
| 11 | + private readonly notificationCenter: NotificationCenter; |
| 12 | + @inject(ProgressService) |
| 13 | + private readonly progressService: ProgressService; |
| 14 | + private currentProgress: |
| 15 | + | (Progress & Readonly<{ progressId: string }>) |
| 16 | + | undefined; |
| 17 | + |
| 18 | + override onStart(): void { |
| 19 | + this.notificationCenter.onIndexWillUpdate((progressId) => |
| 20 | + this.getOrCreateProgress(progressId) |
| 21 | + ); |
| 22 | + this.notificationCenter.onIndexUpdateDidProgress((progress) => { |
| 23 | + this.getOrCreateProgress(progress).then((delegate) => |
| 24 | + delegate.report(progress) |
| 25 | + ); |
| 26 | + }); |
| 27 | + this.notificationCenter.onIndexDidUpdate((progressId) => { |
| 28 | + this.cancelProgress(progressId); |
| 29 | + }); |
| 30 | + this.notificationCenter.onIndexUpdateDidFail(({ progressId, message }) => { |
| 31 | + this.cancelProgress(progressId); |
| 32 | + this.messageService.error(message); |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + private async getOrCreateProgress( |
| 37 | + progressOrId: ProgressMessage | string |
| 38 | + ): Promise<Progress & { progressId: string }> { |
| 39 | + const progressId = ProgressMessage.is(progressOrId) |
| 40 | + ? progressOrId.progressId |
| 41 | + : progressOrId; |
| 42 | + if (this.currentProgress?.progressId === progressId) { |
| 43 | + return this.currentProgress; |
| 44 | + } |
| 45 | + if (this.currentProgress) { |
| 46 | + this.currentProgress.cancel(); |
| 47 | + } |
| 48 | + this.currentProgress = undefined; |
| 49 | + const progress = await this.progressService.showProgress({ |
| 50 | + text: '', |
| 51 | + options: { location: 'notification' }, |
| 52 | + }); |
| 53 | + if (ProgressMessage.is(progressOrId)) { |
| 54 | + progress.report(progressOrId); |
| 55 | + } |
| 56 | + this.currentProgress = { ...progress, progressId }; |
| 57 | + return this.currentProgress; |
| 58 | + } |
| 59 | + |
| 60 | + private cancelProgress(progressId: string) { |
| 61 | + if (this.currentProgress) { |
| 62 | + if (this.currentProgress.progressId !== progressId) { |
| 63 | + console.warn( |
| 64 | + `Mismatching progress IDs. Expected ${progressId}, got ${this.currentProgress.progressId}. Canceling anyway.` |
| 65 | + ); |
| 66 | + } |
| 67 | + this.currentProgress.cancel(); |
| 68 | + this.currentProgress = undefined; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments