@@ -8,6 +8,9 @@ import { AuthenticationSession } from '../../node/auth/types';
8
8
import { ArduinoPreferences } from '../arduino-preferences' ;
9
9
import { AuthenticationClientService } from '../auth/authentication-client-service' ;
10
10
import { LocalCacheFsProvider } from '../local-cache/local-cache-fs-provider' ;
11
+ import { CreateUri } from './create-uri' ;
12
+
13
+ export type CloudSketchState = 'push' | 'pull' ;
11
14
12
15
@injectable ( )
13
16
export class CreateFeatures implements FrontendApplicationContribution {
@@ -18,13 +21,22 @@ export class CreateFeatures implements FrontendApplicationContribution {
18
21
@inject ( LocalCacheFsProvider )
19
22
private readonly localCacheFsProvider : LocalCacheFsProvider ;
20
23
24
+ /**
25
+ * The keys are the Create URI of the sketches.
26
+ */
27
+ private readonly _cloudSketchStates = new Map < string , CloudSketchState > ( ) ;
21
28
private readonly onDidChangeSessionEmitter = new Emitter <
22
29
AuthenticationSession | undefined
23
30
> ( ) ;
24
31
private readonly onDidChangeEnabledEmitter = new Emitter < boolean > ( ) ;
32
+ private readonly onDidChangeCloudSketchStateEmitter = new Emitter < {
33
+ uri : URI ;
34
+ state : CloudSketchState | undefined ;
35
+ } > ( ) ;
25
36
private readonly toDispose = new DisposableCollection (
26
37
this . onDidChangeSessionEmitter ,
27
- this . onDidChangeEnabledEmitter
38
+ this . onDidChangeEnabledEmitter ,
39
+ this . onDidChangeCloudSketchStateEmitter
28
40
) ;
29
41
private _enabled : boolean ;
30
42
private _session : AuthenticationSession | undefined ;
@@ -64,14 +76,55 @@ export class CreateFeatures implements FrontendApplicationContribution {
64
76
return this . onDidChangeEnabledEmitter . event ;
65
77
}
66
78
67
- get enabled ( ) : boolean {
68
- return this . _enabled ;
79
+ get onDidChangeCloudSketchState ( ) : Event < {
80
+ uri : URI ;
81
+ state : CloudSketchState | undefined ;
82
+ } > {
83
+ return this . onDidChangeCloudSketchStateEmitter . event ;
69
84
}
70
85
71
86
get session ( ) : AuthenticationSession | undefined {
72
87
return this . _session ;
73
88
}
74
89
90
+ get enabled ( ) : boolean {
91
+ return this . _enabled ;
92
+ }
93
+
94
+ get cloudSketchStates ( ) : {
95
+ uri : URI ;
96
+ state : CloudSketchState | undefined ;
97
+ } [ ] {
98
+ return Array . from ( this . _cloudSketchStates . entries ( ) ) . map (
99
+ ( [ uri , state ] ) => ( { uri : new URI ( uri ) , state } )
100
+ ) ;
101
+ }
102
+
103
+ cloudSketchState ( uri : URI ) : CloudSketchState | undefined {
104
+ return this . _cloudSketchStates . get ( uri . toString ( ) ) ;
105
+ }
106
+
107
+ setCloudSketchState ( uri : URI , state : CloudSketchState | undefined ) : void {
108
+ if ( uri . scheme !== CreateUri . scheme ) {
109
+ throw new Error (
110
+ `Expected a URI with '${ uri . scheme } ' scheme. Got: ${ uri . toString ( ) } `
111
+ ) ;
112
+ }
113
+ const key = uri . toString ( ) ;
114
+ if ( ! state ) {
115
+ if ( ! this . _cloudSketchStates . delete ( key ) ) {
116
+ console . warn (
117
+ `Could not reset the cloud sketch state of ${ key } . No state existed for the the cloud sketch.`
118
+ ) ;
119
+ } else {
120
+ this . onDidChangeCloudSketchStateEmitter . fire ( { uri, state : undefined } ) ;
121
+ }
122
+ } else {
123
+ this . _cloudSketchStates . set ( key , state ) ;
124
+ this . onDidChangeCloudSketchStateEmitter . fire ( { uri, state } ) ;
125
+ }
126
+ }
127
+
75
128
/**
76
129
* `true` if the sketch is under `directories.data/RemoteSketchbook`. Otherwise, `false`.
77
130
* Returns with `undefined` if `dataDirUri` is `undefined`.
@@ -83,7 +136,10 @@ export class CreateFeatures implements FrontendApplicationContribution {
83
136
) ;
84
137
return undefined ;
85
138
}
86
- return dataDirUri . isEqualOrParent ( new URI ( sketch . uri ) ) ;
139
+ return dataDirUri
140
+ . resolve ( 'RemoteSketchbook' )
141
+ . resolve ( 'ArduinoCloud' )
142
+ . isEqualOrParent ( new URI ( sketch . uri ) ) ;
87
143
}
88
144
89
145
cloudUri ( sketch : Sketch ) : URI | undefined {
0 commit comments