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

chore: WIP

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>
Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-turso 2023-05-23 16:17:21 +03:00
parent 5f82cc50e4
commit e84269b15a
3 changed files with 79 additions and 27 deletions

View File

@ -1,19 +1,59 @@
import { getMessageChannel, getMessageChannelListenerInjectable } from "@k8slens/messaging";
import { sendMessageToChannelInjectionToken } from "@k8slens/messaging";
import { getMessageChannel } from "@k8slens/messaging";
import { getInjectable } from "@ogre-tools/injectable";
import { sendMessageToListenersInMainInjectable } from "../send-message-to-channel/send-message-to-listeners-in-main.injectable";
import { beforeApplicationIsLoadingInjectionToken } from "@k8slens/application/src/start-application/time-slots";
import { enlistMessageChannelListenerInjectionToken } from "@k8slens/messaging/src/features/actual";
import { getStartableStoppable } from "@k8slens/startable-stoppable/src/get-startable-stoppable";
type BroadcasterChannelMessage = { targetChannelId: string; message: unknown };
const broadcasterChannel = getMessageChannel<BroadcasterChannelMessage>("messaging-broadcaster-in-main");
export const messageBroadcasterListenerInjectable = getMessageChannelListenerInjectable({
id: "message-broadcaster-listener",
channel: broadcasterChannel,
export const messageBrokerInjectable = getInjectable({
id: "message-broker",
getHandler: (di) => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
instantiate: (di) => {
const enlistMessageChannelListener = di.inject(enlistMessageChannelListenerInjectionToken);
const sendToListenersInMain = di.inject(sendMessageToListenersInMainInjectable);
return ({ targetChannelId, message }, extraData) => {
sendMessageToChannel({ id: targetChannelId }, message, extraData);
};
return getStartableStoppable("message-broker", () =>
enlistMessageChannelListener({
id: "message-broker",
channel: broadcasterChannel,
handler: ({ targetChannelId, message }, electronMessageMetadata) => {
sendToListenersInMain({ id: targetChannelId }, message, electronMessageMetadata);
},
}),
);
},
});
export const startMessageBrokerInjectable = getInjectable({
id: "start-message-broker",
instantiate: (di) => {
const messageBroker = di.inject(messageBrokerInjectable);
return {
run: messageBroker.start,
};
},
injectionToken: beforeApplicationIsLoadingInjectionToken,
});
export const stopMessageBrokerInjectable = getInjectable({
id: "stop-message-broker",
instantiate: (di) => {
const messageBroker = di.inject(messageBrokerInjectable);
return {
run: messageBroker.stop,
};
},
// Todo: extract token and start using.
// injectionToken: onQuitOfBackEndInjectionToken,
});

View File

@ -2,10 +2,10 @@ import { getInjectable } from "@ogre-tools/injectable";
import { pipeline } from "@ogre-tools/fp";
import { SendMessageToChannel, sendMessageToChannelInjectionToken } from "@k8slens/messaging";
import getWebContentsInjectable from "./get-web-contents.injectable";
import { filter, flatMap, reject } from "lodash/fp";
import { flatMap, reject } from "lodash/fp";
import type { WebContents } from "electron";
import frameIdsInjectable from "./frameIds.injectable";
import { messageChannelListenerInjectionToken } from "@k8slens/messaging";
import { sendMessageToListenersInMainInjectable } from "./send-message-to-listeners-in-main.injectable";
const isDestroyed = (webContent: WebContents) => webContent.isDestroyed();
const isCrashed = (webContent: WebContents) => webContent.isCrashed();
@ -21,21 +21,9 @@ const sendMessageToChannelInjectable = getInjectable({
instantiate: (di) => {
const getWebContents = di.inject(getWebContentsInjectable);
const frameIds = di.inject(frameIdsInjectable);
const getMessageChannelListeners = () => di.injectMany(messageChannelListenerInjectionToken);
return ((channel, message, ...asd) => {
const messageChannelListeners = getMessageChannelListeners();
const sendToListenersInMain = () => {
pipeline(
messageChannelListeners,
filter((listener) => listener.channel.id === channel.id),
forEach(({ channel, handler }) => {
handler(message, ...asd);
}),
);
};
const sendToListenersInMain = di.inject(sendMessageToListenersInMainInjectable);
return ((channel, message) => {
const sendToListenersInRenderers = () => {
pipeline(
getWebContents(),
@ -56,7 +44,7 @@ const sendMessageToChannelInjectable = getInjectable({
);
};
sendToListenersInMain();
sendToListenersInMain(channel, message);
sendToListenersInRenderers();
}) as SendMessageToChannel;
},

View File

@ -0,0 +1,24 @@
import { filter, forEach } from "lodash/fp";
import { pipeline } from "@ogre-tools/fp";
import { getInjectable } from "@ogre-tools/injectable";
import { messageChannelListenerInjectionToken, MessageChannel } from "@k8slens/messaging";
export type ElectronMessageMetadata = { frameId: number; processId: number };
export const sendMessageToListenersInMainInjectable = getInjectable({
id: "send-message-to-listeners-in-main",
instantiate: (di) => {
const getMessageChannelListeners = () => di.injectMany(messageChannelListenerInjectionToken);
return <T>(channel: MessageChannel<T>, message: T, electronMessageMetadata?: ElectronMessageMetadata) => {
pipeline(
getMessageChannelListeners(),
filter((listener) => listener.channel.id === channel.id),
forEach(({ handler }) => {
handler(message, electronMessageMetadata);
}),
);
};
},
});