Skip to content

Commit

Permalink
chore: extract some code to a function
Browse files Browse the repository at this point in the history
Signed-off-by: Florent Benoit <[email protected]>
  • Loading branch information
benoitf committed Aug 2, 2024
1 parent a1026f3 commit ae09775
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions packages/main/src/plugin/extensions-catalog/extensions-catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,9 @@ export class ExtensionsCatalog {
this.configurationRegistry.registerConfigurations([recommendationConfiguration]);
}

// internal method, not exposed
protected async getCatalogJson(): Promise<InternalCatalogJSON | undefined> {
// return the cache version if cache is not reached and we have a cached version
if (this.lastFetchTime + ExtensionsCatalog.CACHE_TIMEOUT > Date.now() && this.cachedCatalog) {
return this.cachedCatalog;
}

// can be called to force the refresh of the catalog
// or it is called automatically when asking the catalog
public async refreshCatalog(): Promise<void> {
// get the URL from the configuration
const catalogUrl = this.configurationRegistry
.getConfiguration(ExtensionsCatalogSettings.SectionName)
Expand All @@ -92,11 +88,25 @@ export class ExtensionsCatalog {
// unable to fetch the extensions
// extract only the error message
if (requestErr.message) {
console.error('Unable to fetch the available extensions: ' + requestErr.message);
throw new Error(`Unable to fetch the available extensions: ${String(requestErr.message)}`);
} else {
console.error('Unable to fetch the available extensions', requestErr.message);
throw new Error(`Unable to fetch the available extensions: ${String(requestErr)}`);
}
}
}

// internal method, not exposed
protected async getCatalogJson(): Promise<InternalCatalogJSON | undefined> {
// return the cache version if cache is not reached and we have a cached version
if (this.lastFetchTime + ExtensionsCatalog.CACHE_TIMEOUT > Date.now() && this.cachedCatalog) {
return this.cachedCatalog;
}

try {
await this.refreshCatalog();
} catch (error: unknown) {
console.error(String(error));
}
// update the last fetch time
this.lastFetchTime = Date.now();

Expand Down

0 comments on commit ae09775

Please sign in to comment.