Skip to content

Commit

Permalink
Explicitly initialize registries
Browse files Browse the repository at this point in the history
- Helps moves towards turning on banning CyclicImports

Signed-off-by: Sebastian Malton <sebastian@malton.name>
  • Loading branch information
Nokel81 committed May 25, 2021
1 parent cbc2dd4 commit fe4ce27
Show file tree
Hide file tree
Showing 98 changed files with 1,132 additions and 1,092 deletions.
2 changes: 1 addition & 1 deletion src/common/protocol-handler/router.ts
Expand Up @@ -29,7 +29,7 @@ import { RoutingError, RoutingErrorType } from "./error";
import { ExtensionsStore } from "../../extensions/extensions-store";
import { ExtensionLoader } from "../../extensions/extension-loader";
import type { LensExtension } from "../../extensions/lens-extension";
import type { RouteHandler, RouteParams } from "../../extensions/registries/protocol-handler-registry";
import type { RouteHandler, RouteParams } from "../../extensions/registries/protocol-handler";

// IPC channel for protocol actions. Main broadcasts the open-url events to this channel.
export const ProtocolHandlerIpcPrefix = "protocol-handler";
Expand Down
28 changes: 15 additions & 13 deletions src/extensions/extension-loader.ts
Expand Up @@ -221,11 +221,13 @@ export class ExtensionLoader extends Singleton {
}

loadOnMain() {
registries.MenuRegistry.createInstance();

logger.debug(`${logModule}: load on main`);
this.autoInitExtensions(async (extension: LensMainExtension) => {
// Each .add returns a function to remove the item
const removeItems = [
registries.menuRegistry.add(extension.appMenus)
registries.MenuRegistry.getInstance().add(extension.appMenus)
];

this.events.on("remove", (removedExtension: LensRendererExtension) => {
Expand All @@ -244,12 +246,12 @@ export class ExtensionLoader extends Singleton {
logger.debug(`${logModule}: load on main renderer (cluster manager)`);
this.autoInitExtensions(async (extension: LensRendererExtension) => {
const removeItems = [
registries.globalPageRegistry.add(extension.globalPages, extension),
registries.appPreferenceRegistry.add(extension.appPreferences),
registries.entitySettingRegistry.add(extension.entitySettings),
registries.statusBarRegistry.add(extension.statusBarItems),
registries.commandRegistry.add(extension.commands),
registries.welcomeMenuRegistry.add(extension.welcomeMenus),
registries.GlobalPageRegistry.getInstance().add(extension.globalPages, extension),
registries.AppPreferenceRegistry.getInstance().add(extension.appPreferences),
registries.EntitySettingRegistry.getInstance().add(extension.entitySettings),
registries.StatusBarRegistry.getInstance().add(extension.statusBarItems),
registries.CommandRegistry.getInstance().add(extension.commands),
registries.WelcomeMenuRegistry.getInstance().add(extension.welcomeMenus),
];

this.events.on("remove", (removedExtension: LensRendererExtension) => {
Expand All @@ -274,12 +276,12 @@ export class ExtensionLoader extends Singleton {
}

const removeItems = [
registries.clusterPageRegistry.add(extension.clusterPages, extension),
registries.clusterPageMenuRegistry.add(extension.clusterPageMenus, extension),
registries.kubeObjectMenuRegistry.add(extension.kubeObjectMenuItems),
registries.kubeObjectDetailRegistry.add(extension.kubeObjectDetailItems),
registries.kubeObjectStatusRegistry.add(extension.kubeObjectStatusTexts),
registries.commandRegistry.add(extension.commands),
registries.ClusterPageRegistry.getInstance().add(extension.clusterPages, extension),
registries.ClusterPageMenuRegistry.getInstance().add(extension.clusterPageMenus, extension),
registries.KubeObjectMenuRegistry.getInstance().add(extension.kubeObjectMenuItems),
registries.KubeObjectDetailRegistry.getInstance().add(extension.kubeObjectDetailItems),
registries.KubeObjectStatusRegistry.getInstance().add(extension.kubeObjectStatusTexts),
registries.CommandRegistry.getInstance().add(extension.commands),
];

this.events.on("remove", (removedExtension: LensRendererExtension) => {
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/interfaces/registrations.ts
Expand Up @@ -26,4 +26,4 @@ export type { KubeObjectStatusRegistration } from "../registries/kube-object-sta
export type { PageRegistration, RegisteredPage, PageParams, PageComponentProps, PageComponents, PageTarget } from "../registries/page-registry";
export type { PageMenuRegistration, ClusterPageMenuRegistration, PageMenuComponents } from "../registries/page-menu-registry";
export type { StatusBarRegistration } from "../registries/status-bar-registry";
export type { ProtocolHandlerRegistration, RouteParams as ProtocolRouteParams, RouteHandler as ProtocolRouteHandler } from "../registries/protocol-handler-registry";
export type { ProtocolHandlerRegistration, RouteParams as ProtocolRouteParams, RouteHandler as ProtocolRouteHandler } from "../registries/protocol-handler";
24 changes: 18 additions & 6 deletions src/extensions/registries/__tests__/page-registry.test.ts
Expand Up @@ -19,7 +19,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { getExtensionPageUrl, globalPageRegistry, PageParams } from "../page-registry";
import { ClusterPageRegistry, getExtensionPageUrl, GlobalPageRegistry, PageParams } from "../page-registry";
import { LensExtension } from "../../lens-extension";
import React from "react";
import { Console } from "console";
Expand All @@ -42,7 +42,8 @@ describe("getPageUrl", () => {
isBundled: false,
isEnabled: true
});
globalPageRegistry.add({
ClusterPageRegistry.createInstance();
GlobalPageRegistry.createInstance().add({
id: "page-with-params",
components: {
Page: () => React.createElement("Page with params")
Expand All @@ -54,6 +55,11 @@ describe("getPageUrl", () => {
}, ext);
});

afterEach(() => {
GlobalPageRegistry.resetInstance();
ClusterPageRegistry.resetInstance();
});

it("returns a page url for extension", () => {
expect(getExtensionPageUrl({ extensionId: ext.name })).toBe("/extension/foo-bar");
});
Expand Down Expand Up @@ -109,7 +115,8 @@ describe("globalPageRegistry", () => {
isBundled: false,
isEnabled: true
});
globalPageRegistry.add([
ClusterPageRegistry.createInstance();
GlobalPageRegistry.createInstance().add([
{
id: "test-page",
components: {
Expand All @@ -130,17 +137,22 @@ describe("globalPageRegistry", () => {
], ext);
});

afterEach(() => {
GlobalPageRegistry.resetInstance();
ClusterPageRegistry.resetInstance();
});

describe("getByPageTarget", () => {
it("matching to first registered page without id", () => {
const page = globalPageRegistry.getByPageTarget({ extensionId: ext.name });
const page = GlobalPageRegistry.getInstance().getByPageTarget({ extensionId: ext.name });

expect(page.id).toEqual(undefined);
expect(page.extensionId).toEqual(ext.name);
expect(page.url).toEqual(getExtensionPageUrl({ extensionId: ext.name }));
});

it("returns matching page", () => {
const page = globalPageRegistry.getByPageTarget({
const page = GlobalPageRegistry.getInstance().getByPageTarget({
pageId: "test-page",
extensionId: ext.name
});
Expand All @@ -149,7 +161,7 @@ describe("globalPageRegistry", () => {
});

it("returns null if target not found", () => {
const page = globalPageRegistry.getByPageTarget({
const page = GlobalPageRegistry.getInstance().getByPageTarget({
pageId: "wrong-page",
extensionId: ext.name
});
Expand Down
2 changes: 0 additions & 2 deletions src/extensions/registries/app-preference-registry.ts
Expand Up @@ -46,5 +46,3 @@ export class AppPreferenceRegistry extends BaseRegistry<AppPreferenceRegistratio
};
}
}

export const appPreferenceRegistry = new AppPreferenceRegistry();
4 changes: 3 additions & 1 deletion src/extensions/registries/base-registry.ts
Expand Up @@ -21,12 +21,14 @@

// Base class for extensions-api registries
import { action, observable, makeObservable } from "mobx";
import { Singleton } from "../core-api/utils";
import { LensExtension } from "../lens-extension";

export class BaseRegistry<T, I = T> {
export class BaseRegistry<T, I = T> extends Singleton {
private items = observable.map<T, I>();

constructor() {
super();
makeObservable(this);
}

Expand Down
2 changes: 0 additions & 2 deletions src/extensions/registries/command-registry.ts
Expand Up @@ -59,5 +59,3 @@ export class CommandRegistry extends BaseRegistry<CommandRegistration> {
return super.add(filteredItems, extension);
}
}

export const commandRegistry = new CommandRegistry();
2 changes: 0 additions & 2 deletions src/extensions/registries/entity-setting-registry.ts
Expand Up @@ -68,5 +68,3 @@ export class EntitySettingRegistry extends BaseRegistry<EntitySettingRegistratio
return items.sort((a, b) => (b.priority ?? 50) - (a.priority ?? 50));
}
}

export const entitySettingRegistry = new EntitySettingRegistry();
2 changes: 1 addition & 1 deletion src/extensions/registries/index.ts
Expand Up @@ -32,4 +32,4 @@ export * from "./kube-object-status-registry";
export * from "./command-registry";
export * from "./entity-setting-registry";
export * from "./welcome-menu-registry";
export * from "./protocol-handler-registry";
export * from "./protocol-handler";
6 changes: 3 additions & 3 deletions src/extensions/registries/kube-object-detail-registry.ts
Expand Up @@ -20,10 +20,12 @@
*/

import type React from "react";
import type { KubeObjectDetailsProps } from "../renderer-api/components";
import type { KubeObject } from "../renderer-api/k8s-api";
import { BaseRegistry } from "./base-registry";

export interface KubeObjectDetailComponents {
Details: React.ComponentType<any>;
Details: React.ComponentType<KubeObjectDetailsProps<KubeObject>>;
}

export interface KubeObjectDetailRegistration {
Expand All @@ -42,5 +44,3 @@ export class KubeObjectDetailRegistry extends BaseRegistry<KubeObjectDetailRegis
return items.sort((a, b) => (b.priority ?? 50) - (a.priority ?? 50));
}
}

export const kubeObjectDetailRegistry = new KubeObjectDetailRegistry();
2 changes: 0 additions & 2 deletions src/extensions/registries/kube-object-menu-registry.ts
Expand Up @@ -39,5 +39,3 @@ export class KubeObjectMenuRegistry extends BaseRegistry<KubeObjectMenuRegistrat
});
}
}

export const kubeObjectMenuRegistry = new KubeObjectMenuRegistry();
2 changes: 0 additions & 2 deletions src/extensions/registries/kube-object-status-registry.ts
Expand Up @@ -43,5 +43,3 @@ export class KubeObjectStatusRegistry extends BaseRegistry<KubeObjectStatusRegis
.filter(Boolean);
}
}

export const kubeObjectStatusRegistry = new KubeObjectStatusRegistry();
2 changes: 0 additions & 2 deletions src/extensions/registries/menu-registry.ts
Expand Up @@ -30,5 +30,3 @@ export interface MenuRegistration extends MenuItemConstructorOptions {

export class MenuRegistry extends BaseRegistry<MenuRegistration> {
}

export const menuRegistry = new MenuRegistry();
2 changes: 0 additions & 2 deletions src/extensions/registries/page-menu-registry.ts
Expand Up @@ -75,5 +75,3 @@ export class ClusterPageMenuRegistry extends PageMenuRegistry<ClusterPageMenuReg
));
}
}

export const clusterPageMenuRegistry = new ClusterPageMenuRegistry();
8 changes: 4 additions & 4 deletions src/extensions/registries/page-registry.ts
Expand Up @@ -75,7 +75,7 @@ export function getExtensionPageUrl(target: PageTarget): string {
const pageUrl = new URL(pagePath, `http://localhost`);

// stringify params to matched target page
const registeredPage = globalPageRegistry.getByPageTarget(target) || clusterPageRegistry.getByPageTarget(target);
const registeredPage = GlobalPageRegistry.getInstance().getByPageTarget(target) || ClusterPageRegistry.getInstance().getByPageTarget(target);

if (registeredPage?.params) {
Object.entries(registeredPage.params).forEach(([name, param]) => {
Expand All @@ -91,7 +91,7 @@ export function getExtensionPageUrl(target: PageTarget): string {
return pageUrl.href.replace(pageUrl.origin, "");
}

export class PageRegistry extends BaseRegistry<PageRegistration, RegisteredPage> {
class PageRegistry extends BaseRegistry<PageRegistration, RegisteredPage> {
protected getRegisteredItem(page: PageRegistration, ext: LensExtension): RegisteredPage {
const { id: pageId } = page;
const extensionId = ext.name;
Expand Down Expand Up @@ -157,5 +157,5 @@ export class PageRegistry extends BaseRegistry<PageRegistration, RegisteredPage>
}
}

export const globalPageRegistry = new PageRegistry();
export const clusterPageRegistry = new PageRegistry();
export class ClusterPageRegistry extends PageRegistry {}
export class GlobalPageRegistry extends PageRegistry {}
2 changes: 0 additions & 2 deletions src/extensions/registries/status-bar-registry.ts
Expand Up @@ -41,5 +41,3 @@ export interface StatusBarRegistration extends StatusBarRegistrationV2 {

export class StatusBarRegistry extends BaseRegistry<StatusBarRegistration> {
}

export const statusBarRegistry = new StatusBarRegistry();
2 changes: 0 additions & 2 deletions src/extensions/registries/welcome-menu-registry.ts
Expand Up @@ -28,5 +28,3 @@ export interface WelcomeMenuRegistration {
}

export class WelcomeMenuRegistry extends BaseRegistry<WelcomeMenuRegistration> {}

export const welcomeMenuRegistry = new WelcomeMenuRegistry();
2 changes: 2 additions & 0 deletions src/main/index.ts
Expand Up @@ -56,6 +56,7 @@ import { HelmRepoManager } from "./helm/helm-repo-manager";
import { KubeconfigSyncManager } from "./catalog-sources";
import { handleWsUpgrade } from "./proxy/ws-upgrade";
import configurePackages from "../common/configure-packages";
import { initRegistries } from "./initializers";

const workingDir = path.join(app.getPath("appData"), appName);
const cleanup = disposer();
Expand Down Expand Up @@ -171,6 +172,7 @@ app.on("ready", async () => {
app.exit();
}

initRegistries();
const extensionDiscovery = ExtensionDiscovery.createInstance();

ExtensionLoader.createInstance().init();
Expand Down
Expand Up @@ -19,20 +19,4 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { navigate } from "../../navigation";
import { commandRegistry } from "../../../extensions/registries/command-registry";
import { helmChartsURL, releaseURL } from "../../../common/routes";

commandRegistry.add({
id: "cluster.viewHelmCharts",
title: "Cluster: View Helm Charts",
scope: "entity",
action: () => navigate(helmChartsURL())
});

commandRegistry.add({
id: "cluster.viewHelmReleases",
title: "Cluster: View Helm Releases",
scope: "entity",
action: () => navigate(releaseURL())
});
export * from "./registries";
Expand Up @@ -19,13 +19,8 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { navigate } from "../../navigation";
import { commandRegistry } from "../../../extensions/registries/command-registry";
import { nodesURL } from "../../../common/routes";
import * as registries from "../../extensions/registries";

commandRegistry.add({
id: "cluster.viewNodes",
title: "Cluster: View Nodes",
scope: "entity",
action: () => navigate(nodesURL())
});
export function initRegistries() {
registries.MenuRegistry.createInstance();
}
4 changes: 2 additions & 2 deletions src/main/menu.ts
Expand Up @@ -23,7 +23,7 @@ import { app, BrowserWindow, dialog, ipcMain, IpcMainEvent, Menu, MenuItem, Menu
import { autorun } from "mobx";
import type { WindowManager } from "./window-manager";
import { appName, isMac, isWindows, isTestEnv, docsUrl, supportUrl, productName } from "../common/vars";
import { menuRegistry } from "../extensions/registries/menu-registry";
import { MenuRegistry } from "../extensions/registries/menu-registry";
import logger from "./logger";
import { exitApp } from "./exit-app";
import { broadcastMessage } from "../common/ipc";
Expand Down Expand Up @@ -255,7 +255,7 @@ export function buildMenu(windowManager: WindowManager) {
};

// Modify menu from extensions-api
menuRegistry.getItems().forEach(({ parentId, ...menuItem }) => {
MenuRegistry.getInstance().getItems().forEach(({ parentId, ...menuItem }) => {
try {
const topMenu = appMenu[parentId as MenuTopId].submenu as MenuItemConstructorOptions[];

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/api/catalog-entity.ts
Expand Up @@ -20,7 +20,7 @@
*/

import { navigate } from "../navigation";
import { commandRegistry } from "../../extensions/registries";
import { CommandRegistry } from "../../extensions/registries";
import type { CatalogEntity } from "../../common/catalog";

export { CatalogCategory, CatalogEntity } from "../../common/catalog";
Expand All @@ -37,6 +37,6 @@ export type {
export const catalogEntityRunContext = {
navigate: (url: string) => navigate(url),
setCommandPaletteContext: (entity?: CatalogEntity) => {
commandRegistry.activeEntity = entity;
CommandRegistry.getInstance().activeEntity = entity;
}
};
2 changes: 1 addition & 1 deletion src/renderer/api/kube-object-detail-registry.ts
Expand Up @@ -19,4 +19,4 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

export { kubeObjectDetailRegistry } from "../../extensions/registries/kube-object-detail-registry";
export { KubeObjectDetailRegistry } from "../../extensions/registries/kube-object-detail-registry";
8 changes: 8 additions & 0 deletions src/renderer/bootstrap.tsx
Expand Up @@ -45,6 +45,7 @@ import { HelmRepoManager } from "../main/helm/helm-repo-manager";
import { ExtensionInstallationStateStore } from "./components/+extensions/extension-install.store";
import { DefaultProps } from "./mui-base-theme";
import configurePackages from "../common/configure-packages";
import * as initializers from "./initializers";

configurePackages();

Expand All @@ -69,6 +70,13 @@ export async function bootstrap(App: AppComponent) {
await attachChromeDebugger();
rootElem.classList.toggle("is-mac", isMac);

initializers.initRegistries();
initializers.initCommandRegistry();
initializers.initEntitySettingsRegistry();
initializers.initKubeObjectMenuRegistry();
initializers.intiKubeObjectDetailRegistry();
initializers.initWelcomeMenuRegistry();

ExtensionLoader.createInstance().init();
ExtensionDiscovery.createInstance().init();

Expand Down
1 change: 0 additions & 1 deletion src/renderer/components/+apps/index.ts
Expand Up @@ -20,4 +20,3 @@
*/

export * from "./apps";
export * from "./apps.command";

0 comments on commit fe4ce27

Please sign in to comment.