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

Re-enable communicating from main to cluster frames

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2023-03-20 15:35:49 +02:00
parent 2a4fc4f20f
commit 42bd808e43
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
11 changed files with 211 additions and 54 deletions

View File

@ -6,8 +6,10 @@ export interface MessageChannel<Message> {
_messageSignature?: Message; _messageSignature?: Message;
} }
export type ExtraData = { processId: number; frameId: number };
export type MessageChannelHandler<Channel> = Channel extends MessageChannel<infer Message> export type MessageChannelHandler<Channel> = Channel extends MessageChannel<infer Message>
? (message: Message) => void ? (message: Message, data: ExtraData) => void
: never; : never;
export interface MessageChannelListener<Channel> { export interface MessageChannelListener<Channel> {

View File

@ -162,6 +162,10 @@ const someRequestChannelWithoutListeners: SomeRequestChannel = {
it("the response gets handled in di-1", () => { it("the response gets handled in di-1", () => {
expect(someHandler1MockInDi1).toHaveBeenCalledWith( expect(someHandler1MockInDi1).toHaveBeenCalledWith(
"some-response-to: some-message", "some-response-to: some-message",
{
frameId: 42,
processId: 42,
},
); );
}); });
@ -188,6 +192,10 @@ const someRequestChannelWithoutListeners: SomeRequestChannel = {
it("the response gets handled in di-1", () => { it("the response gets handled in di-1", () => {
expect(someHandler1MockInDi1).toHaveBeenCalledWith( expect(someHandler1MockInDi1).toHaveBeenCalledWith(
"some-response-to: some-message", "some-response-to: some-message",
{
frameId: 42,
processId: 42,
},
); );
}); });
}); });
@ -224,9 +232,15 @@ const someRequestChannelWithoutListeners: SomeRequestChannel = {
}); });
it("listeners in other than sending DIs handle the message", () => { it("listeners in other than sending DIs handle the message", () => {
expect(someHandler1MockInDi2).toHaveBeenCalledWith("some-message"); expect(someHandler1MockInDi2).toHaveBeenCalledWith("some-message", {
frameId: 42,
processId: 42,
});
expect(someHandler2MockInDi2).toHaveBeenCalledWith("some-message"); expect(someHandler2MockInDi2).toHaveBeenCalledWith("some-message", {
frameId: 42,
processId: 42,
});
}); });
}); });
@ -245,9 +259,15 @@ const someRequestChannelWithoutListeners: SomeRequestChannel = {
}); });
it("listeners still handle the message", () => { it("listeners still handle the message", () => {
expect(someHandler1MockInDi2).toHaveBeenCalledWith("some-message"); expect(someHandler1MockInDi2).toHaveBeenCalledWith("some-message", {
frameId: 42,
processId: 42,
});
expect(someHandler2MockInDi2).toHaveBeenCalledWith("some-message"); expect(someHandler2MockInDi2).toHaveBeenCalledWith("some-message", {
frameId: 42,
processId: 42,
});
}); });
}); });
}); });

View File

@ -54,12 +54,12 @@ const overrideMessaging = ({
const resolvableHandlePromise = asyncFn(); const resolvableHandlePromise = asyncFn();
resolvableHandlePromise().then(() => { resolvableHandlePromise().then(() => {
handlersForChannel.forEach((handler) => handler(message)); handlersForChannel.forEach((handler) => handler(message, { frameId: 42, processId: 42 }));
}); });
messagePropagationBuffer.add(resolvableHandlePromise); messagePropagationBuffer.add(resolvableHandlePromise);
} else { } else {
handlersForChannel.forEach((handler) => handler(message)); handlersForChannel.forEach((handler) => handler(message, { frameId: 42, processId: 42 }));
} }
}); });
}); });
@ -85,7 +85,7 @@ const overrideRequesting = ({
}: { }: {
di: DiContainer; di: DiContainer;
requestListenersByDi: Map<DiContainer, Map<string, Set<MessageChannelHandler<Channel>>>>; requestListenersByDi: Map<DiContainer, Map<string, Set<RequestChannelHandler<Channel>>>>;
}) => { }) => {
const requestHandlersByChannel = new Map<string, Set<RequestChannelHandler<Channel>>>(); const requestHandlersByChannel = new Map<string, Set<RequestChannelHandler<Channel>>>();

View File

@ -10,8 +10,8 @@ const enlistMessageChannelListenerInjectable = getInjectable({
const ipcMain = di.inject(ipcMainInjectable); const ipcMain = di.inject(ipcMainInjectable);
return ({ channel, handler }) => { return ({ channel, handler }) => {
const nativeOnCallback = (_: IpcMainEvent, message: unknown) => { const nativeOnCallback = (nativeEvent: IpcMainEvent, message: unknown) => {
handler(message); handler(message, { frameId: nativeEvent.frameId, processId: nativeEvent.processId });
}; };
ipcMain.on(channel.id, nativeOnCallback); ipcMain.on(channel.id, nativeOnCallback);

View File

@ -60,11 +60,11 @@ describe("enlist message channel listener in main", () => {
describe("when message arrives", () => { describe("when message arrives", () => {
beforeEach(() => { beforeEach(() => {
onMock.mock.calls[0][1]({} as IpcMainEvent, "some-message"); onMock.mock.calls[0][1]({ frameId: 42, processId: 84 } as IpcMainEvent, "some-message");
}); });
it("calls the handler with the message", () => { it("calls the handler with the message", () => {
expect(handlerMock).toHaveBeenCalledWith("some-message"); expect(handlerMock).toHaveBeenCalledWith("some-message", { frameId: 42, processId: 84 });
}); });
it("when disposing the listener, de-registers the listener", () => { it("when disposing the listener, de-registers the listener", () => {
@ -75,21 +75,21 @@ describe("enlist message channel listener in main", () => {
}); });
it("given number as message, when message arrives, calls the handler with the message", () => { it("given number as message, when message arrives, calls the handler with the message", () => {
onMock.mock.calls[0][1]({} as IpcMainEvent, 42); onMock.mock.calls[0][1]({ frameId: 42, processId: 84 } as IpcMainEvent, 42);
expect(handlerMock).toHaveBeenCalledWith(42); expect(handlerMock).toHaveBeenCalledWith(42, { frameId: 42, processId: 84 });
}); });
it("given boolean as message, when message arrives, calls the handler with the message", () => { it("given boolean as message, when message arrives, calls the handler with the message", () => {
onMock.mock.calls[0][1]({} as IpcMainEvent, true); onMock.mock.calls[0][1]({ frameId: 42, processId: 84 } as IpcMainEvent, true);
expect(handlerMock).toHaveBeenCalledWith(true); expect(handlerMock).toHaveBeenCalledWith(true, { frameId: 42, processId: 84 });
}); });
it("given object as message, when message arrives, calls the handler with the message", () => { it("given object as message, when message arrives, calls the handler with the message", () => {
onMock.mock.calls[0][1]({} as IpcMainEvent, { some: "object" }); onMock.mock.calls[0][1]({ frameId: 42, processId: 84 } as IpcMainEvent, { some: "object" });
expect(handlerMock).toHaveBeenCalledWith({ some: "object" }); expect(handlerMock).toHaveBeenCalledWith({ some: "object" }, { frameId: 42, processId: 84 });
}); });
}); });
}); });

View File

@ -0,0 +1,21 @@
import { getMessageChannel, getMessageChannelListenerInjectable } from "@k8slens/messaging";
import frameIdsInjectable from "./frameIds.injectable";
const frameCommunicationAdminChannel = getMessageChannel<undefined>(
"frame-communication-admin-channel",
);
const allowCommunicationListenerInjectable = getMessageChannelListenerInjectable({
id: "allow-communication",
channel: frameCommunicationAdminChannel,
getHandler: (di) => {
const frameIds = di.inject(frameIdsInjectable);
return (_, { frameId, processId }) => {
frameIds.add({ frameId, processId });
};
},
});
export default allowCommunicationListenerInjectable;

View File

@ -0,0 +1,8 @@
import { getInjectable } from "@ogre-tools/injectable";
const frameIdsInjectable = getInjectable({
id: "frame-ids",
instantiate: () => new Set<{ frameId: number; processId: number }>(),
});
export default frameIdsInjectable;

View File

@ -4,6 +4,7 @@ import { messagingFeatureForMain } from "../feature";
import { getMessageChannel, sendMessageToChannelInjectionToken } from "@k8slens/messaging"; import { getMessageChannel, sendMessageToChannelInjectionToken } from "@k8slens/messaging";
import getWebContentsInjectable from "./get-web-contents.injectable"; import getWebContentsInjectable from "./get-web-contents.injectable";
import type { WebContents } from "electron"; import type { WebContents } from "electron";
import allowCommunicationListenerInjectable from "./allow-communication-listener.injectable";
const someChannel = getMessageChannel<string>("some-channel"); const someChannel = getMessageChannel<string>("some-channel");
@ -24,51 +25,88 @@ describe("send-message-to-channel", () => {
expect(() => sendMessageToChannel(someChannel, "some-message")).not.toThrow(); expect(() => sendMessageToChannel(someChannel, "some-message")).not.toThrow();
}); });
it("given multiple web contents, when sending a message, sends message to all web contents", () => { describe("given web content that is alive", () => {
const sendToWebContentsMock = jest.fn(); let sendToFrameMock: jest.Mock;
let sendMessageMock: jest.Mock;
di.override(getWebContentsInjectable, () => () => [ beforeEach(() => {
{ sendToFrameMock = jest.fn();
send: (...args: any[]) => sendToWebContentsMock("some-web-content", ...args), sendMessageMock = jest.fn();
isDestroyed: () => false,
isCrashed: () => false,
} as unknown as WebContents,
{ di.override(getWebContentsInjectable, () => () => [
send: (...args: any[]) => sendToWebContentsMock("some-other-web-content", ...args), {
isDestroyed: () => false, send: (...args: any[]) => sendMessageMock("first", ...args),
isCrashed: () => false, sendToFrame: (...args: any[]) => sendToFrameMock("first", ...args),
} as unknown as WebContents, isDestroyed: () => false,
]); isCrashed: () => false,
} as unknown as WebContents,
{
send: (...args: any[]) => sendMessageMock("second", ...args),
sendToFrame: (...args: any[]) => sendToFrameMock("second", ...args),
isDestroyed: () => false,
isCrashed: () => false,
} as unknown as WebContents,
]);
});
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken); it("when sending message, sends the message to webcontents", () => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
sendMessageToChannel(someChannel, "some-message"); sendMessageToChannel(someChannel, "some-message");
expect(sendToWebContentsMock.mock.calls).toEqual([ expect(sendMessageMock.mock.calls).toEqual([
["some-web-content", "some-channel", "some-message"], ["first", "some-channel", "some-message"],
["some-other-web-content", "some-channel", "some-message"], ["second", "some-channel", "some-message"],
]); ]);
});
describe("when multiple renderers inform that they are ready to listen messages", () => {
beforeEach(() => {
const allowCommunicationListener = di.inject(allowCommunicationListenerInjectable);
allowCommunicationListener.handler(undefined, { frameId: 42, processId: 126 });
allowCommunicationListener.handler(undefined, { frameId: 84, processId: 168 });
});
describe("when sending a message", () => {
beforeEach(() => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
sendMessageToChannel(someChannel, "some-message");
});
it("sends the message to webcontents", () => {
expect(sendMessageMock.mock.calls).toEqual([
["first", "some-channel", "some-message"],
["second", "some-channel", "some-message"],
]);
});
it("sends the message to individual frames in webcontents", () => {
expect(sendToFrameMock.mock.calls).toEqual([
["first", [42, 126], "some-channel", "some-message"],
["first", [84, 168], "some-channel", "some-message"],
["second", [42, 126], "some-channel", "some-message"],
["second", [84, 168], "some-channel", "some-message"],
]);
});
});
});
}); });
it("given non alive web content, when sending a message, sends message to all web contents being alive", () => { it("given non alive web contents, when sending a message, does not send messages", () => {
const sendToWebContentsMock = jest.fn(); const sendToWebContentsMock = jest.fn();
di.override(getWebContentsInjectable, () => () => [ di.override(getWebContentsInjectable, () => () => [
{ {
send: (...args: any[]) => sendToWebContentsMock("some-alive-content", ...args), send: sendToWebContentsMock,
isDestroyed: () => false,
isCrashed: () => false,
} as unknown as WebContents,
{
send: (...args: any[]) => sendToWebContentsMock("destroyed-web-content", ...args),
isDestroyed: () => true, isDestroyed: () => true,
isCrashed: () => false, isCrashed: () => false,
} as unknown as WebContents, } as unknown as WebContents,
{ {
send: (...args: any[]) => sendToWebContentsMock("crashed-web-content", ...args), send: sendToWebContentsMock,
isDestroyed: () => false, isDestroyed: () => false,
isCrashed: () => true, isCrashed: () => true,
} as unknown as WebContents, } as unknown as WebContents,
@ -76,10 +114,8 @@ describe("send-message-to-channel", () => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken); const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
sendMessageToChannel(someChannel, "some-message"); sendMessageToChannel(someChannel, "irrelevant");
expect(sendToWebContentsMock.mock.calls).toEqual([ expect(sendToWebContentsMock).not.toHaveBeenCalled();
["some-alive-content", "some-channel", "some-message"],
]);
}); });
}); });

View File

@ -2,8 +2,9 @@ import { getInjectable } from "@ogre-tools/injectable";
import { pipeline } from "@ogre-tools/fp"; import { pipeline } from "@ogre-tools/fp";
import { SendMessageToChannel, sendMessageToChannelInjectionToken } from "@k8slens/messaging"; import { SendMessageToChannel, sendMessageToChannelInjectionToken } from "@k8slens/messaging";
import getWebContentsInjectable from "./get-web-contents.injectable"; import getWebContentsInjectable from "./get-web-contents.injectable";
import { reject } from "lodash/fp"; import { flatMap, reject } from "lodash/fp";
import type { WebContents } from "electron"; import type { WebContents } from "electron";
import frameIdsInjectable from "./frameIds.injectable";
const isDestroyed = (webContent: WebContents) => webContent.isDestroyed(); const isDestroyed = (webContent: WebContents) => webContent.isDestroyed();
const isCrashed = (webContent: WebContents) => webContent.isCrashed(); const isCrashed = (webContent: WebContents) => webContent.isCrashed();
@ -18,6 +19,7 @@ const sendMessageToChannelInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const getWebContents = di.inject(getWebContentsInjectable); const getWebContents = di.inject(getWebContentsInjectable);
const frameIds = di.inject(frameIdsInjectable);
return ((channel, message) => { return ((channel, message) => {
pipeline( pipeline(
@ -25,8 +27,16 @@ const sendMessageToChannelInjectable = getInjectable({
reject(isDestroyed), reject(isDestroyed),
reject(isCrashed), reject(isCrashed),
forEach((webContent) => { flatMap((webContent) => [
webContent.send(channel.id, message); (channelId: string, ...args: any[]) => webContent.send(channelId, ...args),
...[...frameIds].map(({ frameId, processId }) => (channelId: string, ...args: any[]) => {
webContent.sendToFrame([frameId, processId], channelId, ...args);
}),
]),
forEach((send) => {
send(channel.id, message);
}), }),
); );
}) as SendMessageToChannel; }) as SendMessageToChannel;

View File

@ -0,0 +1,25 @@
import { getInjectable } from "@ogre-tools/injectable";
import { onLoadOfApplicationInjectionToken } from "@k8slens/application";
import { getMessageChannel, sendMessageToChannelInjectionToken } from "@k8slens/messaging";
export const frameCommunicationAdminChannel = getMessageChannel<undefined>(
"frame-communication-admin-channel",
);
const allowCommunicationToIframeInjectable = getInjectable({
id: "allow-communication-to-iframe-injectable",
instantiate: (di) => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
return {
run: () => {
sendMessageToChannel(frameCommunicationAdminChannel);
},
};
},
injectionToken: onLoadOfApplicationInjectionToken,
});
export default allowCommunicationToIframeInjectable;

View File

@ -0,0 +1,35 @@
import { createContainer, DiContainer } from "@ogre-tools/injectable";
import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx";
import { startApplicationInjectionToken } from "@k8slens/application";
import { registerFeature } from "@k8slens/feature-core";
import { messagingFeatureForRenderer } from "./feature";
import { runInAction } from "mobx";
import ipcRendererInjectable from "./ipc/ipc-renderer.injectable";
import { sendMessageToChannelInjectionToken } from "@k8slens/messaging";
import { frameCommunicationAdminChannel } from "./allow-communication-to-iframe.injectable";
describe("allow communication to iframe", () => {
let di: DiContainer;
let sendMessageToChannelMock: jest.Mock;
beforeEach(() => {
di = createContainer("irrelevant");
registerMobX(di);
runInAction(() => {
registerFeature(di, messagingFeatureForRenderer);
});
di.override(ipcRendererInjectable, () => ({ on: () => {} } as unknown));
sendMessageToChannelMock = jest.fn();
di.override(sendMessageToChannelInjectionToken, () => sendMessageToChannelMock);
});
it("when application starts, sends message to communication channel to register the frame ID and process ID for further usage", async () => {
await di.inject(startApplicationInjectionToken)();
expect(sendMessageToChannelMock).toHaveBeenCalledWith(frameCommunicationAdminChannel);
});
});