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 2fbf3cb

Browse files
author
Akos Kitta
committedMay 27, 2022
Resolve and cache current sketch.
Signed-off-by: Akos Kitta <[email protected]>
1 parent b947be0 commit 2fbf3cb

24 files changed

+132
-71
lines changed
 

‎arduino-ide-extension/src/browser/arduino-frontend-contribution.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ import { ArduinoMenus } from './menu/arduino-menus';
6666
import { MonitorViewContribution } from './serial/monitor/monitor-view-contribution';
6767
import { ArduinoToolbar } from './toolbar/arduino-toolbar';
6868
import { ArduinoPreferences } from './arduino-preferences';
69-
import { SketchesServiceClientImpl } from '../common/protocol/sketches-service-client-impl';
69+
import {
70+
CurrentSketch,
71+
SketchesServiceClientImpl,
72+
} from '../common/protocol/sketches-service-client-impl';
7073
import { SaveAsSketch } from './contributions/save-as-sketch';
7174
import { IDEUpdaterDialog } from './dialogs/ide-updater/ide-updater-dialog';
7275
import { IDEUpdater } from '../common/protocol/ide-updater';
@@ -219,7 +222,10 @@ export class ArduinoFrontendContribution
219222
updateStatusBar(this.boardsServiceClientImpl.boardsConfig);
220223
this.appStateService.reachedState('ready').then(async () => {
221224
const sketch = await this.sketchServiceClient.currentSketch();
222-
if (sketch && !(await this.sketchService.isTemp(sketch))) {
225+
if (
226+
CurrentSketch.isValid(sketch) &&
227+
!(await this.sketchService.isTemp(sketch))
228+
) {
223229
this.toDisposeOnStop.push(this.fileService.watch(new URI(sketch.uri)));
224230
this.toDisposeOnStop.push(
225231
this.fileService.onDidFilesChange(async (event) => {
@@ -373,7 +379,7 @@ export class ArduinoFrontendContribution
373379
let currentSketchPath: string | undefined = undefined;
374380
if (log) {
375381
const currentSketch = await this.sketchServiceClient.currentSketch();
376-
if (currentSketch) {
382+
if (CurrentSketch.isValid(currentSketch)) {
377383
currentSketchPath = await this.fileService.fsPath(
378384
new URI(currentSketch.uri)
379385
);

‎arduino-ide-extension/src/browser/contributions/add-file.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from './contribution';
1111
import { FileDialogService } from '@theia/filesystem/lib/browser';
1212
import { nls } from '@theia/core/lib/common';
13+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1314

1415
@injectable()
1516
export class AddFile extends SketchContribution {
@@ -32,7 +33,7 @@ export class AddFile extends SketchContribution {
3233

3334
protected async addFile(): Promise<void> {
3435
const sketch = await this.sketchServiceClient.currentSketch();
35-
if (!sketch) {
36+
if (!CurrentSketch.isValid(sketch)) {
3637
return;
3738
}
3839
const toAddUri = await this.fileDialogService.showOpenDialog({

‎arduino-ide-extension/src/browser/contributions/archive-sketch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
MenuModelRegistry,
1111
} from './contribution';
1212
import { nls } from '@theia/core/lib/common';
13+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1314

1415
@injectable()
1516
export class ArchiveSketch extends SketchContribution {
@@ -32,7 +33,7 @@ export class ArchiveSketch extends SketchContribution {
3233
this.sketchServiceClient.currentSketch(),
3334
this.configService.getConfiguration(),
3435
]);
35-
if (!sketch) {
36+
if (!CurrentSketch.isValid(sketch)) {
3637
return;
3738
}
3839
const archiveBasename = `${sketch.name}-${dateFormat(

‎arduino-ide-extension/src/browser/contributions/close.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
URI,
1717
} from './contribution';
1818
import { nls } from '@theia/core/lib/common';
19+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1920

2021
/**
2122
* Closes the `current` closeable editor, or any closeable current widget from the main area, or the current sketch window.
@@ -54,7 +55,7 @@ export class Close extends SketchContribution {
5455

5556
// Close the sketch (window).
5657
const sketch = await this.sketchServiceClient.currentSketch();
57-
if (!sketch) {
58+
if (!CurrentSketch.isValid(sketch)) {
5859
return;
5960
}
6061
const isTemp = await this.sketchService.isTemp(sketch);

‎arduino-ide-extension/src/browser/contributions/contribution.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ import {
3939
} from '@theia/core/lib/common/command';
4040
import { EditorMode } from '../editor-mode';
4141
import { SettingsService } from '../dialogs/settings/settings';
42-
import { SketchesServiceClientImpl } from '../../common/protocol/sketches-service-client-impl';
42+
import {
43+
CurrentSketch,
44+
SketchesServiceClientImpl,
45+
} from '../../common/protocol/sketches-service-client-impl';
4346
import {
4447
SketchesService,
4548
ConfigService,
@@ -149,7 +152,7 @@ export abstract class SketchContribution extends Contribution {
149152
protected async sourceOverride(): Promise<Record<string, string>> {
150153
const override: Record<string, string> = {};
151154
const sketch = await this.sketchServiceClient.currentSketch();
152-
if (sketch) {
155+
if (CurrentSketch.isValid(sketch)) {
153156
for (const editor of this.editorManager.all) {
154157
const uri = editor.editor.uri;
155158
if (Saveable.isDirty(editor) && Sketch.isInSketch(uri, sketch)) {

‎arduino-ide-extension/src/browser/contributions/debug.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
TabBarToolbarRegistry,
1414
} from './contribution';
1515
import { MaybePromise, nls } from '@theia/core/lib/common';
16+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1617

1718
@injectable()
1819
export class Debug extends SketchContribution {
@@ -160,7 +161,7 @@ export class Debug extends SketchContribution {
160161
this.sketchServiceClient.currentSketch(),
161162
this.executableService.list(),
162163
]);
163-
if (!sketch) {
164+
if (!CurrentSketch.isValid(sketch)) {
164165
return;
165166
}
166167
const ideTempFolderUri = await this.sketchService.getIdeTempFolderUri(

‎arduino-ide-extension/src/browser/contributions/include-library.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { SketchContribution, Command, CommandRegistry } from './contribution';
1717
import { NotificationCenter } from '../notification-center';
1818
import { nls } from '@theia/core/lib/common';
1919
import * as monaco from '@theia/monaco-editor-core';
20+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
2021

2122
@injectable()
2223
export class IncludeLibrary extends SketchContribution {
@@ -172,7 +173,7 @@ export class IncludeLibrary extends SketchContribution {
172173

173174
protected async includeLibrary(library: LibraryPackage): Promise<void> {
174175
const sketch = await this.sketchServiceClient.currentSketch();
175-
if (!sketch) {
176+
if (!CurrentSketch.isValid(sketch)) {
176177
return;
177178
}
178179
// If the current editor is one of the additional files from the sketch, we use that.

‎arduino-ide-extension/src/browser/contributions/save-as-sketch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { nls } from '@theia/core/lib/common';
1414
import { ApplicationShell, NavigatableWidget, Saveable } from '@theia/core/lib/browser';
1515
import { EditorManager } from '@theia/editor/lib/browser';
1616
import { WindowService } from '@theia/core/lib/browser/window/window-service';
17+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1718

1819
@injectable()
1920
export class SaveAsSketch extends SketchContribution {
@@ -59,7 +60,7 @@ export class SaveAsSketch extends SketchContribution {
5960
}: SaveAsSketch.Options = SaveAsSketch.Options.DEFAULT
6061
): Promise<boolean> {
6162
const sketch = await this.sketchServiceClient.currentSketch();
62-
if (!sketch) {
63+
if (!CurrentSketch.isValid(sketch)) {
6364
return false;
6465
}
6566

‎arduino-ide-extension/src/browser/contributions/save-sketch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
TabBarToolbarRegistry,
1313
} from './contribution';
1414
import { nls } from '@theia/core/lib/common';
15+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1516

1617
@injectable()
1718
export class SaveSketch extends SketchContribution {
@@ -53,7 +54,7 @@ export class SaveSketch extends SketchContribution {
5354

5455
async saveSketch(): Promise<void> {
5556
const sketch = await this.sketchServiceClient.currentSketch();
56-
if (!sketch) {
57+
if (!CurrentSketch.isValid(sketch)) {
5758
return;
5859
}
5960
const isTemp = await this.sketchService.isTemp(sketch);

‎arduino-ide-extension/src/browser/contributions/sketch-control.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {
1919
} from './contribution';
2020
import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus';
2121
import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
22-
import { SketchesServiceClientImpl } from '../../common/protocol/sketches-service-client-impl';
22+
import {
23+
CurrentSketch,
24+
SketchesServiceClientImpl,
25+
} from '../../common/protocol/sketches-service-client-impl';
2326
import { LocalCacheFsProvider } from '../local-cache/local-cache-fs-provider';
2427
import { nls } from '@theia/core/lib/common';
2528

@@ -55,7 +58,7 @@ export class SketchControl extends SketchContribution {
5558
execute: async () => {
5659
this.toDisposeBeforeCreateNewContextMenu.dispose();
5760
const sketch = await this.sketchServiceClient.currentSketch();
58-
if (!sketch) {
61+
if (!CurrentSketch.isValid(sketch)) {
5962
return;
6063
}
6164

@@ -70,25 +73,22 @@ export class SketchControl extends SketchContribution {
7073
return;
7174
}
7275

73-
const { mainFileUri, rootFolderFileUris } =
74-
await this.sketchService.loadSketch(sketch.uri);
76+
const { mainFileUri, rootFolderFileUris } = sketch;
7577
const uris = [mainFileUri, ...rootFolderFileUris];
7678

77-
const currentSketch =
78-
await this.sketchesServiceClient.currentSketch();
79-
const parentsketchUri = this.editorManager.currentEditor
79+
const parentSketchUri = this.editorManager.currentEditor
8080
?.getResourceUri()
8181
?.toString();
82-
const parentsketch = await this.sketchService.getSketchFolder(
83-
parentsketchUri || ''
82+
const parentSketch = await this.sketchService.getSketchFolder(
83+
parentSketchUri || ''
8484
);
8585

8686
// if the current file is in the current opened sketch, show extra menus
8787
if (
88-
currentSketch &&
89-
parentsketch &&
90-
parentsketch.uri === currentSketch.uri &&
91-
this.allowRename(parentsketch.uri)
88+
sketch &&
89+
parentSketch &&
90+
parentSketch.uri === sketch.uri &&
91+
this.allowRename(parentSketch.uri)
9292
) {
9393
this.menuRegistry.registerMenuAction(
9494
ArduinoMenus.SKETCH_CONTROL__CONTEXT__MAIN_GROUP,
@@ -122,10 +122,10 @@ export class SketchControl extends SketchContribution {
122122
}
123123

124124
if (
125-
currentSketch &&
126-
parentsketch &&
127-
parentsketch.uri === currentSketch.uri &&
128-
this.allowDelete(parentsketch.uri)
125+
sketch &&
126+
parentSketch &&
127+
parentSketch.uri === sketch.uri &&
128+
this.allowDelete(parentSketch.uri)
129129
) {
130130
this.menuRegistry.registerMenuAction(
131131
ArduinoMenus.SKETCH_CONTROL__CONTEXT__MAIN_GROUP,

‎arduino-ide-extension/src/browser/contributions/upload-sketch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from './contribution';
1717
import { UserFieldsDialog } from '../dialogs/user-fields/user-fields-dialog';
1818
import { DisposableCollection, nls } from '@theia/core/lib/common';
19+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1920

2021
@injectable()
2122
export class UploadSketch extends SketchContribution {
@@ -209,7 +210,7 @@ export class UploadSketch extends SketchContribution {
209210
this.uploadInProgress = true;
210211
this.onDidChangeEmitter.fire();
211212
const sketch = await this.sketchServiceClient.currentSketch();
212-
if (!sketch) {
213+
if (!CurrentSketch.isValid(sketch)) {
213214
return;
214215
}
215216

‎arduino-ide-extension/src/browser/contributions/verify-sketch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
TabBarToolbarRegistry,
1515
} from './contribution';
1616
import { nls } from '@theia/core/lib/common';
17+
import { CurrentSketch } from '../../common/protocol/sketches-service-client-impl';
1718

1819
@injectable()
1920
export class VerifySketch extends SketchContribution {
@@ -99,7 +100,7 @@ export class VerifySketch extends SketchContribution {
99100
this.onDidChangeEmitter.fire();
100101
const sketch = await this.sketchServiceClient.currentSketch();
101102

102-
if (!sketch) {
103+
if (!CurrentSketch.isValid(sketch)) {
103104
return;
104105
}
105106
try {

‎arduino-ide-extension/src/browser/theia/core/application-shell.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from '@theia/core/lib/browser';
1616
import { Sketch } from '../../../common/protocol';
1717
import { SaveAsSketch } from '../../contributions/save-as-sketch';
18-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
18+
import { CurrentSketch, SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
1919
import { nls } from '@theia/core/lib/common';
2020
import URI from '@theia/core/lib/common/uri';
2121

@@ -41,7 +41,7 @@ export class ApplicationShell extends TheiaApplicationShell {
4141
if (widget instanceof EditorWidget) {
4242
// Make the editor un-closeable asynchronously.
4343
this.sketchesServiceClient.currentSketch().then((sketch) => {
44-
if (sketch) {
44+
if (CurrentSketch.isValid(sketch)) {
4545
if (!this.isSketchFile(widget.editor.uri, sketch.uri)) {
4646
return;
4747
}

‎arduino-ide-extension/src/browser/theia/debug/debug-configuration-manager.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { DebugConfiguration } from '@theia/debug/lib/common/debug-common';
77
import { DebugConfigurationModel as TheiaDebugConfigurationModel } from '@theia/debug/lib/browser/debug-configuration-model';
88
import { DebugConfigurationManager as TheiaDebugConfigurationManager } from '@theia/debug/lib/browser/debug-configuration-manager';
99
import { SketchesService } from '../../../common/protocol';
10-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
10+
import {
11+
CurrentSketch,
12+
SketchesServiceClientImpl,
13+
} from '../../../common/protocol/sketches-service-client-impl';
1114
import { DebugConfigurationModel } from './debug-configuration-model';
1215
import {
1316
FileOperationError,
@@ -113,7 +116,7 @@ export class DebugConfigurationManager extends TheiaDebugConfigurationManager {
113116
(TheiaDebugConfigurationModel.JsonContent & { uri: URI }) | URI | undefined
114117
> {
115118
const sketch = await this.sketchesServiceClient.currentSketch();
116-
if (!sketch) {
119+
if (!CurrentSketch.isValid(sketch)) {
117120
return undefined;
118121
}
119122
const uri = await this.sketchesService.getIdeTempFolderUri(sketch);

‎arduino-ide-extension/src/browser/theia/editor/editor-widget-factory.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import URI from '@theia/core/lib/common/uri';
33
import { EditorWidget } from '@theia/editor/lib/browser';
44
import { LabelProvider } from '@theia/core/lib/browser';
55
import { EditorWidgetFactory as TheiaEditorWidgetFactory } from '@theia/editor/lib/browser/editor-widget-factory';
6-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
6+
import {
7+
CurrentSketch,
8+
SketchesServiceClientImpl,
9+
} from '../../../common/protocol/sketches-service-client-impl';
710
import { SketchesService, Sketch } from '../../../common/protocol';
811
import { nls } from '@theia/core/lib/common';
912

@@ -28,7 +31,7 @@ export class EditorWidgetFactory extends TheiaEditorWidgetFactory {
2831
): Promise<EditorWidget> {
2932
const sketch = await this.sketchesServiceClient.currentSketch();
3033
const { uri } = widget.editor;
31-
if (sketch && Sketch.isInSketch(uri, sketch)) {
34+
if (CurrentSketch.isValid(sketch) && Sketch.isInSketch(uri, sketch)) {
3235
const isTemp = await this.sketchesService.isTemp(sketch);
3336
if (isTemp) {
3437
widget.title.caption = nls.localize(

‎arduino-ide-extension/src/browser/theia/workspace/workspace-commands.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import {
1212
} from '@theia/workspace/lib/browser/workspace-commands';
1313
import { Sketch, SketchesService } from '../../../common/protocol';
1414
import { WorkspaceInputDialog } from './workspace-input-dialog';
15-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
15+
import {
16+
CurrentSketch,
17+
SketchesServiceClientImpl,
18+
} from '../../../common/protocol/sketches-service-client-impl';
1619
import { SaveAsSketch } from '../../contributions/save-as-sketch';
1720
import { SingleTextInputDialog } from '@theia/core/lib/browser';
1821
import { nls } from '@theia/core/lib/common';
@@ -129,15 +132,15 @@ export class WorkspaceCommandContribution extends TheiaWorkspaceCommandContribut
129132
return;
130133
}
131134
const sketch = await this.sketchesServiceClient.currentSketch();
132-
if (!sketch) {
135+
if (!CurrentSketch.isValid(sketch)) {
133136
return;
134137
}
135138

136139
// file belongs to another sketch, do not allow rename
137-
const parentsketch = await this.sketchService.getSketchFolder(
140+
const parentSketch = await this.sketchService.getSketchFolder(
138141
uri.toString()
139142
);
140-
if (parentsketch && parentsketch.uri !== sketch.uri) {
143+
if (parentSketch && parentSketch.uri !== sketch.uri) {
141144
return;
142145
}
143146

‎arduino-ide-extension/src/browser/theia/workspace/workspace-delete-handler.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { inject, injectable } from '@theia/core/shared/inversify';
22
import * as remote from '@theia/core/electron-shared/@electron/remote';
33
import URI from '@theia/core/lib/common/uri';
44
import { WorkspaceDeleteHandler as TheiaWorkspaceDeleteHandler } from '@theia/workspace/lib/browser/workspace-delete-handler';
5-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
5+
import {
6+
CurrentSketch,
7+
SketchesServiceClientImpl,
8+
} from '../../../common/protocol/sketches-service-client-impl';
69
import { nls } from '@theia/core/lib/common';
710

811
@injectable()
@@ -12,7 +15,7 @@ export class WorkspaceDeleteHandler extends TheiaWorkspaceDeleteHandler {
1215

1316
override async execute(uris: URI[]): Promise<void> {
1417
const sketch = await this.sketchesServiceClient.currentSketch();
15-
if (!sketch) {
18+
if (!CurrentSketch.isValid(sketch)) {
1619
return;
1720
}
1821
// Deleting the main sketch file.

‎arduino-ide-extension/src/browser/theia/workspace/workspace-variable-contribution.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
1+
import {
2+
inject,
3+
injectable,
4+
postConstruct,
5+
} from '@theia/core/shared/inversify';
26
import URI from '@theia/core/lib/common/uri';
37
import { WorkspaceVariableContribution as TheiaWorkspaceVariableContribution } from '@theia/workspace/lib/browser/workspace-variable-contribution';
48
import { Sketch } from '../../../common/protocol';
5-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
9+
import {
10+
CurrentSketch,
11+
SketchesServiceClientImpl,
12+
} from '../../../common/protocol/sketches-service-client-impl';
613

714
@injectable()
815
export class WorkspaceVariableContribution extends TheiaWorkspaceVariableContribution {
@@ -13,10 +20,11 @@ export class WorkspaceVariableContribution extends TheiaWorkspaceVariableContrib
1320

1421
@postConstruct()
1522
protected override init(): void {
16-
this.sketchesServiceClient
17-
.currentSketch()
18-
.then()
19-
.then((sketch) => (this.currentSketch = sketch));
23+
this.sketchesServiceClient.currentSketch().then((sketch) => {
24+
if (CurrentSketch.isValid(sketch)) {
25+
this.currentSketch = sketch;
26+
}
27+
});
2028
}
2129

2230
override getResourceUri(): URI | undefined {

‎arduino-ide-extension/src/browser/widgets/cloud-sketchbook/cloud-sketchbook-contributions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from '@theia/core/lib/browser/preferences/preference-service';
2424
import { ArduinoMenus, PlaceholderMenuNode } from '../../menu/arduino-menus';
2525
import { SketchbookCommands } from '../sketchbook/sketchbook-commands';
26-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
26+
import { CurrentSketch, SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
2727
import { Contribution } from '../../contributions/contribution';
2828
import { ArduinoPreferences } from '../../arduino-preferences';
2929
import { MainMenuManager } from '../../../common/main-menu-manager';
@@ -279,7 +279,8 @@ export class CloudSketchbookContribution extends Contribution {
279279
// disable the "open sketch" command for the current sketch and for those not in sync
280280
if (
281281
!CloudSketchbookTree.CloudSketchTreeNode.isSynced(arg.node) ||
282-
(currentSketch && currentSketch.uri === arg.node.uri.toString())
282+
(CurrentSketch.isValid(currentSketch) &&
283+
currentSketch.uri === arg.node.uri.toString())
283284
) {
284285
const placeholder = new PlaceholderMenuNode(
285286
SKETCHBOOKSYNC__CONTEXT__MAIN_GROUP,

‎arduino-ide-extension/src/browser/widgets/sketchbook/sketchbook-tree-model.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@theia/core/lib/browser/tree';
1414
import { SketchbookCommands } from './sketchbook-commands';
1515
import { OpenerService, open } from '@theia/core/lib/browser';
16-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
16+
import { CurrentSketch, SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
1717
import { CommandRegistry } from '@theia/core/lib/common/command';
1818
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
1919
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
@@ -294,7 +294,10 @@ export class SketchbookTreeModel extends FileTreeModel {
294294

295295
// check if the node is a file that belongs to another sketch
296296
const sketch = await this.sketchServiceClient.currentSketch();
297-
if (sketch && node.uri.toString().indexOf(sketch.uri) !== 0) {
297+
if (
298+
CurrentSketch.isValid(sketch) &&
299+
node.uri.toString().indexOf(sketch.uri) !== 0
300+
) {
298301
return false;
299302
}
300303
return true;

‎arduino-ide-extension/src/browser/widgets/sketchbook/sketchbook-tree-widget.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import { ContextMenuRenderer } from '@theia/core/lib/browser/context-menu-render
1414
import { SketchbookTree } from './sketchbook-tree';
1515
import { SketchbookTreeModel } from './sketchbook-tree-model';
1616
import { ArduinoPreferences } from '../../arduino-preferences';
17-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
17+
import {
18+
CurrentSketch,
19+
SketchesServiceClientImpl,
20+
} from '../../../common/protocol/sketches-service-client-impl';
1821
import { SelectableTreeNode } from '@theia/core/lib/browser/tree/tree-selection';
1922
import { Sketch } from '../../contributions/contribution';
2023
import { nls } from '@theia/core/lib/common';
@@ -54,7 +57,7 @@ export class SketchbookTreeWidget extends FileTreeWidget {
5457
super.init();
5558
// cache the current open sketch uri
5659
const currentSketch = await this.sketchServiceClient.currentSketch();
57-
this.currentSketchUri = (currentSketch && currentSketch.uri) || '';
60+
this.currentSketchUri = (CurrentSketch.isValid(currentSketch) && currentSketch.uri) || '';
5861
}
5962

6063
protected override createNodeClassNames(node: TreeNode, props: NodeProps): string[] {

‎arduino-ide-extension/src/browser/widgets/sketchbook/sketchbook-widget-contribution.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import {
2323
Disposable,
2424
DisposableCollection,
2525
} from '@theia/core/lib/common/disposable';
26-
import { SketchesServiceClientImpl } from '../../../common/protocol/sketches-service-client-impl';
26+
import {
27+
CurrentSketch,
28+
SketchesServiceClientImpl,
29+
} from '../../../common/protocol/sketches-service-client-impl';
2730
import { FileService } from '@theia/filesystem/lib/browser/file-service';
2831
import { URI } from '../../contributions/contribution';
2932

@@ -142,7 +145,10 @@ export class SketchbookWidgetContribution
142145
// disable the "open sketch" command for the current sketch.
143146
// otherwise make the command clickable
144147
const currentSketch = await this.sketchServiceClient.currentSketch();
145-
if (currentSketch && currentSketch.uri === arg.node.uri.toString()) {
148+
if (
149+
CurrentSketch.isValid(currentSketch) &&
150+
currentSketch.uri === arg.node.uri.toString()
151+
) {
146152
const placeholder = new PlaceholderMenuNode(
147153
SKETCHBOOK__CONTEXT__MAIN_GROUP,
148154
SketchbookCommands.OPEN_NEW_WINDOW.label!

‎arduino-ide-extension/src/common/protocol/sketches-service-client-impl.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ import {
1616
REMOTE_SKETCHBOOK_FOLDER,
1717
} from '../../browser/utils/constants';
1818
import * as monaco from '@theia/monaco-editor-core';
19+
import { Deferred } from '@theia/core/lib/common/promise-util';
1920

2021
const READ_ONLY_FILES = ['sketch.json'];
2122
const READ_ONLY_FILES_REMOTE = ['thingProperties.h', 'thingsProperties.h'];
2223

24+
export type CurrentSketch = Sketch | 'invalid';
25+
export namespace CurrentSketch {
26+
export function isValid(arg: CurrentSketch | undefined): arg is Sketch {
27+
return !!arg && arg !== 'invalid';
28+
}
29+
}
30+
2331
@injectable()
2432
export class SketchesServiceClientImpl
2533
implements FrontendApplicationContribution
@@ -41,12 +49,15 @@ export class SketchesServiceClientImpl
4149

4250
protected toDispose = new DisposableCollection();
4351
protected sketches = new Map<string, SketchRef>();
52+
// TODO: rename this + event to the `onBlabla` pattern
4453
protected sketchbookDidChangeEmitter = new Emitter<{
4554
created: SketchRef[];
4655
removed: SketchRef[];
4756
}>();
4857
readonly onSketchbookDidChange = this.sketchbookDidChangeEmitter.event;
4958

59+
private _currentSketch = new Deferred<CurrentSketch>();
60+
5061
onStart(): void {
5162
this.configService.getConfiguration().then(({ sketchDirUri }) => {
5263
this.sketchService
@@ -99,13 +110,16 @@ export class SketchesServiceClientImpl
99110
);
100111
});
101112
});
113+
this.loadCurrentSketch().then((currentSketch) =>
114+
this._currentSketch.resolve(currentSketch)
115+
);
102116
}
103117

104118
onStop(): void {
105119
this.toDispose.dispose();
106120
}
107121

108-
async currentSketch(): Promise<Sketch | undefined> {
122+
private async loadCurrentSketch(): Promise<CurrentSketch> {
109123
const sketches = (
110124
await Promise.all(
111125
this.workspaceService
@@ -116,7 +130,7 @@ export class SketchesServiceClientImpl
116130
)
117131
).filter(notEmpty);
118132
if (!sketches.length) {
119-
return undefined;
133+
return 'invalid';
120134
}
121135
if (sketches.length > 1) {
122136
console.log(
@@ -128,16 +142,14 @@ export class SketchesServiceClientImpl
128142
return sketches[0];
129143
}
130144

145+
async currentSketch(): Promise<CurrentSketch> {
146+
return this._currentSketch.promise;
147+
}
148+
131149
async currentSketchFile(): Promise<string | undefined> {
132-
const sketch = await this.currentSketch();
133-
if (sketch) {
134-
const uri = sketch.mainFileUri;
135-
const exists = await this.fileService.exists(new URI(uri));
136-
if (!exists) {
137-
this.messageService.warn(`Could not find sketch file: ${uri}`);
138-
return undefined;
139-
}
140-
return uri;
150+
const currentSketch = await this.currentSketch();
151+
if (CurrentSketch.isValid(currentSketch)) {
152+
return currentSketch.mainFileUri;
141153
}
142154
return undefined;
143155
}

‎arduino-ide-extension/src/node/board-discovery.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ export class BoardDiscovery extends CoreClientAware {
5555

5656
@postConstruct()
5757
protected async init(): Promise<void> {
58-
await this.coreClientProvider.initialized;
59-
const coreClient = await this.coreClient();
60-
this.startBoardListWatch(coreClient);
58+
this.coreClient().then((client) => this.startBoardListWatch(client));
6159
}
6260

6361
stopBoardListWatch(coreClient: CoreClientProvider.Client): Promise<void> {

0 commit comments

Comments
 (0)
Please sign in to comment.