|
| 1 | +import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application'; |
| 2 | +import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state'; |
| 3 | +import { DisposableCollection } from '@theia/core/lib/common/disposable'; |
| 4 | +import { Emitter, Event } from '@theia/core/lib/common/event'; |
| 5 | +import { MessageService } from '@theia/core/lib/common/message-service'; |
| 6 | +import { deepClone } from '@theia/core/lib/common/objects'; |
| 7 | +import URI from '@theia/core/lib/common/uri'; |
| 8 | +import { |
| 9 | + inject, |
| 10 | + injectable, |
| 11 | + postConstruct, |
| 12 | +} from '@theia/core/shared/inversify'; |
| 13 | +import { ConfigService, ConfigState } from '../../common/protocol'; |
| 14 | +import { NotificationCenter } from '../notification-center'; |
| 15 | + |
| 16 | +@injectable() |
| 17 | +export class ConfigServiceClient implements FrontendApplicationContribution { |
| 18 | + @inject(ConfigService) |
| 19 | + private readonly delegate: ConfigService; |
| 20 | + @inject(NotificationCenter) |
| 21 | + private readonly notificationCenter: NotificationCenter; |
| 22 | + @inject(FrontendApplicationStateService) |
| 23 | + private readonly appStateService: FrontendApplicationStateService; |
| 24 | + @inject(MessageService) |
| 25 | + private readonly messageService: MessageService; |
| 26 | + |
| 27 | + private readonly didChangeSketchDirUriEmitter = new Emitter< |
| 28 | + URI | undefined |
| 29 | + >(); |
| 30 | + private readonly didChangeDataDirUriEmitter = new Emitter<URI | undefined>(); |
| 31 | + private readonly toDispose = new DisposableCollection( |
| 32 | + this.didChangeSketchDirUriEmitter, |
| 33 | + this.didChangeDataDirUriEmitter |
| 34 | + ); |
| 35 | + |
| 36 | + private config: ConfigState | undefined; |
| 37 | + |
| 38 | + @postConstruct() |
| 39 | + protected init(): void { |
| 40 | + this.appStateService.reachedState('ready').then(async () => { |
| 41 | + const config = await this.fetchConfig(); |
| 42 | + this.use(config); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + onStart(): void { |
| 47 | + this.notificationCenter.onConfigDidChange((config) => this.use(config)); |
| 48 | + } |
| 49 | + |
| 50 | + onStop(): void { |
| 51 | + this.toDispose.dispose(); |
| 52 | + } |
| 53 | + |
| 54 | + get onDidChangeSketchDirUri(): Event<URI | undefined> { |
| 55 | + return this.didChangeSketchDirUriEmitter.event; |
| 56 | + } |
| 57 | + |
| 58 | + get onDidChangeDataDirUri(): Event<URI | undefined> { |
| 59 | + return this.didChangeDataDirUriEmitter.event; |
| 60 | + } |
| 61 | + |
| 62 | + async fetchConfig(): Promise<ConfigState> { |
| 63 | + return this.delegate.getConfiguration(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * CLI config related error messages if any. |
| 68 | + */ |
| 69 | + tryGetMessages(): string[] | undefined { |
| 70 | + return this.config?.messages; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * `directories.user` |
| 75 | + */ |
| 76 | + tryGetSketchDirUri(): URI | undefined { |
| 77 | + return this.config?.config?.sketchDirUri |
| 78 | + ? new URI(this.config?.config?.sketchDirUri) |
| 79 | + : undefined; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * `directories.data` |
| 84 | + */ |
| 85 | + tryGetDataDirUri(): URI | undefined { |
| 86 | + return this.config?.config?.dataDirUri |
| 87 | + ? new URI(this.config?.config?.dataDirUri) |
| 88 | + : undefined; |
| 89 | + } |
| 90 | + |
| 91 | + private use(config: ConfigState): void { |
| 92 | + const oldConfig = deepClone(this.config); |
| 93 | + this.config = config; |
| 94 | + if (oldConfig?.config?.sketchDirUri !== this.config?.config?.sketchDirUri) { |
| 95 | + this.didChangeSketchDirUriEmitter.fire(this.tryGetSketchDirUri()); |
| 96 | + } |
| 97 | + if (oldConfig?.config?.dataDirUri !== this.config?.config?.dataDirUri) { |
| 98 | + this.didChangeDataDirUriEmitter.fire(this.tryGetDataDirUri()); |
| 99 | + } |
| 100 | + if (this.config.messages?.length) { |
| 101 | + const message = this.config.messages.join(' '); |
| 102 | + // toast the error later otherwise it might not show up in IDE2 |
| 103 | + setTimeout(() => this.messageService.error(message), 1_000); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments