1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Simplify loading extensions

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-28 12:30:31 -05:00
parent a1003a5cd1
commit 0150edc8fd
5 changed files with 13 additions and 33 deletions

View File

@ -8,7 +8,6 @@ import { isEqual } from "lodash";
import type { ObservableMap } from "mobx"; import type { ObservableMap } from "mobx";
import { action, computed, makeObservable, observable, observe, reaction, when } from "mobx"; import { action, computed, makeObservable, observable, observe, reaction, when } from "mobx";
import { broadcastMessage, ipcMainOn, ipcRendererOn, ipcMainHandle } from "../../common/ipc"; import { broadcastMessage, ipcMainOn, ipcRendererOn, ipcMainHandle } from "../../common/ipc";
import type { Disposer } from "../../common/utils";
import { isDefined, toJS } from "../../common/utils"; import { isDefined, toJS } from "../../common/utils";
import type { InstalledExtension } from "../extension-discovery/extension-discovery"; import type { InstalledExtension } from "../extension-discovery/extension-discovery";
import type { LensExtension, LensExtensionConstructor, LensExtensionId } from "../lens-extension"; import type { LensExtension, LensExtensionConstructor, LensExtensionId } from "../lens-extension";
@ -201,7 +200,7 @@ export class ExtensionLoader {
protected async initMain() { protected async initMain() {
this.isLoaded = true; this.isLoaded = true;
this.loadOnMain(); await this.autoInitExtensions();
ipcMainHandle(extensionLoaderFromMainChannel, () => { ipcMainHandle(extensionLoaderFromMainChannel, () => {
return Array.from(this.toJSON()); return Array.from(this.toJSON());
@ -249,23 +248,7 @@ export class ExtensionLoader {
}); });
} }
loadOnMain() { protected async loadExtensions(installedExtensions: Map<string, InstalledExtension>) {
this.autoInitExtensions(async () => []);
}
loadOnClusterManagerRenderer = () => {
this.dependencies.logger.debug(`${logModule}: load on main renderer (cluster manager)`);
return this.autoInitExtensions(async () => []);
};
loadOnClusterRenderer = () => {
this.dependencies.logger.debug(`${logModule}: load on cluster renderer (dashboard)`);
this.autoInitExtensions(async () => []);
};
protected async loadExtensions(installedExtensions: Map<string, InstalledExtension>, register: (ext: LensExtension) => Promise<Disposer[]>) {
// Steps of the function: // Steps of the function:
// 1. require and call .activate for each Extension // 1. require and call .activate for each Extension
// 2. Wait until every extension's onActivate has been resolved // 2. Wait until every extension's onActivate has been resolved
@ -329,7 +312,7 @@ export class ExtensionLoader {
// Return ExtensionLoading[] // Return ExtensionLoading[]
return extensions.map(extension => { return extensions.map(extension => {
const loaded = extension.instance.enable(register).catch((err) => { const loaded = extension.instance.enable().catch((err) => {
this.dependencies.logger.error(`${logModule}: failed to enable`, { ext: extension, err }); this.dependencies.logger.error(`${logModule}: failed to enable`, { ext: extension, err });
}); });
@ -340,12 +323,14 @@ export class ExtensionLoader {
}); });
} }
protected autoInitExtensions(register: (ext: LensExtension) => Promise<Disposer[]>) { autoInitExtensions() {
this.dependencies.logger.info(`${logModule}: auto initializing extensions`);
// Setup reaction to load extensions on JSON changes // Setup reaction to load extensions on JSON changes
reaction(() => this.toJSON(), installedExtensions => this.loadExtensions(installedExtensions, register)); reaction(() => this.toJSON(), installedExtensions => this.loadExtensions(installedExtensions));
// Load initial extensions // Load initial extensions
return this.loadExtensions(this.toJSON(), register); return this.loadExtensions(this.toJSON());
} }
protected requireExtension(extension: InstalledExtension): LensExtensionConstructor | null { protected requireExtension(extension: InstalledExtension): LensExtensionConstructor | null {

View File

@ -8,7 +8,6 @@ import { action, computed, makeObservable, observable } from "mobx";
import logger from "../main/logger"; import logger from "../main/logger";
import type { ProtocolHandlerRegistration } from "./registries"; import type { ProtocolHandlerRegistration } from "./registries";
import type { PackageJson } from "type-fest"; import type { PackageJson } from "type-fest";
import type { Disposer } from "../common/utils";
import { disposer } from "../common/utils"; import { disposer } from "../common/utils";
import type { LensExtensionDependencies } from "./lens-extension-set-dependencies"; import type { LensExtensionDependencies } from "./lens-extension-set-dependencies";
@ -88,15 +87,13 @@ export class LensExtension<Dependencies extends LensExtensionDependencies = Lens
} }
@action @action
async enable(register: (ext: this) => Promise<Disposer[]>) { async enable() {
if (this._isEnabled) { if (this._isEnabled) {
return; return;
} }
try { try {
this._isEnabled = true; this._isEnabled = true;
this[Disposers].push(...await register(this));
logger.info(`[EXTENSION]: enabled ${this.name}@${this.version}`); logger.info(`[EXTENSION]: enabled ${this.name}@${this.version}`);
} catch (error) { } catch (error) {

View File

@ -22,7 +22,7 @@ const initClusterFrameInjectable = getInjectable({
return initClusterFrame({ return initClusterFrame({
hostedCluster, hostedCluster,
loadExtensions: di.inject(extensionLoaderInjectable).loadOnClusterRenderer, loadExtensions: di.inject(extensionLoaderInjectable).autoInitExtensions,
catalogEntityRegistry: di.inject(catalogEntityRegistryInjectable), catalogEntityRegistry: di.inject(catalogEntityRegistryInjectable),
frameRoutingId: di.inject(frameRoutingIdInjectable), frameRoutingId: di.inject(frameRoutingIdInjectable),
emitAppEvent: di.inject(emitAppEventInjectable), emitAppEvent: di.inject(emitAppEventInjectable),

View File

@ -5,9 +5,7 @@
import type { Cluster } from "../../../../common/cluster/cluster"; import type { Cluster } from "../../../../common/cluster/cluster";
import type { CatalogEntityRegistry } from "../../../api/catalog/entity/registry"; import type { CatalogEntityRegistry } from "../../../api/catalog/entity/registry";
import logger from "../../../../main/logger"; import logger from "../../../../main/logger";
import type { KubernetesCluster } from "../../../../common/catalog-entities";
import { Notifications } from "../../../components/notifications"; import { Notifications } from "../../../components/notifications";
import type { CatalogEntity } from "../../../../common/catalog";
import { when } from "mobx"; import { when } from "mobx";
import type { ClusterFrameContext } from "../../../cluster-frame-context/cluster-frame-context"; import type { ClusterFrameContext } from "../../../cluster-frame-context/cluster-frame-context";
import { KubeObjectStore } from "../../../../common/k8s-api/kube-object.store"; import { KubeObjectStore } from "../../../../common/k8s-api/kube-object.store";
@ -16,7 +14,7 @@ import type { EmitAppEvent } from "../../../../common/app-event-bus/emit-event.i
interface Dependencies { interface Dependencies {
hostedCluster: Cluster; hostedCluster: Cluster;
loadExtensions: (getCluster: () => CatalogEntity) => void; loadExtensions: () => void;
catalogEntityRegistry: CatalogEntityRegistry; catalogEntityRegistry: CatalogEntityRegistry;
frameRoutingId: number; frameRoutingId: number;
emitAppEvent: EmitAppEvent; emitAppEvent: EmitAppEvent;
@ -53,7 +51,7 @@ export const initClusterFrame = ({
when( when(
() => catalogEntityRegistry.items.get().length > 0, () => catalogEntityRegistry.items.get().length > 0,
() => () =>
loadExtensions(() => catalogEntityRegistry.activeEntity as KubernetesCluster), loadExtensions(),
{ {
timeout: 15_000, timeout: 15_000,
onError: (error) => { onError: (error) => {

View File

@ -14,7 +14,7 @@ import registerIpcListenersInjectable from "../../../ipc/register-ipc-listeners.
const initRootFrameInjectable = getInjectable({ const initRootFrameInjectable = getInjectable({
id: "init-root-frame", id: "init-root-frame",
instantiate: (di) => initRootFrame({ instantiate: (di) => initRootFrame({
loadExtensions: di.inject(extensionLoaderInjectable).loadOnClusterManagerRenderer, loadExtensions: di.inject(extensionLoaderInjectable).autoInitExtensions,
registerIpcListeners: di.inject(registerIpcListenersInjectable), registerIpcListeners: di.inject(registerIpcListenersInjectable),
ipcRenderer: di.inject(ipcRendererInjectable), ipcRenderer: di.inject(ipcRendererInjectable),
bindProtocolAddRouteHandlers: di.inject(bindProtocolAddRouteHandlersInjectable), bindProtocolAddRouteHandlers: di.inject(bindProtocolAddRouteHandlersInjectable),