mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Prevent duplicate app windows (#5533)
Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Co-authored-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
1 parent
5c1f0daf50
commit
6451df1f17
19 files changed
+528
-109
No files matched your search
@@ -2,32 +2,24 @@
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { lensWindowInjectionToken } from "../../start-main-application/lens-window/application-window/lens-window-injection-token";
|
||||
import { pipeline } from "@ogre-tools/fp";
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { filter } from "lodash/fp";
|
||||
import { messageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token";
|
||||
import type { MessageChannel } from "../../../common/utils/channel/message-channel-injection-token";
|
||||
import { tentativeStringifyJson } from "../../../common/utils/tentative-stringify-json";
|
||||
import getVisibleWindowsInjectable from "../../start-main-application/lens-window/get-visible-windows.injectable";
|
||||
|
||||
const messageToChannelInjectable = getInjectable({
|
||||
id: "message-to-channel",
|
||||
|
||||
instantiate: (di) => {
|
||||
const getAllLensWindows = () => di.injectMany(lensWindowInjectionToken);
|
||||
const getVisibleWindows = di.inject(getVisibleWindowsInjectable);
|
||||
|
||||
// TODO: Figure out way to improve typing in internals
|
||||
// Notice that this should be injected using "messageToChannelInjectionToken" which is typed correctly.
|
||||
return (channel: MessageChannel<any>, message?: unknown) => {
|
||||
const stringifiedMessage = tentativeStringifyJson(message);
|
||||
|
||||
|
||||
const visibleWindows = pipeline(
|
||||
getAllLensWindows(),
|
||||
filter((lensWindow) => !!lensWindow.visible),
|
||||
);
|
||||
|
||||
visibleWindows.forEach((lensWindow) =>
|
||||
getVisibleWindows().forEach((lensWindow) =>
|
||||
lensWindow.send({ channel: channel.id, data: stringifiedMessage ? [stringifiedMessage] : [] }),
|
||||
);
|
||||
};
|
||||
|
||||
@@ -43,9 +43,9 @@ describe("message to channel from main", () => {
|
||||
expect(sendToChannelInBrowserMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe("given visible window", () => {
|
||||
describe("given started window", () => {
|
||||
beforeEach(async () => {
|
||||
await someTestWindow.show();
|
||||
await someTestWindow.start();
|
||||
});
|
||||
|
||||
it("when messaging to channel, messages to window", () => {
|
||||
@@ -109,9 +109,9 @@ describe("message to channel from main", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("given multiple visible windows, when messaging to channel, messages to window", async () => {
|
||||
await someTestWindow.show();
|
||||
await someOtherTestWindow.show();
|
||||
it("given multiple started windows, when messaging to channel, messages to window", async () => {
|
||||
await someTestWindow.start();
|
||||
await someOtherTestWindow.start();
|
||||
|
||||
messageToChannel(someChannel, "some-message");
|
||||
|
||||
|
||||
Reference in new issue
Block a user