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

Implement sending message to channel in main

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2023-03-17 15:41:18 +02:00
parent 656e1cdb28
commit 08f427e476
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
3 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,10 @@
import { getInjectable } from "@ogre-tools/injectable";
import { webContents } from "electron";
const getWebContentsInjectable = getInjectable({
id: "web-contents",
instantiate: () => () => webContents.getAllWebContents(),
causesSideEffects: true,
});
export default getWebContentsInjectable;

View File

@ -0,0 +1,85 @@
import { registerFeature } from "@k8slens/feature-core";
import { createContainer, DiContainer } from "@ogre-tools/injectable";
import { messagingFeatureForMain } from "../feature";
import { getMessageChannel, sendMessageToChannelInjectionToken } from "@k8slens/messaging";
import getWebContentsInjectable from "./get-web-contents.injectable";
import type { WebContents } from "electron";
const someChannel = getMessageChannel<string>("some-channel");
describe("send-message-to-channel", () => {
let di: DiContainer;
beforeEach(() => {
di = createContainer("irrelevant");
registerFeature(di, messagingFeatureForMain);
});
it("given no web contents, when sending a message, does not do anything", () => {
di.override(getWebContentsInjectable, () => () => []);
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
expect(() => sendMessageToChannel(someChannel, "some-message")).not.toThrow();
});
it("given multiple web contents, when sending a message, sends message to all web contents", () => {
const sendToWebContentsMock = jest.fn();
di.override(getWebContentsInjectable, () => () => [
{
send: (...args: any[]) => sendToWebContentsMock("some-web-content", ...args),
isDestroyed: () => false,
isCrashed: () => false,
} as unknown as WebContents,
{
send: (...args: any[]) => sendToWebContentsMock("some-other-web-content", ...args),
isDestroyed: () => false,
isCrashed: () => false,
} as unknown as WebContents,
]);
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
sendMessageToChannel(someChannel, "some-message");
expect(sendToWebContentsMock.mock.calls).toEqual([
["some-web-content", "some-channel", "some-message"],
["some-other-web-content", "some-channel", "some-message"],
]);
});
it("given non alive web content, when sending a message, sends message to all web contents being alive", () => {
const sendToWebContentsMock = jest.fn();
di.override(getWebContentsInjectable, () => () => [
{
send: (...args: any[]) => sendToWebContentsMock("some-alive-content", ...args),
isDestroyed: () => false,
isCrashed: () => false,
} as unknown as WebContents,
{
send: (...args: any[]) => sendToWebContentsMock("destroyed-web-content", ...args),
isDestroyed: () => true,
isCrashed: () => false,
} as unknown as WebContents,
{
send: (...args: any[]) => sendToWebContentsMock("crashed-web-content", ...args),
isDestroyed: () => false,
isCrashed: () => true,
} as unknown as WebContents,
]);
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
sendMessageToChannel(someChannel, "some-message");
expect(sendToWebContentsMock.mock.calls).toEqual([
["some-alive-content", "some-channel", "some-message"],
]);
});
});

View File

@ -0,0 +1,38 @@
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 { reject } from "lodash/fp";
import type { WebContents } from "electron";
const isDestroyed = (webContent: WebContents) => webContent.isDestroyed();
const isCrashed = (webContent: WebContents) => webContent.isCrashed();
const forEach =
<T>(predicate: (item: T) => void) =>
(items: T[]) =>
items.forEach(predicate);
const sendMessageToChannelInjectable = getInjectable({
id: "send-message-to-channel",
instantiate: (di) => {
const getWebContents = di.inject(getWebContentsInjectable);
return ((channel, message) => {
pipeline(
getWebContents(),
reject(isDestroyed),
reject(isCrashed),
forEach((webContent) => {
webContent.send(channel.id, message);
}),
);
}) as SendMessageToChannel;
},
injectionToken: sendMessageToChannelInjectionToken,
});
export default sendMessageToChannelInjectable;