From 4c93aec164a6e075a3ed71d24372e75e29fa6d41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Tue, 27 Jun 2023 11:23:38 +0200 Subject: [PATCH] when bundle removed all its manifests should remov --- .../bundle-extension-initializer.ts | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/libs/extension-api/bundle-extension-initializer.ts b/src/Umbraco.Web.UI.Client/src/libs/extension-api/bundle-extension-initializer.ts index 4f7fac564e..72350d2002 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/extension-api/bundle-extension-initializer.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/extension-api/bundle-extension-initializer.ts @@ -1,21 +1,27 @@ -import type { ManifestBundle } from './types.js'; +import type { ManifestBase, ManifestBundle } from './types.js'; import { loadExtension } from './load-extension.function.js'; import { UmbExtensionRegistry } from './registry/extension.registry.js'; import { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api'; export class UmbBundleExtensionInitializer { - #host; #extensionRegistry; #bundleMap = new Map(); constructor(host: UmbControllerHostElement, extensionRegistry: UmbExtensionRegistry) { - this.#host = host; this.#extensionRegistry = extensionRegistry; extensionRegistry.extensionsOfType('bundle').subscribe((bundles) => { + // Unregister removed bundles: + this.#bundleMap.forEach((existingBundle) => { + if (!bundles.find((b) => b.alias === existingBundle.alias)) { + this.unregisterBundle(existingBundle); + this.#bundleMap.delete(existingBundle.alias); + } + }); + + // Register new bundles: bundles.forEach((bundle) => { if (this.#bundleMap.has(bundle.alias)) return; this.#bundleMap.set(bundle.alias, bundle); - // TODO: Should we unInit a entry point if is removed? this.instantiateBundle(bundle); }); }); @@ -36,4 +42,20 @@ export class UmbBundleExtensionInitializer { }); } } + + async unregisterBundle(manifest: ManifestBundle) { + const js = await loadExtension(manifest); + + if (js) { + Object.keys(js).forEach((key) => { + const value = js[key]; + + if (Array.isArray(value)) { + this.#extensionRegistry.unregisterMany(value.map((v) => v.alias)); + } else if (typeof value === 'object') { + this.#extensionRegistry.unregister((value as ManifestBase).alias); + } + }); + } + } }