Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2d8267d

Browse files
author
Akos Kitta
committedDec 15, 2022
feat: configure sketchbook location without restart
Closes #1764 Signed-off-by: Akos Kitta <[email protected]>
1 parent 1d342cd commit 2d8267d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+827
-470
lines changed
 

‎arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
SketchesService,
2424
SketchesServicePath,
2525
} from '../common/protocol/sketches-service';
26-
import { SketchesServiceClientImpl } from '../common/protocol/sketches-service-client-impl';
26+
import { SketchesServiceClientImpl } from './sketches-service-client-impl';
2727
import { CoreService, CoreServicePath } from '../common/protocol/core-service';
2828
import { BoardsListWidget } from './boards/boards-list-widget';
2929
import { BoardsListWidgetFrontendContribution } from './boards/boards-widget-frontend-contribution';
@@ -343,6 +343,7 @@ import { DebugWidget } from '@theia/debug/lib/browser/view/debug-widget';
343343
import { DebugViewModel } from '@theia/debug/lib/browser/view/debug-view-model';
344344
import { DebugSessionWidget } from '@theia/debug/lib/browser/view/debug-session-widget';
345345
import { DebugConfigurationWidget } from '@theia/debug/lib/browser/view/debug-configuration-widget';
346+
import { ConfigServiceClient } from './config/config-service-client';
346347

347348
export default new ContainerModule((bind, unbind, isBound, rebind) => {
348349
// Commands and toolbar items
@@ -404,6 +405,8 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
404405
)
405406
)
406407
.inSingletonScope();
408+
bind(ConfigServiceClient).toSelf().inSingletonScope();
409+
bind(FrontendApplicationContribution).toService(ConfigServiceClient);
407410

408411
// Boards service
409412
bind(BoardsService)
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)
Please sign in to comment.