- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathcreate-features.ts
146 lines (132 loc) · 4.91 KB
/
create-features.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import{FrontendApplicationContribution}from'@theia/core/lib/browser/frontend-application-contribution';
import{DisposableCollection}from'@theia/core/lib/common/disposable';
import{Emitter,Event}from'@theia/core/lib/common/event';
importURIfrom'@theia/core/lib/common/uri';
import{inject,injectable}from'@theia/core/shared/inversify';
import{Sketch}from'../../common/protocol';
import{AuthenticationSession}from'../../common/protocol/authentication-service';
import{ArduinoPreferences}from'../arduino-preferences';
import{AuthenticationClientService}from'../auth/authentication-client-service';
import{LocalCacheFsProvider}from'../local-cache/local-cache-fs-provider';
import{
ARDUINO_CLOUD_FOLDER,
REMOTE_SKETCHBOOK_FOLDER,
}from'../utils/constants';
import{CreateUri}from'./create-uri';
exporttypeCloudSketchState='push'|'pull';
@injectable()
exportclassCreateFeaturesimplementsFrontendApplicationContribution{
@inject(ArduinoPreferences)
privatereadonlypreferences: ArduinoPreferences;
@inject(AuthenticationClientService)
privatereadonlyauthenticationService: AuthenticationClientService;
@inject(LocalCacheFsProvider)
privatereadonlylocalCacheFsProvider: LocalCacheFsProvider;
/**
* The keys are the Create URI of the sketches.
*/
privatereadonly_cloudSketchStates=newMap<string,CloudSketchState>();
privatereadonlyonDidChangeSessionEmitter=newEmitter<
AuthenticationSession|undefined
>();
privatereadonlyonDidChangeEnabledEmitter=newEmitter<boolean>();
privatereadonlyonDidChangeCloudSketchStateEmitter=newEmitter<{
uri: URI;
state: CloudSketchState|undefined;
}>();
privatereadonlytoDispose=newDisposableCollection(
this.onDidChangeSessionEmitter,
this.onDidChangeEnabledEmitter,
this.onDidChangeCloudSketchStateEmitter
);
private_enabled: boolean;
private_session: AuthenticationSession|undefined;
onStart(): void{
this.toDispose.pushAll([
this.authenticationService.onSessionDidChange((session)=>{
constoldSession=this._session;
this._session=session;
if(!!oldSession!==!!this._session){
this.onDidChangeSessionEmitter.fire(this._session);
}
}),
this.preferences.onPreferenceChanged(({ preferenceName, newValue })=>{
if(preferenceName==='arduino.cloud.enabled'){
constoldEnabled=this._enabled;
this._enabled=Boolean(newValue);
if(this._enabled!==oldEnabled){
this.onDidChangeEnabledEmitter.fire(this._enabled);
}
}
}),
]);
this._enabled=this.preferences['arduino.cloud.enabled'];
this._session=this.authenticationService.session;
}
onStop(): void{
this.toDispose.dispose();
}
getonDidChangeSession(): Event<AuthenticationSession|undefined>{
returnthis.onDidChangeSessionEmitter.event;
}
getonDidChangeEnabled(): Event<boolean>{
returnthis.onDidChangeEnabledEmitter.event;
}
getonDidChangeCloudSketchState(): Event<{
uri: URI;
state: CloudSketchState|undefined;
}>{
returnthis.onDidChangeCloudSketchStateEmitter.event;
}
getsession(): AuthenticationSession|undefined{
returnthis._session;
}
getenabled(): boolean{
returnthis._enabled;
}
cloudSketchState(uri: URI): CloudSketchState|undefined{
returnthis._cloudSketchStates.get(uri.toString());
}
setCloudSketchState(uri: URI,state: CloudSketchState|undefined): void{
if(uri.scheme!==CreateUri.scheme){
thrownewError(
`Expected a URI with '${uri.scheme}' scheme. Got: ${uri.toString()}`
);
}
constkey=uri.toString();
if(!state){
if(!this._cloudSketchStates.delete(key)){
console.warn(
`Could not reset the cloud sketch state of ${key}. No state existed for the the cloud sketch.`
);
}else{
this.onDidChangeCloudSketchStateEmitter.fire({ uri,state: undefined});
}
}else{
this._cloudSketchStates.set(key,state);
this.onDidChangeCloudSketchStateEmitter.fire({ uri, state });
}
}
/**
* `true` if the sketch is under `directories.data/RemoteSketchbook`. Otherwise, `false`.
* Returns with `undefined` if `dataDirUri` is `undefined`.
*/
isCloud(sketch: Sketch,dataDirUri: URI|undefined): boolean|undefined{
if(!dataDirUri){
console.warn(
`Could not decide whether the sketch ${sketch.uri} is cloud or local. The 'directories.data' location was not available from the CLI config.`
);
returnundefined;
}
returndataDirUri
.resolve(REMOTE_SKETCHBOOK_FOLDER)
.resolve(ARDUINO_CLOUD_FOLDER)
.isEqualOrParent(newURI(sketch.uri));
}
cloudUri(sketch: Sketch): URI|undefined{
if(!this.session){
returnundefined;
}
returnthis.localCacheFsProvider.from(newURI(sketch.uri));
}
}