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

Convert seting cluster frame to be IPC injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-02-17 09:50:12 -05:00
parent 1058113e90
commit fe86b79adb
15 changed files with 176 additions and 71 deletions

View File

@ -3,12 +3,17 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { clusterFrameMap } from "./cluster-frames";
import { observable } from "mobx";
import type { ClusterId } from "./cluster-types";
export interface ClusterFrameInfo {
frameId: number;
processId: number;
}
const clusterFramesInjectable = getInjectable({
id: "cluster-frames",
instantiate: () => clusterFrameMap,
causesSideEffects: true,
instantiate: () => observable.map<ClusterId, ClusterFrameInfo>(),
});
export default clusterFramesInjectable;

View File

@ -1,13 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { observable } from "mobx";
export interface ClusterFrameInfo {
frameId: number;
processId: number;
}
export const clusterFrameMap = observable.map<string, ClusterFrameInfo>();

View File

@ -4,7 +4,6 @@
*/
export const clusterActivateHandler = "cluster:activate";
export const clusterSetFrameIdHandler = "cluster:set-frame-id";
export const clusterVisibilityHandler = "cluster:visibility";
export const clusterDisconnectHandler = "cluster:disconnect";
export const clusterStates = "cluster:states";

View File

@ -9,13 +9,12 @@
import { ipcMain, ipcRenderer, webContents } from "electron";
import { toJS } from "../utils/toJS";
import type { ClusterFrameInfo } from "../cluster-frames";
import { clusterFrameMap } from "../cluster-frames";
import type { Disposer } from "../utils";
import { getLegacyGlobalDiForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
import ipcRendererInjectable from "../../renderer/utils/channel/ipc-renderer.injectable";
import loggerInjectable from "../logger.injectable";
import ipcMainInjectionToken from "./ipc-main-injection-token";
import clusterFramesInjectable from "../cluster-frames.injectable";
export const broadcastMainChannel = "ipc:broadcast-main";
@ -29,10 +28,6 @@ export function ipcMainHandle(channel: string, listener: (event: Electron.IpcMai
});
}
function getSubFrames(): ClusterFrameInfo[] {
return Array.from(clusterFrameMap.values());
}
export async function broadcastMessage(channel: string, ...args: any[]): Promise<void> {
if (ipcRenderer) {
return ipcRenderer.invoke(broadcastMainChannel, channel, ...args.map(sanitizePayload));
@ -44,12 +39,13 @@ export async function broadcastMessage(channel: string, ...args: any[]): Promise
const di = getLegacyGlobalDiForExtensionApi();
const logger = di.inject(loggerInjectable);
const clusterFrames = di.inject(clusterFramesInjectable);
ipcMain.listeners(channel).forEach((func) => func({
processId: undefined, frameId: undefined, sender: undefined, senderFrame: undefined,
}, ...args));
const subFrames = getSubFrames();
const subFrames = [...clusterFrames.values()];
const views = webContents.getAllWebContents();
if (!views || !Array.isArray(views) || views.length === 0) return;

View File

@ -10,6 +10,10 @@ export interface RequestFromChannel {
<Response>(channel: RequestChannel<void, Response>): Promise<Response>;
}
export type ChannelRequester<Channel> = Channel extends RequestChannel<infer Req, infer Res>
? (req: Req) => Promise<Res>
: never;
export const requestFromChannelInjectionToken = getInjectionToken<RequestFromChannel>({
id: "request-from-request-channel",
});

View File

@ -0,0 +1,11 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { ClusterId } from "../../../../common/cluster-types";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-listener-injection-token";
export const setClusterFrameIdChannel: RequestChannel<ClusterId, void> = {
id: "set-cluster-frame-id",
};

View File

@ -0,0 +1,29 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import clusterFramesInjectable from "../../../../common/cluster-frames.injectable";
import getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable";
import pushCatalogToRendererInjectable from "../../../../main/catalog-sync-to-renderer/push-catalog-to-renderer.injectable";
import { getRawRequestChannelListenerInjectable } from "../../../../main/utils/channel/channel-listeners/listener-tokens";
import { setClusterFrameIdChannel } from "../common/channel";
const handleSetClusterFrameIdInjectable = getRawRequestChannelListenerInjectable({
channel: setClusterFrameIdChannel,
handler: (di) => {
const getClusterById = di.inject(getClusterByIdInjectable);
const clusterFrames = di.inject(clusterFramesInjectable);
const pushCatalogToRenderer = di.inject(pushCatalogToRendererInjectable);
return (event, clusterId) => {
const cluster = getClusterById(clusterId);
if (cluster) {
clusterFrames.set(cluster.id, { frameId: event.frameId, processId: event.processId });
pushCatalogToRenderer();
}
};
},
});
export default handleSetClusterFrameIdInjectable;

View File

@ -0,0 +1,21 @@
/**
* 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 type { ChannelRequester } from "../../../../common/utils/channel/request-from-channel-injection-token";
import requestFromChannelInjectable from "../../../../renderer/utils/channel/request-from-channel.injectable";
import { setClusterFrameIdChannel } from "../common/channel";
export type RequestSetClusterFrameId = ChannelRequester<typeof setClusterFrameIdChannel>;
const requestSetClusterFrameIdInjectable = getInjectable({
id: "request-set-cluster-frame-id",
instantiate: (di): RequestSetClusterFrameId => {
const requestFromChannel = di.inject(requestFromChannelInjectable);
return (clusterId) => requestFromChannel(setClusterFrameIdChannel, clusterId);
},
});
export default requestSetClusterFrameIdInjectable;

View File

@ -4,40 +4,33 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import { setupIpcMainHandlers } from "./setup-ipc-main-handlers";
import loggerInjectable from "../../../../common/logger.injectable";
import clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
import { onLoadOfApplicationInjectionToken } from "../../../start-main-application/runnable-tokens/on-load-of-application-injection-token";
import applicationMenuItemCompositeInjectable from "../../../../features/application-menu/main/application-menu-item-composite.injectable";
import emitAppEventInjectable from "../../../../common/app-event-bus/emit-event.injectable";
import getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable";
import pushCatalogToRendererInjectable from "../../../catalog-sync-to-renderer/push-catalog-to-renderer.injectable";
import clusterFramesInjectable from "../../../../common/cluster-frames.injectable";
import prefixedLoggerInjectable from "../../../../common/logger/prefixed-logger.injectable";
const setupIpcMainHandlersInjectable = getInjectable({
id: "setup-ipc-main-handlers",
instantiate: (di) => {
const logger = di.inject(loggerInjectable);
const applicationMenuItemComposite = di.inject(applicationMenuItemCompositeInjectable);
const pushCatalogToRenderer = di.inject(pushCatalogToRendererInjectable);
const clusterStore = di.inject(clusterStoreInjectable);
const emitAppEvent = di.inject(emitAppEventInjectable);
const getClusterById = di.inject(getClusterByIdInjectable);
instantiate: (di) => ({
id: "setup-ipc-main-handlers",
run: () => {
const logger = di.inject(prefixedLoggerInjectable, "APP-MAIN");
return {
id: "setup-ipc-main-handlers",
run: () => {
logger.debug("[APP-MAIN] initializing ipc main handlers");
logger.debug("initializing ipc main handlers");
setupIpcMainHandlers({
applicationMenuItemComposite,
pushCatalogToRenderer,
clusterStore,
emitAppEvent,
getClusterById,
});
},
};
},
setupIpcMainHandlers({
applicationMenuItemComposite: di.inject(applicationMenuItemCompositeInjectable),
clusterStore: di.inject(clusterStoreInjectable),
emitAppEvent: di.inject(emitAppEventInjectable),
getClusterById: di.inject(getClusterByIdInjectable),
clusterFrameMap: di.inject(clusterFramesInjectable),
});
},
}),
injectionToken: onLoadOfApplicationInjectionToken,
causesSideEffects: true,

View File

@ -2,14 +2,12 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { IpcMainInvokeEvent } from "electron";
import { BrowserWindow, Menu } from "electron";
import { clusterFrameMap } from "../../../../common/cluster-frames";
import { clusterActivateHandler, clusterSetFrameIdHandler, clusterDisconnectHandler, clusterStates } from "../../../../common/ipc/cluster";
import { clusterActivateHandler, clusterDisconnectHandler, clusterStates } from "../../../../common/ipc/cluster";
import type { ClusterId } from "../../../../common/cluster-types";
import type { ClusterStore } from "../../../../common/cluster-store/cluster-store";
import { broadcastMainChannel, broadcastMessage, ipcMainHandle, ipcMainOn } from "../../../../common/ipc";
import type { IComputedValue } from "mobx";
import type { IComputedValue, ObservableMap } from "mobx";
import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppMenuAsContextMenuChannel } from "../../../../common/ipc/window";
import { handleWindowAction, onLocationChange } from "../../../ipc/window";
import type { ApplicationMenuItemTypes } from "../../../../features/application-menu/main/menu-items/application-menu-item-injection-token";
@ -18,12 +16,13 @@ import { getApplicationMenuTemplate } from "../../../../features/application-men
import type { MenuItemRoot } from "../../../../features/application-menu/main/application-menu-item-composite.injectable";
import type { EmitAppEvent } from "../../../../common/app-event-bus/emit-event.injectable";
import type { GetClusterById } from "../../../../common/cluster-store/get-by-id.injectable";
import type { ClusterFrameInfo } from "../../../../common/cluster-frames.injectable";
interface Dependencies {
applicationMenuItemComposite: IComputedValue<Composite<ApplicationMenuItemTypes | MenuItemRoot>>;
clusterStore: ClusterStore;
emitAppEvent: EmitAppEvent;
getClusterById: GetClusterById;
pushCatalogToRenderer: () => void;
clusterFrameMap: ObservableMap<ClusterId, ClusterFrameInfo>;
}
export const setupIpcMainHandlers = ({
@ -31,22 +30,13 @@ export const setupIpcMainHandlers = ({
clusterStore,
emitAppEvent,
getClusterById,
pushCatalogToRenderer,
clusterFrameMap,
}: Dependencies) => {
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
return getClusterById(clusterId)
?.activate(force);
});
ipcMainHandle(clusterSetFrameIdHandler, (event: IpcMainInvokeEvent, clusterId: ClusterId) => {
const cluster = getClusterById(clusterId);
if (cluster) {
clusterFrameMap.set(cluster.id, { frameId: event.frameId, processId: event.processId });
pushCatalogToRenderer();
}
});
ipcMainHandle(clusterDisconnectHandler, (event, clusterId: ClusterId) => {
emitAppEvent({ name: "cluster", action: "stop" });
const cluster = getClusterById(clusterId);

View File

@ -0,0 +1,29 @@
/**
* 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 type { Disposer } from "../../../../common/utils";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-listener-injection-token";
import type { RawRequestChannelListener } from "./listener-tokens";
import ipcMainInjectionToken from "../../../../common/ipc/ipc-main-injection-token";
export type EnlistRawRequestChannelListener = <TChannel extends RequestChannel<unknown, unknown>>(listener: RawRequestChannelListener<TChannel>) => Disposer;
const enlistRawRequestChannelListenerInjectable = getInjectable({
id: "enlist-raw-request-channel-listener-for-main",
instantiate: (di): EnlistRawRequestChannelListener => {
const ipcMain = di.inject(ipcMainInjectionToken);
return ({ channel, handler }) => {
ipcMain.handle(channel.id, handler);
return () => {
ipcMain.off(channel.id, handler);
};
};
},
});
export default enlistRawRequestChannelListenerInjectable;

View File

@ -5,22 +5,36 @@
import type { DiContainerForInjection } from "@ogre-tools/injectable";
import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
import type { IpcMainInvokeEvent } from "electron";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-listener-injection-token";
export type RequestChannelHandler<Channel> = Channel extends RequestChannel<infer Request, infer Response>
? (req: Request) => Promise<Response> | Response
: never;
export type RawRequestChannelHandler<Channel> = Channel extends RequestChannel<infer Request, infer Response>
? (event: IpcMainInvokeEvent, req: Request) => Promise<Response> | Response
: never;
export interface RequestChannelListener<Channel> {
channel: Channel;
handler: RequestChannelHandler<Channel>;
}
export interface RawRequestChannelListener<Channel> {
channel: Channel;
handler: RawRequestChannelHandler<Channel>;
}
export const requestChannelListenerInjectionToken = getInjectionToken<RequestChannelListener<RequestChannel<unknown, unknown>>>( {
id: "request-channel-listener",
});
export const rawRequestChannelListenerInjectionToken = getInjectionToken<RawRequestChannelListener<RequestChannel<unknown, unknown>>>( {
id: "raw-request-channel-listener",
});
export interface GetRequestChannelListenerInjectableInfo<
Channel extends RequestChannel<Request, Response>,
Request,
@ -44,3 +58,27 @@ export function getRequestChannelListenerInjectable<
injectionToken: requestChannelListenerInjectionToken,
});
}
export interface GeRawtRequestChannelListenerInjectableInfo<
Channel extends RequestChannel<Request, Response>,
Request,
Response,
> {
channel: Channel;
handler: (di: DiContainerForInjection) => RawRequestChannelHandler<Channel>;
}
export function getRawRequestChannelListenerInjectable<
Channel extends RequestChannel<Request, Response>,
Request,
Response,
>(info: GeRawtRequestChannelListenerInjectableInfo<Channel, Request, Response>) {
return getInjectable({
id: `${info.channel.id}-listener`,
instantiate: (di) => ({
channel: info.channel,
handler: info.handler(di),
}),
injectionToken: rawRequestChannelListenerInjectionToken,
});
}

View File

@ -6,19 +6,22 @@ import { getInjectable } from "@ogre-tools/injectable";
import { disposer } from "../../../../common/utils";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-listener-injection-token";
import { getStartableStoppable } from "../../../../common/utils/get-startable-stoppable";
import enlistRawRequestChannelListenerInjectable from "./enlist-raw-request-channel-listener.injectable";
import enlistRequestChannelListenerInjectable from "./enlist-request-channel-listener.injectable";
import { requestChannelListenerInjectionToken } from "./listener-tokens";
import { rawRequestChannelListenerInjectionToken, requestChannelListenerInjectionToken } from "./listener-tokens";
const listeningOnRequestChannelsInjectable = getInjectable({
id: "listening-on-request-channels",
instantiate: (di) => {
const enlistRequestChannelListener = di.inject(enlistRequestChannelListenerInjectable);
const enlistRawRequestChannelListener = di.inject(enlistRawRequestChannelListenerInjectable);
const requestChannelListeners = di.injectMany(requestChannelListenerInjectionToken);
const rawRequestChannelListeners = di.injectMany(rawRequestChannelListenerInjectionToken);
return getStartableStoppable("listening-on-request-channels", () => {
const seenChannels = new Set<RequestChannel<unknown, unknown>>();
for (const listener of requestChannelListeners) {
for (const listener of [...requestChannelListeners, ...rawRequestChannelListeners]) {
if (seenChannels.has(listener.channel)) {
throw new Error(`Tried to register a multiple channel handlers for "${listener.channel.id}", only one handler is supported for a request channel.`);
}
@ -26,7 +29,10 @@ const listeningOnRequestChannelsInjectable = getInjectable({
seenChannels.add(listener.channel);
}
return disposer(requestChannelListeners.map(enlistRequestChannelListener));
return disposer(
requestChannelListeners.map(enlistRequestChannelListener),
rawRequestChannelListeners.map(enlistRawRequestChannelListener),
);
});
},
});

View File

@ -12,7 +12,7 @@ import showErrorNotificationInjectable from "../../components/notifications/show
import autoInitExtensionsInjectable from "../../../features/extensions/loader/common/auto-init-extensions.injectable";
import prefixedLoggerInjectable from "../../../common/logger/prefixed-logger.injectable";
import { when } from "mobx";
import { requestSetClusterFrameId } from "../../ipc";
import requestSetClusterFrameIdInjectable from "../../../features/cluster/frame-id/renderer/request-set-frame-id.injectable";
const initClusterFrameInjectable = getInjectable({
id: "init-cluster-frame",
@ -28,6 +28,7 @@ const initClusterFrameInjectable = getInjectable({
const emitAppEvent = di.inject(emitAppEventInjectable);
const logger = di.inject(prefixedLoggerInjectable, "CLUSTER-FRAME");
const showErrorNotification = di.inject(showErrorNotificationInjectable);
const requestSetClusterFrameId = di.inject(requestSetClusterFrameIdInjectable);
return async (unmountRoot: () => void) => {
// TODO: Make catalogEntityRegistry already initialized when passed as dependency

View File

@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { clusterActivateHandler, clusterDisconnectHandler, clusterSetFrameIdHandler, clusterStates } from "../../common/ipc/cluster";
import { clusterActivateHandler, clusterDisconnectHandler, clusterStates } from "../../common/ipc/cluster";
import type { ClusterId, ClusterState } from "../../common/cluster-types";
import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppMenuAsContextMenuChannel, type WindowAction } from "../../common/ipc/window";
import { toJS } from "../utils";
@ -39,10 +39,6 @@ export function requestWindowAction(type: WindowAction): Promise<void> {
return requestMain(windowActionHandleChannel, type);
}
export function requestSetClusterFrameId(clusterId: ClusterId): Promise<void> {
return requestMain(clusterSetFrameIdHandler, clusterId);
}
export function requestClusterActivation(clusterId: ClusterId, force?: boolean): Promise<void> {
return requestMain(clusterActivateHandler, clusterId, force);
}