From edafcc916c0e1637823185a3ad0496f035df2359 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 17 Feb 2023 11:16:38 -0500 Subject: [PATCH] Conver catalog entity run to injectable IPC Signed-off-by: Sebastian Malton --- packages/core/src/common/ipc/catalog.ts | 9 ------- .../catalog/entity-run/common/channels.ts | 19 ++++++++++++++ .../entity-run/main/listener.injectable.ts | 19 ++++++++++++++ .../main-frame-listener.injectable.ts | 26 +++++++++++++++++++ .../renderer/request-entity-run.injectable.ts | 20 ++++++++++++++ .../renderer/api/catalog/entity/registry.ts | 21 ++++++--------- .../activate-entity-command.tsx | 9 ++++--- .../init-cluster-frame.injectable.ts | 3 --- .../root-frame/init-root-frame.injectable.ts | 4 --- 9 files changed, 98 insertions(+), 32 deletions(-) delete mode 100644 packages/core/src/common/ipc/catalog.ts create mode 100644 packages/core/src/features/catalog/entity-run/common/channels.ts create mode 100644 packages/core/src/features/catalog/entity-run/main/listener.injectable.ts create mode 100644 packages/core/src/features/catalog/entity-run/renderer/main-frame-listener.injectable.ts create mode 100644 packages/core/src/features/catalog/entity-run/renderer/request-entity-run.injectable.ts diff --git a/packages/core/src/common/ipc/catalog.ts b/packages/core/src/common/ipc/catalog.ts deleted file mode 100644 index 50d9a46704..0000000000 --- a/packages/core/src/common/ipc/catalog.ts +++ /dev/null @@ -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"; diff --git a/packages/core/src/features/catalog/entity-run/common/channels.ts b/packages/core/src/features/catalog/entity-run/common/channels.ts new file mode 100644 index 0000000000..61ae47089f --- /dev/null +++ b/packages/core/src/features/catalog/entity-run/common/channels.ts @@ -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 = { + id: "run-catalog-entity", +}; + +export const runCatalogEntityMainFrameChannel: MessageChannel = { + id: "run-catalog-entity-main-frame", +}; diff --git a/packages/core/src/features/catalog/entity-run/main/listener.injectable.ts b/packages/core/src/features/catalog/entity-run/main/listener.injectable.ts new file mode 100644 index 0000000000..71864cde0e --- /dev/null +++ b/packages/core/src/features/catalog/entity-run/main/listener.injectable.ts @@ -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; diff --git a/packages/core/src/features/catalog/entity-run/renderer/main-frame-listener.injectable.ts b/packages/core/src/features/catalog/entity-run/renderer/main-frame-listener.injectable.ts new file mode 100644 index 0000000000..ba8032559f --- /dev/null +++ b/packages/core/src/features/catalog/entity-run/renderer/main-frame-listener.injectable.ts @@ -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; diff --git a/packages/core/src/features/catalog/entity-run/renderer/request-entity-run.injectable.ts b/packages/core/src/features/catalog/entity-run/renderer/request-entity-run.injectable.ts new file mode 100644 index 0000000000..24e080268e --- /dev/null +++ b/packages/core/src/features/catalog/entity-run/renderer/request-entity-run.injectable.ts @@ -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; diff --git a/packages/core/src/renderer/api/catalog/entity/registry.ts b/packages/core/src/renderer/api/catalog/entity/registry.ts index fe3820ad07..336636bfe5 100644 --- a/packages/core/src/renderer/api/catalog/entity/registry.ts +++ b/packages/core/src/renderer/api/catalog/entity/registry.ts @@ -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 diff --git a/packages/core/src/renderer/components/activate-entity-command/activate-entity-command.tsx b/packages/core/src/renderer/components/activate-entity-command/activate-entity-command.tsx index 631415d804..ee49af555b 100644 --- a/packages/core/src/renderer/components/activate-entity-command/activate-entity-command.tsx +++ b/packages/core/src/renderer/components/activate-entity-command/activate-entity-command.tsx @@ -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; } const NonInjectedActivateEntityCommand = observer(({ closeCommandOverlay, + requestCatalogEntityRun, entities, }: Dependencies) => (