1
1
import { injectable , inject } from 'inversify' ;
2
+ import { MessageService } from '@theia/core' ;
2
3
import { LabelProvider } from '@theia/core/lib/browser' ;
3
4
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service' ;
4
5
import { ConfigService } from '../common/protocol/config-service' ;
@@ -21,26 +22,44 @@ export class ArduinoWorkspaceService extends WorkspaceService {
21
22
@inject ( EditorMode )
22
23
protected readonly editorMode : EditorMode ;
23
24
24
- async getDefaultWorkspaceUri ( ) : Promise < string | undefined > {
25
- const [ hash , recentWorkspaces , recentSketches ] = await Promise . all ( [
26
- window . location . hash ,
27
- this . sketchService . getSketches ( ) . then ( sketches => sketches . map ( ( { uri } ) => uri ) ) ,
28
- this . server . getRecentWorkspaces ( )
29
- ] ) ;
30
- const toOpen = await new ArduinoWorkspaceRootResolver ( {
31
- isValid : this . isValid . bind ( this )
32
- } ) . resolve ( {
33
- hash,
34
- recentWorkspaces,
35
- recentSketches
36
- } ) ;
37
- if ( toOpen ) {
38
- const { uri } = toOpen ;
39
- await this . server . setMostRecentlyUsedWorkspace ( uri ) ;
40
- return toOpen . uri ;
25
+ @inject ( MessageService )
26
+ protected readonly messageService : MessageService ;
27
+
28
+ private workspaceUri ?: Promise < string | undefined > ;
29
+
30
+ protected getDefaultWorkspaceUri ( ) : Promise < string | undefined > {
31
+ if ( this . workspaceUri ) {
32
+ // Avoid creating a new sketch twice
33
+ return this . workspaceUri ;
41
34
}
42
- const { sketchDirUri } = ( await this . configService . getConfiguration ( ) ) ;
43
- return ( await this . sketchService . createNewSketch ( sketchDirUri ) ) . uri ;
35
+ this . workspaceUri = ( async ( ) => {
36
+ try {
37
+ const hash = window . location . hash ;
38
+ const [ recentWorkspaces , recentSketches ] = await Promise . all ( [
39
+ this . server . getRecentWorkspaces ( ) ,
40
+ this . sketchService . getSketches ( ) . then ( sketches => sketches . map ( s => s . uri ) )
41
+ ] ) ;
42
+ const toOpen = await new ArduinoWorkspaceRootResolver ( {
43
+ isValid : this . isValid . bind ( this )
44
+ } ) . resolve ( { hash, recentWorkspaces, recentSketches } ) ;
45
+ if ( toOpen ) {
46
+ const { uri } = toOpen ;
47
+ await this . server . setMostRecentlyUsedWorkspace ( uri ) ;
48
+ return toOpen . uri ;
49
+ }
50
+ const { sketchDirUri } = ( await this . configService . getConfiguration ( ) ) ;
51
+ this . logger . info ( `No valid workspace URI found. Creating new sketch in ${ sketchDirUri } ` )
52
+ return ( await this . sketchService . createNewSketch ( sketchDirUri ) ) . uri ;
53
+ } catch ( err ) {
54
+ this . logger . fatal ( `Failed to determine the sketch directory: ${ err } ` )
55
+ this . messageService . error (
56
+ 'There was an error creating the sketch directory. ' +
57
+ 'See the log for more details. ' +
58
+ 'The application will probably not work as expected.' )
59
+ return super . getDefaultWorkspaceUri ( ) ;
60
+ }
61
+ } ) ( ) ;
62
+ return this . workspaceUri ;
44
63
}
45
64
46
65
private async isValid ( uri : string ) : Promise < boolean > {
0 commit comments