|
| 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 { Deferred } from '@theia/core/lib/common/promise-util'; |
| 7 | +import URI from '@theia/core/lib/common/uri'; |
| 8 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 9 | +import { Config, ConfigService, ConfigState } from '../../common/protocol'; |
| 10 | +import { NotificationCenter } from '../notification-center'; |
| 11 | + |
| 12 | +@injectable() |
| 13 | +export class ConfigServiceClient implements FrontendApplicationContribution { |
| 14 | + @inject(ConfigService) |
| 15 | + private readonly delegate: ConfigService; |
| 16 | + @inject(NotificationCenter) |
| 17 | + private readonly notificationCenter: NotificationCenter; |
| 18 | + @inject(FrontendApplicationStateService) |
| 19 | + private readonly appStateService: FrontendApplicationStateService; |
| 20 | + @inject(MessageService) |
| 21 | + private readonly messageService: MessageService; |
| 22 | + |
| 23 | + private readonly didChangeSketchDirUriEmitter = new Emitter< |
| 24 | + URI | undefined |
| 25 | + >(); |
| 26 | + private readonly didChangeDataDirUriEmitter = new Emitter<URI | undefined>(); |
| 27 | + private readonly toDispose = new DisposableCollection( |
| 28 | + this.didChangeSketchDirUriEmitter, |
| 29 | + this.didChangeDataDirUriEmitter |
| 30 | + ); |
| 31 | + |
| 32 | + private configFileUri: Deferred<URI> | undefined; |
| 33 | + private _config: ConfigState | undefined; |
| 34 | + private _dataDirUri: string | undefined; |
| 35 | + private _sketchDirUri: string | undefined; |
| 36 | + |
| 37 | + onStart(): void { |
| 38 | + this.appStateService.reachedState('ready').then(async () => { |
| 39 | + const config = await this.fetchConfig(); |
| 40 | + this.use(config); |
| 41 | + }); |
| 42 | + this.notificationCenter.onConfigDidChange((config) => this.use(config)); |
| 43 | + } |
| 44 | + |
| 45 | + onStop(): void { |
| 46 | + this.toDispose.dispose(); |
| 47 | + } |
| 48 | + |
| 49 | + get onDidChangeSketchDirUri(): Event<URI | undefined> { |
| 50 | + return this.didChangeSketchDirUriEmitter.event; |
| 51 | + } |
| 52 | + |
| 53 | + get onDidChangeDataDirUri(): Event<URI | undefined> { |
| 54 | + return this.didChangeSketchDirUriEmitter.event; |
| 55 | + } |
| 56 | + |
| 57 | + async getCliConfigFileUri(): Promise<URI> { |
| 58 | + if (!this.configFileUri) { |
| 59 | + this.configFileUri = new Deferred(); |
| 60 | + setTimeout(async () => { |
| 61 | + try { |
| 62 | + const uri = await this.delegate.configFileUri(); |
| 63 | + this.configFileUri?.resolve(new URI(uri)); |
| 64 | + } catch (err) { |
| 65 | + console.error( |
| 66 | + `Could not retrieve the URI of the CLI configuration file`, |
| 67 | + err |
| 68 | + ); |
| 69 | + this.configFileUri?.reject(err); |
| 70 | + this.configFileUri = undefined; |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + return this.configFileUri.promise; |
| 75 | + } |
| 76 | + |
| 77 | + async fetchConfig(): Promise<ConfigState> { |
| 78 | + return this.delegate.config(); |
| 79 | + } |
| 80 | + |
| 81 | + tryGetConfig(): Config | undefined { |
| 82 | + return this._config?.config; |
| 83 | + } |
| 84 | + |
| 85 | + tryGetMessages(): string[] | undefined { |
| 86 | + return this._config?.messages; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * `directories.user` |
| 91 | + */ |
| 92 | + tryGetSketchDirUri(): URI | undefined { |
| 93 | + return this._sketchDirUri ? new URI(this._sketchDirUri) : undefined; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * `directories.data` |
| 98 | + */ |
| 99 | + tryGetDataDirUri(): URI | undefined { |
| 100 | + return this._dataDirUri ? new URI(this._dataDirUri) : undefined; |
| 101 | + } |
| 102 | + |
| 103 | + private use(config: ConfigState): void { |
| 104 | + this._config = config; |
| 105 | + if (this._dataDirUri !== this._config?.config?.dataDirUri) { |
| 106 | + this._dataDirUri = this._config?.config?.dataDirUri; |
| 107 | + this.didChangeDataDirUriEmitter.fire( |
| 108 | + this._dataDirUri ? new URI(this._dataDirUri) : undefined |
| 109 | + ); |
| 110 | + } |
| 111 | + if (this._sketchDirUri !== this._config?.config?.sketchDirUri) { |
| 112 | + this._sketchDirUri = this._config?.config?.sketchDirUri; |
| 113 | + this.didChangeSketchDirUriEmitter.fire( |
| 114 | + this._sketchDirUri ? new URI(this._sketchDirUri) : undefined |
| 115 | + ); |
| 116 | + } |
| 117 | + if (this._config.messages?.length) { |
| 118 | + this.messageService.error(this._config.messages.join(' ')); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments