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

Conver catalog entity run to injectable IPC

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-02-17 11:16:38 -05:00
parent b0e2660dfa
commit edafcc916c
9 changed files with 98 additions and 32 deletions

View File

@ -1,9 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
/**
* This is used to activate a specific entity in the renderer main frame
*/
export const catalogEntityRunListener = "catalog-entity:run";

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
/**
* These channels are seperated so that the `main` environment can be used as a launchpad between
* `renderer` environments (iframes)
*/
import type { MessageChannel } from "../../../../common/utils/channel/message-channel-listener-injection-token";
export const runCatalogEntityChannel: MessageChannel<string> = {
id: "run-catalog-entity",
};
export const runCatalogEntityMainFrameChannel: MessageChannel<string> = {
id: "run-catalog-entity-main-frame",
};

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getMessageChannelListenerInjectable } from "../../../../common/utils/channel/message-channel-listener-injection-token";
import { sendMessageToChannelInjectionToken } from "../../../../common/utils/channel/message-to-channel-injection-token";
import { runCatalogEntityChannel, runCatalogEntityMainFrameChannel } from "../common/channels";
const entityRunListenerInjectable = getMessageChannelListenerInjectable({
channel: runCatalogEntityChannel,
id: "main",
handler: (di) => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
return (id) => sendMessageToChannel(runCatalogEntityMainFrameChannel, id);
},
});
export default entityRunListenerInjectable;

View File

@ -0,0 +1,26 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { noop } from "../../../../common/utils";
import { getMessageChannelListenerInjectable } from "../../../../common/utils/channel/message-channel-listener-injection-token";
import catalogEntityRegistryInjectable from "../../../../renderer/api/catalog/entity/registry.injectable";
import currentlyInClusterFrameInjectable from "../../../../renderer/routes/currently-in-cluster-frame.injectable";
import { runCatalogEntityMainFrameChannel } from "../common/channels";
const entityRunMainFrameListenerInjectable = getMessageChannelListenerInjectable({
channel: runCatalogEntityMainFrameChannel,
id: "main",
handler: (di) => {
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
const currentlyInClusterFrame = di.inject(currentlyInClusterFrameInjectable);
if (currentlyInClusterFrame) {
return noop;
}
return (id) => catalogEntityRegistry.onRunById(id);
},
});
export default entityRunMainFrameListenerInjectable;

View File

@ -0,0 +1,20 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { sendMessageToChannelInjectionToken } from "../../../../common/utils/channel/message-to-channel-injection-token";
import { runCatalogEntityChannel } from "../common/channels";
export type RequestCatalogEntityRun = (id: string) => void;
const requestCatalogEntityRunInjectable = getInjectable({
id: "request-catalog-entity-run",
instantiate: (di): RequestCatalogEntityRun => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
return (id) => sendMessageToChannel(runCatalogEntityChannel, id);
},
});
export default requestCatalogEntityRunInjectable;

View File

@ -4,15 +4,11 @@
*/
import { computed, observable, makeObservable, action } from "mobx";
import { ipcRendererOn } from "../../../../common/ipc";
import type { CatalogCategory, CatalogEntity, CatalogEntityData, CatalogCategoryRegistry, CatalogEntityKindData } from "../../../../common/catalog";
import "../../../../common/catalog-entities";
import { iter } from "../../../utils";
import type { Disposer } from "../../../utils";
import { once } from "lodash";
import { CatalogRunEvent } from "../../../../common/catalog/catalog-run-event";
import { catalogEntityRunListener } from "../../../../common/ipc/catalog";
import { isMainFrame } from "process";
import type { Navigate } from "../../../navigation/navigate.injectable";
import type { Logger } from "../../../../common/logger";
@ -82,15 +78,6 @@ export class CatalogEntityRegistry {
}
init() {
if (isMainFrame) {
ipcRendererOn(catalogEntityRunListener, (event, id: string) => {
const entity = this.getById(id);
if (entity) {
this.onRun(entity);
}
});
}
}
@action updateItems(items: (CatalogEntityData & CatalogEntityKindData)[]) {
@ -231,6 +218,14 @@ export class CatalogEntityRegistry {
return true;
}
onRunById(entityId: string): void {
const entity = this.getById(entityId);
if (entity) {
this.onRun(entity);
}
}
/**
* Perform the onBeforeRun check and, if successful, then proceed to call `entity`'s onRun method
* @param entity The instance to invoke the hooks and then execute the onRun

View File

@ -7,8 +7,8 @@ import { withInjectables } from "@ogre-tools/injectable-react";
import type { IComputedValue } from "mobx";
import { observer } from "mobx-react";
import React from "react";
import { broadcastMessage } from "../../../common/ipc";
import { catalogEntityRunListener } from "../../../common/ipc/catalog";
import type { RequestCatalogEntityRun } from "../../../features/catalog/entity-run/renderer/request-entity-run.injectable";
import requestCatalogEntityRunInjectable from "../../../features/catalog/entity-run/renderer/request-entity-run.injectable";
import type { CatalogEntity } from "../../api/catalog-entity";
import catalogEnitiesInjectable from "../../api/catalog/entity/entities.injectable";
import commandOverlayInjectable from "../command-palette/command-overlay.injectable";
@ -16,11 +16,13 @@ import { Select } from "../select";
interface Dependencies {
closeCommandOverlay: () => void;
requestCatalogEntityRun: RequestCatalogEntityRun;
entities: IComputedValue<CatalogEntity[]>;
}
const NonInjectedActivateEntityCommand = observer(({
closeCommandOverlay,
requestCatalogEntityRun,
entities,
}: Dependencies) => (
<Select
@ -28,7 +30,7 @@ const NonInjectedActivateEntityCommand = observer(({
menuPortalTarget={null}
onChange={(option) => {
if (option) {
broadcastMessage(catalogEntityRunListener, option.value.getId());
requestCatalogEntityRun(option.value.getId());
closeCommandOverlay();
}
}}
@ -51,5 +53,6 @@ export const ActivateEntityCommand = withInjectables<Dependencies>(NonInjectedAc
getProps: di => ({
closeCommandOverlay: di.inject(commandOverlayInjectable).close,
entities: di.inject(catalogEnitiesInjectable),
requestCatalogEntityRun: di.inject(requestCatalogEntityRunInjectable),
}),
});

View File

@ -31,9 +31,6 @@ const initClusterFrameInjectable = getInjectable({
const requestSetClusterFrameId = di.inject(requestSetClusterFrameIdInjectable);
return async (unmountRoot: () => void) => {
// TODO: Make catalogEntityRegistry already initialized when passed as dependency
catalogEntityRegistry.init();
logger.info(`Init dashboard, clusterId=${hostedCluster.id}, frameId=${frameRoutingId}`);
await requestSetClusterFrameId(hostedCluster.id);

View File

@ -5,7 +5,6 @@
import { getInjectable } from "@ogre-tools/injectable";
import bindProtocolAddRouteHandlersInjectable from "../../protocol-handler/bind-protocol-add-route-handlers/bind-protocol-add-route-handlers.injectable";
import lensProtocolRouterRendererInjectable from "../../protocol-handler/lens-protocol-router-renderer/lens-protocol-router-renderer.injectable";
import catalogEntityRegistryInjectable from "../../api/catalog/entity/registry.injectable";
import registerIpcListenersInjectable from "../../ipc/register-ipc-listeners.injectable";
import loggerInjectable from "../../../common/logger.injectable";
import { delay } from "../../../common/utils";
@ -20,13 +19,10 @@ const initRootFrameInjectable = getInjectable({
const registerIpcListeners = di.inject(registerIpcListenersInjectable);
const bindProtocolAddRouteHandlers = di.inject(bindProtocolAddRouteHandlersInjectable);
const lensProtocolRouterRenderer = di.inject(lensProtocolRouterRendererInjectable);
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
const logger = di.inject(loggerInjectable);
const sendBundledExtensionsLoaded = di.inject(sendBundledExtensionsLoadedInjectable);
return async (unmountRoot: () => void) => {
catalogEntityRegistry.init();
try {
// maximum time to let bundled extensions finish loading
const timeout = delay(10000);