- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathupdate-indexes.ts
193 lines (181 loc) · 6.5 KB
/
update-indexes.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import{LocalStorageService}from'@theia/core/lib/browser/storage-service';
import{nls}from'@theia/core/lib/common/nls';
import{inject,injectable}from'@theia/core/shared/inversify';
import{CoreService,IndexType}from'../../common/protocol';
import{NotificationCenter}from'../notification-center';
import{WindowServiceExt}from'../theia/core/window-service-ext';
import{Command,CommandRegistry,Contribution}from'./contribution';
@injectable()
exportclassUpdateIndexesextendsContribution{
@inject(WindowServiceExt)
privatereadonlywindowService: WindowServiceExt;
@inject(LocalStorageService)
privatereadonlylocalStorage: LocalStorageService;
@inject(CoreService)
privatereadonlycoreService: CoreService;
@inject(NotificationCenter)
privatereadonlynotificationCenter: NotificationCenter;
protectedoverrideinit(): void{
super.init();
this.notificationCenter.onIndexUpdateDidComplete(({ summary })=>
Promise.all(
Object.entries(summary).map(([type,updatedAt])=>
this.setLastUpdateDateTime(typeasIndexType,updatedAt)
)
)
);
}
overrideonReady(): void{
this.checkForUpdates();
}
overrideregisterCommands(registry: CommandRegistry): void{
registry.registerCommand(UpdateIndexes.Commands.UPDATE_INDEXES,{
execute: ()=>this.updateIndexes(IndexType.All,true),
});
registry.registerCommand(UpdateIndexes.Commands.UPDATE_PLATFORM_INDEX,{
execute: ()=>this.updateIndexes(['platform'],true),
});
registry.registerCommand(UpdateIndexes.Commands.UPDATE_LIBRARY_INDEX,{
execute: ()=>this.updateIndexes(['library'],true),
});
}
privateasynccheckForUpdates(): Promise<void>{
constcheckForUpdates=this.preferences['arduino.checkForUpdates'];
if(!checkForUpdates){
console.debug(
'[update-indexes]: `arduino.checkForUpdates` is `false`. Skipping updating the indexes.'
);
return;
}
if(awaitthis.windowService.isFirstWindow()){
constsummary=awaitthis.coreService.indexUpdateSummaryBeforeInit();
if(summary.message){
this.messageService.error(summary.message);
}
consttypesToCheck=IndexType.All.filter((type)=>!(typeinsummary));
if(Object.keys(summary).length){
console.debug(
`[update-indexes]: Detected an index update summary before the core gRPC client initialization. Updating local storage with ${JSON.stringify(
summary
)}`
);
}else{
console.debug(
'[update-indexes]: No index update summary was available before the core gRPC client initialization. Checking the status of the all the index types.'
);
}
awaitPromise.allSettled([
...Object.entries(summary).map(([type,updatedAt])=>
this.setLastUpdateDateTime(typeasIndexType,updatedAt)
),
this.updateIndexes(typesToCheck),
]);
}
}
privateasyncupdateIndexes(
types: IndexType[],
force=false
): Promise<void>{
constupdatedAt=newDate().toISOString();
returnPromise.all(
types.map((type)=>this.needsIndexUpdate(type,updatedAt,force))
).then((needsIndexUpdateResults)=>{
consttypesToUpdate=needsIndexUpdateResults.filter(IndexType.is);
if(typesToUpdate.length){
console.debug(
`[update-indexes]: Requesting the index update of type: ${JSON.stringify(
typesToUpdate
)} with date time: ${updatedAt}.`
);
returnthis.coreService.updateIndex({types: typesToUpdate});
}
});
}
privateasyncneedsIndexUpdate(
type: IndexType,
now: string,
force=false
): Promise<IndexType|false>{
if(force){
console.debug(
`[update-indexes]: Update for index type: '${type}' was forcefully requested.`
);
returntype;
}
constlastUpdateIsoDateTime=awaitthis.getLastUpdateDateTime(type);
if(!lastUpdateIsoDateTime){
console.debug(
`[update-indexes]: No last update date time was persisted for index type: '${type}'. Index update is required.`
);
returntype;
}
constlastUpdateDateTime=Date.parse(lastUpdateIsoDateTime);
if(Number.isNaN(lastUpdateDateTime)){
console.debug(
`[update-indexes]: Invalid last update date time was persisted for index type: '${type}'. Last update date time was: ${lastUpdateDateTime}. Index update is required.`
);
returntype;
}
constdiff=newDate(now).getTime()-lastUpdateDateTime;
constneedsIndexUpdate=diff>=this.threshold;
console.debug(
`[update-indexes]: Update for index type '${type}' is ${
needsIndexUpdate ? '' : 'not '
}required. Now: ${now}, Last index update date time: ${newDate(
lastUpdateDateTime
).toISOString()}, diff: ${diff} ms, threshold: ${this.threshold} ms.`
);
returnneedsIndexUpdate ? type : false;
}
privateasyncgetLastUpdateDateTime(
type: IndexType
): Promise<string|undefined>{
constkey=this.storageKeyOf(type);
returnthis.localStorage.getData<string>(key);
}
privateasyncsetLastUpdateDateTime(
type: IndexType,
updatedAt: string
): Promise<void>{
constkey=this.storageKeyOf(type);
returnthis.localStorage.setData<string>(key,updatedAt).finally(()=>{
console.debug(
`[update-indexes]: Updated the last index update date time of '${type}' to ${updatedAt}.`
);
});
}
privatestorageKeyOf(type: IndexType): string{
return`index-last-update-time--${type}`;
}
privategetthreshold(): number{
return4*60*60*1_000;// four hours in millis
}
}
exportnamespaceUpdateIndexes{
exportnamespaceCommands{
exportconstUPDATE_INDEXES: Command&{label: string}={
id: 'arduino-update-indexes',
label: nls.localize(
'arduino/updateIndexes/updateIndexes',
'Update Indexes'
),
category: 'Arduino',
};
exportconstUPDATE_PLATFORM_INDEX: Command&{label: string}={
id: 'arduino-update-package-index',
label: nls.localize(
'arduino/updateIndexes/updatePackageIndex',
'Update Package Index'
),
category: 'Arduino',
};
exportconstUPDATE_LIBRARY_INDEX: Command&{label: string}={
id: 'arduino-update-library-index',
label: nls.localize(
'arduino/updateIndexes/updateLibraryIndex',
'Update Library Index'
),
category: 'Arduino',
};
}
}