|
| 1 | +import { LocalStorageService } from '@theia/core/lib/browser/storage-service'; |
| 2 | +import { nls } from '@theia/core/lib/common/nls'; |
| 3 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 4 | +import { CoreService, IndexType } from '../../common/protocol'; |
| 5 | +import { NotificationCenter } from '../notification-center'; |
| 6 | +import { WindowServiceExt } from '../theia/core/window-service-ext'; |
| 7 | +import { Command, CommandRegistry, Contribution } from './contribution'; |
| 8 | + |
| 9 | +@injectable() |
| 10 | +export class UpdateIndexes extends Contribution { |
| 11 | + @inject(WindowServiceExt) |
| 12 | + private readonly windowService: WindowServiceExt; |
| 13 | + @inject(LocalStorageService) |
| 14 | + private readonly localStorage: LocalStorageService; |
| 15 | + @inject(CoreService) |
| 16 | + private readonly coreService: CoreService; |
| 17 | + @inject(NotificationCenter) |
| 18 | + private readonly notificationCenter: NotificationCenter; |
| 19 | + |
| 20 | + protected override init(): void { |
| 21 | + super.init(); |
| 22 | + this.notificationCenter.onIndexUpdateDidComplete(({ summary }) => |
| 23 | + Promise.all( |
| 24 | + Object.entries(summary).map(([type, updatedAt]) => |
| 25 | + this.setLastUpdateDateTime(type as IndexType, updatedAt) |
| 26 | + ) |
| 27 | + ) |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + override onReady(): void { |
| 32 | + this.checkForUpdates(); |
| 33 | + } |
| 34 | + |
| 35 | + override registerCommands(registry: CommandRegistry): void { |
| 36 | + registry.registerCommand(UpdateIndexes.Commands.UPDATE_INDEXES, { |
| 37 | + execute: () => this.updateIndexes(IndexType.All, true), |
| 38 | + }); |
| 39 | + registry.registerCommand(UpdateIndexes.Commands.UPDATE_PLATFORM_INDEX, { |
| 40 | + execute: () => this.updateIndexes(['platform'], true), |
| 41 | + }); |
| 42 | + registry.registerCommand(UpdateIndexes.Commands.UPDATE_LIBRARY_INDEX, { |
| 43 | + execute: () => this.updateIndexes(['library'], true), |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + private async checkForUpdates(): Promise<void> { |
| 48 | + const checkForUpdates = this.preferences['arduino.checkForUpdates']; |
| 49 | + if (!checkForUpdates) { |
| 50 | + console.debug( |
| 51 | + '[update-indexes]: `arduino.checkForUpdates` is `false`. Skipping updating the indexes.' |
| 52 | + ); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (await this.windowService.isFirstWindow()) { |
| 57 | + const summary = await this.coreService.indexUpdateSummaryBeforeInit(); |
| 58 | + if (summary.message) { |
| 59 | + this.messageService.error(summary.message); |
| 60 | + } |
| 61 | + const typesToCheck = IndexType.All.filter((type) => !(type in summary)); |
| 62 | + if (Object.keys(summary).length) { |
| 63 | + console.debug( |
| 64 | + `[update-indexes]: Detected an index update summary before the core gRPC client initialization. Updating local storage with ${JSON.stringify( |
| 65 | + summary |
| 66 | + )}` |
| 67 | + ); |
| 68 | + } else { |
| 69 | + console.debug( |
| 70 | + '[update-indexes]: No index update summary was available before the core gRPC client initialization. Checking the status of the all the index types.' |
| 71 | + ); |
| 72 | + } |
| 73 | + await Promise.allSettled([ |
| 74 | + ...Object.entries(summary).map(([type, updatedAt]) => |
| 75 | + this.setLastUpdateDateTime(type as IndexType, updatedAt) |
| 76 | + ), |
| 77 | + this.updateIndexes(typesToCheck), |
| 78 | + ]); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private async updateIndexes( |
| 83 | + types: IndexType[], |
| 84 | + force = false |
| 85 | + ): Promise<void> { |
| 86 | + const updatedAt = new Date().toISOString(); |
| 87 | + return Promise.all( |
| 88 | + types.map((type) => this.needsIndexUpdate(type, updatedAt, force)) |
| 89 | + ).then((needsIndexUpdateResults) => { |
| 90 | + const typesToUpdate = needsIndexUpdateResults.filter(IndexType.is); |
| 91 | + if (typesToUpdate.length) { |
| 92 | + console.debug( |
| 93 | + `[update-indexes]: Requesting the index update of type: ${JSON.stringify( |
| 94 | + typesToUpdate |
| 95 | + )} with date time: ${updatedAt}.` |
| 96 | + ); |
| 97 | + return this.coreService.updateIndex({ types: typesToUpdate }); |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + private async needsIndexUpdate( |
| 103 | + type: IndexType, |
| 104 | + now: string, |
| 105 | + force = false |
| 106 | + ): Promise<IndexType | false> { |
| 107 | + if (force) { |
| 108 | + console.debug( |
| 109 | + `[update-indexes]: Update for index type: '${type}' was forcefully requested.` |
| 110 | + ); |
| 111 | + return type; |
| 112 | + } |
| 113 | + const lastUpdateIsoDateTime = await this.getLastUpdateDateTime(type); |
| 114 | + if (!lastUpdateIsoDateTime) { |
| 115 | + console.debug( |
| 116 | + `[update-indexes]: No last update date time was persisted for index type: '${type}'. Index update is required.` |
| 117 | + ); |
| 118 | + return type; |
| 119 | + } |
| 120 | + const lastUpdateDateTime = Date.parse(lastUpdateIsoDateTime); |
| 121 | + if (Number.isNaN(lastUpdateDateTime)) { |
| 122 | + console.debug( |
| 123 | + `[update-indexes]: Invalid last update date time was persisted for index type: '${type}'. Last update date time was: ${lastUpdateDateTime}. Index update is required.` |
| 124 | + ); |
| 125 | + return type; |
| 126 | + } |
| 127 | + const diff = new Date(now).getTime() - lastUpdateDateTime; |
| 128 | + const needsIndexUpdate = diff >= this.threshold; |
| 129 | + console.debug( |
| 130 | + `[update-indexes]: Update for index type '${type}' is ${ |
| 131 | + needsIndexUpdate ? '' : 'not ' |
| 132 | + }required. Now: ${now}, Last index update date time: ${new Date( |
| 133 | + lastUpdateDateTime |
| 134 | + ).toISOString()}, diff: ${diff} ms, threshold: ${this.threshold} ms.` |
| 135 | + ); |
| 136 | + return needsIndexUpdate ? type : false; |
| 137 | + } |
| 138 | + |
| 139 | + private async getLastUpdateDateTime( |
| 140 | + type: IndexType |
| 141 | + ): Promise<string | undefined> { |
| 142 | + const key = this.storageKeyOf(type); |
| 143 | + return this.localStorage.getData<string>(key); |
| 144 | + } |
| 145 | + |
| 146 | + private async setLastUpdateDateTime( |
| 147 | + type: IndexType, |
| 148 | + updatedAt: string |
| 149 | + ): Promise<void> { |
| 150 | + const key = this.storageKeyOf(type); |
| 151 | + return this.localStorage.setData<string>(key, updatedAt).finally(() => { |
| 152 | + console.debug( |
| 153 | + `[update-indexes]: Updated the last index update date time of '${type}' to ${updatedAt}.` |
| 154 | + ); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + private storageKeyOf(type: IndexType): string { |
| 159 | + return `index-last-update-time--${type}`; |
| 160 | + } |
| 161 | + |
| 162 | + private get threshold(): number { |
| 163 | + return 4 * 60 * 60 * 1_000; // four hours in millis |
| 164 | + } |
| 165 | +} |
| 166 | +export namespace UpdateIndexes { |
| 167 | + export namespace Commands { |
| 168 | + export const UPDATE_INDEXES: Command & { label: string } = { |
| 169 | + id: 'arduino-update-indexes', |
| 170 | + label: nls.localize( |
| 171 | + 'arduino/updateIndexes/updateIndexes', |
| 172 | + 'Update Indexes' |
| 173 | + ), |
| 174 | + category: 'Arduino', |
| 175 | + }; |
| 176 | + export const UPDATE_PLATFORM_INDEX: Command & { label: string } = { |
| 177 | + id: 'arduino-update-platform-index', |
| 178 | + label: nls.localize( |
| 179 | + 'arduino/updateIndexes/updatePlatformIndex', |
| 180 | + 'Update Platform Index' |
| 181 | + ), |
| 182 | + category: 'Arduino', |
| 183 | + }; |
| 184 | + export const UPDATE_LIBRARY_INDEX: Command & { label: string } = { |
| 185 | + id: 'arduino-update-library-index', |
| 186 | + label: nls.localize( |
| 187 | + 'arduino/updateIndexes/updateLibraryIndex', |
| 188 | + 'Update Library Index' |
| 189 | + ), |
| 190 | + category: 'Arduino', |
| 191 | + }; |
| 192 | + } |
| 193 | +} |
0 commit comments