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