when bundle removed all its manifests should remov

This commit is contained in:
Niels Lyngsø
2023-06-27 11:23:38 +02:00
parent baafc13f30
commit 4c93aec164

View File

@@ -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<ManifestBundle>) {
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);
}
});
}
}
}