From 92f60b52c56a9abeae5e2cf1993a48586fa71f25 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 5 Feb 2024 16:01:12 -0500 Subject: [PATCH] Do not call getAllComponents() in activate Part of #3850 Signed-off-by: David Thompson --- src/extension.ts | 1 - src/registriesView.ts | 70 +++++++++---------- .../common-ext/createComponentHelpers.ts | 8 +-- .../create-component/createComponentLoader.ts | 34 ++++----- .../devfile-registry/registryViewLoader.ts | 16 ++--- 5 files changed, 61 insertions(+), 68 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index a574c8722..6bac3b26e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -204,7 +204,6 @@ export async function activate(extensionContext: ExtensionContext): Promise { readonly odo = Odo.Instance; private registries: Registry[]; private readonly compDescriptions: Set = new Set(); - public subject: Subject = new Subject(); + public subject: Subject = new Subject(); + + private initialComponentTypeLoadPromise: Promise; + + constructor() { + this.initialComponentTypeLoadPromise = this.reloadComponentTypeList(); + void Progress.execFunctionWithProgress('Loading component types', () => this.initialComponentTypeLoadPromise); + } createTreeView(id: string): TreeView { if (!this.treeView) { @@ -102,7 +108,8 @@ export class ComponentTypesView implements TreeDataProvider { return this.registries; } - public getCompDescriptions(): Set { + public async getCompDescriptions(): Promise> { + await this.initialComponentTypeLoadPromise; return this.compDescriptions; } @@ -110,39 +117,26 @@ export class ComponentTypesView implements TreeDataProvider { return this.registries; } - public async getAllComponents(): Promise { - return new Promise((resolve) => { - let isError = false; - this.compDescriptions.clear(); - void Odo.Instance.getComponentTypes().then(async (devFileComponentTypes: ComponentTypeAdapter[]) => { - await this.getRegistries(); - devFileComponentTypes.forEach((component: ComponentTypeAdapter) => { - Odo.Instance.getDetailedComponentInformation(component) // - .then((componentDesc: ComponentTypeDescription) => { - // eslint-disable-next-line max-nested-callbacks - componentDesc.devfileData.devfile?.starterProjects?.map((starter: StarterProject) => { - starter.typeName = component.name; - }); - this.compDescriptions.add(componentDesc); - - if (devFileComponentTypes.length === this.compDescriptions.size) { - this.subject.next('refresh'); - resolve(); - } - }).catch(() => { - isError = true; - }).finally(() => { - if (isError && !this.subject.closed) { - this.subject.next('refresh'); - resolve(); - } - }); + private async reloadComponentTypeList(): Promise { + this.compDescriptions.clear(); + try { + const devfileComponentTypes = await Odo.Instance.getComponentTypes(); + await this.getRegistries(); + await Promise.all(devfileComponentTypes.map(async (devfileComponentType) => { + const componentDesc: ComponentTypeDescription = await Odo.Instance.getDetailedComponentInformation(devfileComponentType); + componentDesc.devfileData.devfile?.starterProjects?.map((starter: StarterProject) => { + starter.typeName = devfileComponentType.name; }); - }).catch(() => { - this.subject.next('error'); - resolve(); - }); - }); + this.compDescriptions.add(componentDesc); + + if (devfileComponentTypes.length === this.compDescriptions.size) { + this.subject.next(); + } + })); + this.subject.next(); + } catch (_) { + this.subject.next(); + } } // eslint-disable-next-line class-methods-use-this @@ -362,7 +356,7 @@ export class ComponentTypesView implements TreeDataProvider { void Progress.execFunctionWithProgress('Devfile registry is updating',async () => { const newRegistry = await Odo.Instance.addRegistry(regName, regURL, token); ComponentTypesView.instance.addRegistry(newRegistry); - await ComponentTypesView.instance.getAllComponents(); + await ComponentTypesView.instance.reloadComponentTypeList(); ComponentTypesView.instance.refresh(false); }) } @@ -389,7 +383,7 @@ export class ComponentTypesView implements TreeDataProvider { await Odo.Instance.removeRegistry(registry.name); ComponentTypesView.instance.removeRegistry(registry); if (!isEdit) { - await ComponentTypesView.instance.getAllComponents(); + await ComponentTypesView.instance.reloadComponentTypeList(); } } } diff --git a/src/webview/common-ext/createComponentHelpers.ts b/src/webview/common-ext/createComponentHelpers.ts index 2f2ae1603..f49273c1c 100644 --- a/src/webview/common-ext/createComponentHelpers.ts +++ b/src/webview/common-ext/createComponentHelpers.ts @@ -185,7 +185,7 @@ export function validatePortNumber(portNumber: number): string { * * @returns a list of the devfile registries with their devfiles attached */ -export function getDevfileRegistries(): DevfileRegistry[] { +export async function getDevfileRegistries(): Promise { const registries = ComponentTypesView.instance.getListOfRegistries(); if (!registries || registries.length === 0) { throw new Error('No Devfile registries available. Default registry is missing'); @@ -198,7 +198,7 @@ export function getDevfileRegistries(): DevfileRegistry[] { } as DevfileRegistry; }); - const components = ComponentTypesView.instance.getCompDescriptions(); + const components = await ComponentTypesView.instance.getCompDescriptions(); for (const component of components) { const devfileRegistry = devfileRegistries.find( (devfileRegistry) => format(devfileRegistry.url) === format(component.registry.url), @@ -261,8 +261,8 @@ export function getDevfileCapabilities(): string[] { * * @returns a list of the devfile tags */ -export function getDevfileTags(url?: string): string[] { - const devfileRegistries = getDevfileRegistries(); +export async function getDevfileTags(url?: string): Promise { + const devfileRegistries = await getDevfileRegistries(); const devfileTags: string[] = [ ...new Set( diff --git a/src/webview/create-component/createComponentLoader.ts b/src/webview/create-component/createComponentLoader.ts index 4c3d44d38..694fe9127 100644 --- a/src/webview/create-component/createComponentLoader.ts +++ b/src/webview/create-component/createComponentLoader.ts @@ -18,6 +18,7 @@ import { ComponentTypesView } from '../../registriesView'; import sendTelemetry from '../../telemetry'; import { ExtensionID } from '../../util/constants'; import { DevfileConverter } from '../../util/devfileConverter'; +import { DevfileV1 } from '../../util/devfileV1Type'; import { getInitialWorkspaceFolder, selectWorkspaceFolder } from '../../util/workspace'; import { getDevfileCapabilities, @@ -29,7 +30,6 @@ import { } from '../common-ext/createComponentHelpers'; import { loadWebviewHtml, validateGitURL } from '../common-ext/utils'; import { Devfile, DevfileRegistry, TemplateProjectIdentifier } from '../common/devfile'; -import { DevfileV1 } from '../../util/devfileV1Type'; interface CloneProcess { status: boolean; @@ -78,7 +78,7 @@ export default class CreateComponentLoader { }); const registriesSubscription = ComponentTypesView.instance.subject.subscribe(() => { - sendUpdatedRegistries(); + void sendUpdatedRegistries(); }); const capabiliiesySubscription = ComponentTypesView.instance.subject.subscribe(() => { @@ -86,7 +86,7 @@ export default class CreateComponentLoader { }); const tagsSubscription = ComponentTypesView.instance.subject.subscribe(() => { - sendUpdatedTags(); + void sendUpdatedTags(); }); panel.onDidDispose(() => { @@ -138,7 +138,7 @@ export default class CreateComponentLoader { case 'getDevfileRegistries': { void CreateComponentLoader.panel.webview.postMessage({ action: 'devfileRegistries', - data: getDevfileRegistries(), + data: await getDevfileRegistries(), }); break; } @@ -158,7 +158,7 @@ export default class CreateComponentLoader { case 'getDevfileTags': { void CreateComponentLoader.panel.webview.postMessage({ action: 'devfileTags', - data: getDevfileTags(), + data: await getDevfileTags(), }); break; } @@ -360,7 +360,7 @@ export default class CreateComponentLoader { await fs.mkdir(componentFolder, {recursive: true}); await fse.copy(tmpFolder.fsPath, componentFolder); } - const devfileType = getDevfileType(message.data.devfileDisplayName); + const devfileType = await getDevfileType(message.data.devfileDisplayName); const componentFolderUri = Uri.file(componentFolder); if (!await isDevfileExists(componentFolderUri)) { await Odo.Instance.createComponentFromLocation( @@ -521,7 +521,7 @@ export default class CreateComponentLoader { action: 'getRecommendedDevfileStart' }); analyzeRes = await Odo.Instance.analyze(uri.fsPath); - compDescriptions = getCompDescription(analyzeRes); + compDescriptions = await getCompDescription(analyzeRes); } catch (error) { if (error.message.toLowerCase().indexOf('failed to parse the devfile') !== -1) { const actions: Array = ['Yes', 'Cancel']; @@ -536,7 +536,7 @@ export default class CreateComponentLoader { const devfileV1 = JSYAML.load(file.toString()) as DevfileV1; await fs.unlink(devFileV1Path); analyzeRes = await Odo.Instance.analyze(uri.fsPath); - compDescriptions = getCompDescription(analyzeRes); + compDescriptions = await getCompDescription(analyzeRes); const endPoints = getEndPoints(compDescriptions[0]); const devfileV2 = DevfileConverter.getInstance().devfileV1toDevfileV2( devfileV1, @@ -562,7 +562,7 @@ export default class CreateComponentLoader { void CreateComponentLoader.panel.webview.postMessage({ action: 'getRecommendedDevfile' }); - const devfileRegistry: DevfileRegistry[] = getDevfileRegistries(); + const devfileRegistry: DevfileRegistry[] = await getDevfileRegistries(); const allDevfiles: Devfile[] = devfileRegistry.flatMap((registry) => registry.devfiles); const devfile: Devfile | undefined = compDescriptions.length !== 0 @@ -583,8 +583,8 @@ export default class CreateComponentLoader { } } -function getCompDescription(devfiles: AnalyzeResponse[]): ComponentTypeDescription[] { - const compDescriptions = ComponentTypesView.instance.getCompDescriptions(); +async function getCompDescription(devfiles: AnalyzeResponse[]): Promise { + const compDescriptions = await ComponentTypesView.instance.getCompDescriptions(); if (devfiles.length === 0) { return Array.from(compDescriptions); } @@ -598,9 +598,9 @@ function getCompDescription(devfiles: AnalyzeResponse[]): ComponentTypeDescripti ); } -function getDevfileType(devfileDisplayName: string): string { +async function getDevfileType(devfileDisplayName: string): Promise { const compDescriptions: Set = - ComponentTypesView.instance.getCompDescriptions(); + await ComponentTypesView.instance.getCompDescriptions(); const devfileDescription: ComponentTypeDescription = Array.from(compDescriptions).find( (description) => description.displayName === devfileDisplayName, ); @@ -661,11 +661,11 @@ async function validateFolderPath(path: string) { } } -function sendUpdatedRegistries() { +async function sendUpdatedRegistries() { if (CreateComponentLoader.panel) { void CreateComponentLoader.panel.webview.postMessage({ action: 'devfileRegistries', - data: getDevfileRegistries(), + data: await getDevfileRegistries(), }); } } @@ -679,11 +679,11 @@ function sendUpdatedCapabilities() { } } -function sendUpdatedTags() { +async function sendUpdatedTags() { if (CreateComponentLoader.panel) { void CreateComponentLoader.panel.webview.postMessage({ action: 'devfileTags', - data: getDevfileTags(), + data: await getDevfileTags(), }); } } diff --git a/src/webview/devfile-registry/registryViewLoader.ts b/src/webview/devfile-registry/registryViewLoader.ts index 6d284fec4..c70685154 100644 --- a/src/webview/devfile-registry/registryViewLoader.ts +++ b/src/webview/devfile-registry/registryViewLoader.ts @@ -36,13 +36,13 @@ async function devfileRegistryViewerMessageListener(event: any): Promise { }) break; case 'getDevfileRegistries': - RegistryViewLoader.sendUpdatedRegistries(); + await RegistryViewLoader.sendUpdatedRegistries(); break; case 'getDevfileCapabilities': RegistryViewLoader.sendUpdatedCapabilities(); break; case 'getDevfileTags': - RegistryViewLoader.sendUpdatedTags(); + await RegistryViewLoader.sendUpdatedTags(); break; case 'createComponent': { const { projectFolder, componentName } = event.data; @@ -198,9 +198,9 @@ export default class RegistryViewLoader { return Promise.resolve(); } - static sendUpdatedRegistries() { + static async sendUpdatedRegistries() { if (panel) { - let registries = getDevfileRegistries(); + let registries = await getDevfileRegistries(); if (RegistryViewLoader.url) { registries = registries.filter((devfileRegistry) => devfileRegistry.url === RegistryViewLoader.url); } @@ -220,18 +220,18 @@ export default class RegistryViewLoader { } } - static sendUpdatedTags() { + static async sendUpdatedTags() { if (panel) { void panel.webview.postMessage({ action: 'devfileTags', - data: getDevfileTags(RegistryViewLoader.url), + data: await getDevfileTags(RegistryViewLoader.url), }); } } } ComponentTypesView.instance.subject.subscribe(() => { - RegistryViewLoader.sendUpdatedRegistries(); + void RegistryViewLoader.sendUpdatedRegistries(); }); ComponentTypesView.instance.subject.subscribe(() => { @@ -239,5 +239,5 @@ ComponentTypesView.instance.subject.subscribe(() => { }); ComponentTypesView.instance.subject.subscribe(() => { - RegistryViewLoader.sendUpdatedTags(); + void RegistryViewLoader.sendUpdatedTags(); });