1
1
import * as PQueue from 'p-queue' ;
2
2
import { inject , injectable } from '@theia/core/shared/inversify' ;
3
- import { CommandHandler } from '@theia/core/lib/common/command' ;
3
+ import { CommandHandler , CommandService } from '@theia/core/lib/common/command' ;
4
4
import {
5
5
MenuPath ,
6
6
CompositeMenuNode ,
@@ -11,7 +11,11 @@ import {
11
11
DisposableCollection ,
12
12
} from '@theia/core/lib/common/disposable' ;
13
13
import { OpenSketch } from './open-sketch' ;
14
- import { ArduinoMenus , PlaceholderMenuNode } from '../menu/arduino-menus' ;
14
+ import {
15
+ ArduinoMenus ,
16
+ examplesLabel ,
17
+ PlaceholderMenuNode ,
18
+ } from '../menu/arduino-menus' ;
15
19
import { BoardsServiceProvider } from '../boards/boards-service-provider' ;
16
20
import { ExamplesService } from '../../common/protocol/examples-service' ;
17
21
import {
@@ -25,11 +29,73 @@ import {
25
29
SketchRef ,
26
30
SketchContainer ,
27
31
SketchesError ,
28
- Sketch ,
29
32
CoreService ,
33
+ SketchesService ,
34
+ Sketch ,
30
35
} from '../../common/protocol' ;
31
- import { nls } from '@theia/core/lib/common' ;
36
+ import { nls } from '@theia/core/lib/common/nls ' ;
32
37
import { unregisterSubmenu } from '../menu/arduino-menus' ;
38
+ import { MaybePromise } from '@theia/core/lib/common/types' ;
39
+ import { ApplicationError } from '@theia/core/lib/common/application-error' ;
40
+
41
+ /**
42
+ * Creates a cloned copy of the example sketch and opens it in a new window.
43
+ */
44
+ export async function openClonedExample (
45
+ uri : string ,
46
+ services : {
47
+ sketchesService : SketchesService ;
48
+ commandService : CommandService ;
49
+ } ,
50
+ onError : {
51
+ onDidFailClone ?: (
52
+ err : ApplicationError <
53
+ number ,
54
+ {
55
+ uri : string ;
56
+ }
57
+ > ,
58
+ uri : string
59
+ ) => MaybePromise < unknown > ;
60
+ onDidFailOpen ?: (
61
+ err : ApplicationError <
62
+ number ,
63
+ {
64
+ uri : string ;
65
+ }
66
+ > ,
67
+ sketch : Sketch
68
+ ) => MaybePromise < unknown > ;
69
+ } = { }
70
+ ) : Promise < void > {
71
+ const { sketchesService, commandService } = services ;
72
+ const { onDidFailClone, onDidFailOpen } = onError ;
73
+ try {
74
+ const sketch = await sketchesService . cloneExample ( uri ) ;
75
+ try {
76
+ await commandService . executeCommand (
77
+ OpenSketch . Commands . OPEN_SKETCH . id ,
78
+ sketch
79
+ ) ;
80
+ } catch ( openError ) {
81
+ if ( SketchesError . NotFound . is ( openError ) ) {
82
+ if ( onDidFailOpen ) {
83
+ await onDidFailOpen ( openError , sketch ) ;
84
+ return ;
85
+ }
86
+ }
87
+ throw openError ;
88
+ }
89
+ } catch ( cloneError ) {
90
+ if ( SketchesError . NotFound . is ( cloneError ) ) {
91
+ if ( onDidFailClone ) {
92
+ await onDidFailClone ( cloneError , uri ) ;
93
+ return ;
94
+ }
95
+ }
96
+ throw cloneError ;
97
+ }
98
+ }
33
99
34
100
@injectable ( )
35
101
export abstract class Examples extends SketchContribution {
@@ -94,7 +160,7 @@ export abstract class Examples extends SketchContribution {
94
160
// TODO: unregister submenu? https://github.com/eclipse-theia/theia/issues/7300
95
161
registry . registerSubmenu (
96
162
ArduinoMenus . FILE__EXAMPLES_SUBMENU ,
97
- nls . localize ( 'arduino/examples/menu' , 'Examples' ) ,
163
+ examplesLabel ,
98
164
{
99
165
order : '4' ,
100
166
}
@@ -174,47 +240,33 @@ export abstract class Examples extends SketchContribution {
174
240
}
175
241
176
242
protected createHandler ( uri : string ) : CommandHandler {
243
+ const forceUpdate = ( ) =>
244
+ this . update ( {
245
+ board : this . boardsServiceClient . boardsConfig . selectedBoard ,
246
+ forceRefresh : true ,
247
+ } ) ;
177
248
return {
178
249
execute : async ( ) => {
179
- const sketch = await this . clone ( uri ) ;
180
- if ( sketch ) {
181
- try {
182
- return this . commandService . executeCommand (
183
- OpenSketch . Commands . OPEN_SKETCH . id ,
184
- sketch
185
- ) ;
186
- } catch ( err ) {
187
- if ( SketchesError . NotFound . is ( err ) ) {
250
+ await openClonedExample (
251
+ uri ,
252
+ {
253
+ sketchesService : this . sketchesService ,
254
+ commandService : this . commandRegistry ,
255
+ } ,
256
+ {
257
+ onDidFailClone : ( ) => {
188
258
// Do not toast the error message. It's handled by the `Open Sketch` command.
189
- this . update ( {
190
- board : this . boardsServiceClient . boardsConfig . selectedBoard ,
191
- forceRefresh : true ,
192
- } ) ;
193
- } else {
194
- throw err ;
195
- }
259
+ forceUpdate ( ) ;
260
+ } ,
261
+ onDidFailOpen : ( err ) => {
262
+ this . messageService . error ( err . message ) ;
263
+ forceUpdate ( ) ;
264
+ } ,
196
265
}
197
- }
266
+ ) ;
198
267
} ,
199
268
} ;
200
269
}
201
-
202
- private async clone ( uri : string ) : Promise < Sketch | undefined > {
203
- try {
204
- const sketch = await this . sketchesService . cloneExample ( uri ) ;
205
- return sketch ;
206
- } catch ( err ) {
207
- if ( SketchesError . NotFound . is ( err ) ) {
208
- this . messageService . error ( err . message ) ;
209
- this . update ( {
210
- board : this . boardsServiceClient . boardsConfig . selectedBoard ,
211
- forceRefresh : true ,
212
- } ) ;
213
- } else {
214
- throw err ;
215
- }
216
- }
217
- }
218
270
}
219
271
220
272
@injectable ( )
0 commit comments