@@ -21,23 +21,33 @@ import {
21
21
MenuModelRegistry ,
22
22
} from './contribution' ;
23
23
import { NotificationCenter } from '../notification-center' ;
24
- import { Board , SketchRef , SketchContainer } from '../../common/protocol' ;
24
+ import {
25
+ Board ,
26
+ SketchRef ,
27
+ SketchContainer ,
28
+ SketchesError ,
29
+ Sketch ,
30
+ CoreService ,
31
+ } from '../../common/protocol' ;
25
32
import { nls } from '@theia/core/lib/common' ;
26
33
27
34
@injectable ( )
28
35
export abstract class Examples extends SketchContribution {
29
36
@inject ( CommandRegistry )
30
- protected readonly commandRegistry : CommandRegistry ;
37
+ private readonly commandRegistry : CommandRegistry ;
31
38
32
39
@inject ( MenuModelRegistry )
33
- protected readonly menuRegistry : MenuModelRegistry ;
40
+ private readonly menuRegistry : MenuModelRegistry ;
34
41
35
42
@inject ( MainMenuManager )
36
43
protected readonly menuManager : MainMenuManager ;
37
44
38
45
@inject ( ExamplesService )
39
46
protected readonly examplesService : ExamplesService ;
40
47
48
+ @inject ( CoreService )
49
+ protected readonly coreService : CoreService ;
50
+
41
51
@inject ( BoardsServiceProvider )
42
52
protected readonly boardsServiceClient : BoardsServiceProvider ;
43
53
@@ -50,10 +60,16 @@ export abstract class Examples extends SketchContribution {
50
60
) ;
51
61
}
52
62
63
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, unused-imports/no-unused-vars
53
64
protected handleBoardChanged ( board : Board | undefined ) : void {
54
65
// NOOP
55
66
}
56
67
68
+ protected abstract update ( options ?: {
69
+ board ?: Board | undefined ;
70
+ forceRefresh ?: boolean ;
71
+ } ) : void ;
72
+
57
73
override registerMenus ( registry : MenuModelRegistry ) : void {
58
74
try {
59
75
// This is a hack the ensures the desired menu ordering! We cannot use https://github.com/eclipse-theia/theia/pull/8377 due to ATL-222.
@@ -149,23 +165,54 @@ export abstract class Examples extends SketchContribution {
149
165
protected createHandler ( uri : string ) : CommandHandler {
150
166
return {
151
167
execute : async ( ) => {
152
- const sketch = await this . sketchService . cloneExample ( uri ) ;
153
- return this . commandService . executeCommand (
154
- OpenSketch . Commands . OPEN_SKETCH . id ,
155
- sketch
156
- ) ;
168
+ const sketch = await this . clone ( uri ) ;
169
+ if ( sketch ) {
170
+ try {
171
+ return this . commandService . executeCommand (
172
+ OpenSketch . Commands . OPEN_SKETCH . id ,
173
+ sketch
174
+ ) ;
175
+ } catch ( err ) {
176
+ if ( SketchesError . NotFound . is ( err ) ) {
177
+ // Do not toast the error message. It's handled by the `Open Sketch` command.
178
+ this . update ( {
179
+ board : this . boardsServiceClient . boardsConfig . selectedBoard ,
180
+ forceRefresh : true ,
181
+ } ) ;
182
+ } else {
183
+ throw err ;
184
+ }
185
+ }
186
+ }
157
187
} ,
158
188
} ;
159
189
}
190
+
191
+ private async clone ( uri : string ) : Promise < Sketch | undefined > {
192
+ try {
193
+ const sketch = await this . sketchService . cloneExample ( uri ) ;
194
+ return sketch ;
195
+ } catch ( err ) {
196
+ if ( SketchesError . NotFound . is ( err ) ) {
197
+ this . messageService . error ( err . message ) ;
198
+ this . update ( {
199
+ board : this . boardsServiceClient . boardsConfig . selectedBoard ,
200
+ forceRefresh : true ,
201
+ } ) ;
202
+ } else {
203
+ throw err ;
204
+ }
205
+ }
206
+ }
160
207
}
161
208
162
209
@injectable ( )
163
210
export class BuiltInExamples extends Examples {
164
211
override async onReady ( ) : Promise < void > {
165
- this . register ( ) ; // no `await`
212
+ this . update ( ) ; // no `await`
166
213
}
167
214
168
- protected async register ( ) : Promise < void > {
215
+ protected override async update ( ) : Promise < void > {
169
216
let sketchContainers : SketchContainer [ ] | undefined ;
170
217
try {
171
218
sketchContainers = await this . examplesService . builtIns ( ) ;
@@ -197,29 +244,34 @@ export class BuiltInExamples extends Examples {
197
244
@injectable ( )
198
245
export class LibraryExamples extends Examples {
199
246
@inject ( NotificationCenter )
200
- protected readonly notificationCenter : NotificationCenter ;
247
+ private readonly notificationCenter : NotificationCenter ;
201
248
202
- protected readonly queue = new PQueue ( { autoStart : true , concurrency : 1 } ) ;
249
+ private readonly queue = new PQueue ( { autoStart : true , concurrency : 1 } ) ;
203
250
204
251
override onStart ( ) : void {
205
- this . notificationCenter . onLibraryDidInstall ( ( ) => this . register ( ) ) ;
206
- this . notificationCenter . onLibraryDidUninstall ( ( ) => this . register ( ) ) ;
252
+ this . notificationCenter . onLibraryDidInstall ( ( ) => this . update ( ) ) ;
253
+ this . notificationCenter . onLibraryDidUninstall ( ( ) => this . update ( ) ) ;
207
254
}
208
255
209
256
override async onReady ( ) : Promise < void > {
210
- this . register ( ) ; // no `await`
257
+ this . update ( ) ; // no `await`
211
258
}
212
259
213
260
protected override handleBoardChanged ( board : Board | undefined ) : void {
214
- this . register ( board ) ;
261
+ this . update ( { board } ) ;
215
262
}
216
263
217
- protected async register (
218
- board : Board | undefined = this . boardsServiceClient . boardsConfig
219
- . selectedBoard
264
+ protected override async update (
265
+ options : { board ?: Board ; forceRefresh ?: boolean } = {
266
+ board : this . boardsServiceClient . boardsConfig . selectedBoard ,
267
+ }
220
268
) : Promise < void > {
269
+ const { board, forceRefresh } = options ;
221
270
return this . queue . add ( async ( ) => {
222
271
this . toDispose . dispose ( ) ;
272
+ if ( forceRefresh ) {
273
+ await this . coreService . refresh ( ) ;
274
+ }
223
275
const fqbn = board ?. fqbn ;
224
276
const name = board ?. name ;
225
277
// Shows all examples when no board is selected, or the platform of the currently selected board is not installed.
0 commit comments