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

Allow main to message to cluster frames (#6835)

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-12-28 08:18:58 +02:00 committed by GitHub
parent 44bc703606
commit 8156936b82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 0 deletions

View File

@ -6,16 +6,22 @@ import { getInjectable } from "@ogre-tools/injectable";
import type { SendMessageToChannel } from "../../../common/utils/channel/message-to-channel-injection-token";
import { sendMessageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token";
import getVisibleWindowsInjectable from "../../start-main-application/lens-window/get-visible-windows.injectable";
import clusterFramesInjectable from "../../../common/cluster-frames.injectable";
const messageToChannelInjectable = getInjectable({
id: "message-to-channel",
instantiate: (di) => {
const getVisibleWindows = di.inject(getVisibleWindowsInjectable);
const clusterFrames = di.inject(clusterFramesInjectable);
return ((channel, data) => {
for (const window of getVisibleWindows()) {
window.send({ channel: channel.id, data });
clusterFrames.forEach(frameInfo => {
window.send({ channel: channel.id, data, frameInfo });
});
}
}) as SendMessageToChannel;
},

View File

@ -0,0 +1,115 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getDiForUnitTesting } from "../../getDiForUnitTesting";
import getVisibleWindowsInjectable from "../../start-main-application/lens-window/get-visible-windows.injectable";
import clusterFramesInjectable from "../../../common/cluster-frames.injectable";
import type { MessageChannel } from "../../../common/utils/channel/message-channel-listener-injection-token";
import { sendMessageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token";
import type { DiContainer } from "@ogre-tools/injectable";
import type { ClusterFrameInfo } from "../../../common/cluster-frames";
describe("message-to-channel", () => {
let di: DiContainer;
let sendToWindowMock: jest.Mock;
beforeEach(() => {
di = getDiForUnitTesting({ doGeneralOverrides: true });
sendToWindowMock = jest.fn();
di.override(getVisibleWindowsInjectable, () => () => [
{
id: "some-window",
send: sendToWindowMock,
show: () => {},
reload: () => {},
isStarting: false,
start: async () => {},
close: () => {},
isVisible: true,
},
{
id: "some-other-window",
send: sendToWindowMock,
show: () => {},
reload: () => {},
isStarting: false,
start: async () => {},
close: () => {},
isVisible: true,
},
]);
di.override(
clusterFramesInjectable,
() =>
new Map<string, ClusterFrameInfo>([
[
"some-cluster-id",
{ frameId: 42, processId: 84 },
],
[
"some-other-cluster-id",
{ frameId: 126, processId: 168 },
],
]),
);
});
describe("when sending message", () => {
beforeEach(() => {
const sendMessageToChannel = di.inject(
sendMessageToChannelInjectionToken,
);
sendMessageToChannel(someChannel, 42);
});
it("sends to each window and cluster frames", () => {
expect(sendToWindowMock.mock.calls).toEqual([
[{ channel: "some-channel-id", data: 42 }],
[
{
channel: "some-channel-id",
data: 42,
frameInfo: { frameId: 42, processId: 84 },
},
],
[
{
channel: "some-channel-id",
data: 42,
frameInfo: { frameId: 126, processId: 168 },
},
],
[{ channel: "some-channel-id", data: 42 }],
[
{
channel: "some-channel-id",
data: 42,
frameInfo: { frameId: 42, processId: 84 },
},
],
[
{
channel: "some-channel-id",
data: 42,
frameInfo: { frameId: 126, processId: 168 },
},
],
]);
});
});
});
const someChannel: MessageChannel<number> = {
id: "some-channel-id",
};