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:
1 parent
1058113e90
commit
fe86b79adb
15 files changed
+176
-71
No files matched your search
+16
-23
@@ -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,
|
||||
|
||||
+5
-15
@@ -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);
|
||||
|
||||
+29
@@ -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;
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
+9
-3
@@ -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),
|
||||
);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user