mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Switch to using messaging and startable stoppable from NPM package (#7368)
* Add custom jest resolver to fix requiring "uuid" module Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Update dependencies Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Introduce test utils for rendering and running with thrown mobx reactions Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Extract startable-stoppable to NPM package Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Extract messaging to NPM package Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Switch to using startable-stoppable from NPM package Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Switch to using messaging from the Feature Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Remove old implementation of messaging Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Make setupping app paths happen earlier in renderer Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Fix typo Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Add kludge to make testing-library work properly from test-utils package Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Fix code style Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Add lint:fix -root script Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Fix unrelated failing unit tests Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Turn of no-floating-promises from typescript linting for being broken Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Make linting not happen for dist -directories Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Make linting failures appear as failure Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Stop running prettier twice It already gets ran as eslint-plugin. Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Make CI run unit tests for all packages by consolidating name of NPM script Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Add missing unit tests for coverage Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Skip coverage for test utils Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Remove check for coverage in packages which are not ready for it Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Stop collecting coverage from index.ts files them being indirections to the implementation Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Implement sending message to channel in main Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Add missing feature dependencies Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Add dummy implementations for requesting in main from renderer Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Re-enable communicating from main to cluster frames Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Ignore trivial files from coverage Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Update package-lock Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Extract message-bridge to separate NPM package to prevent dev dependencies being in the production bundle Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Extract computed channel to own NPM package for clear dependencies Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Consolidate electron related stuff to a directory Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Add missing publish configurations Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> * Ignore test implementation from coverage being not interesting Signed-off-by: Janne Savolainen <janne.savolainen@live.fi> --------- Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
1 parent
df49ff9b96
commit
0f1f030a06
261 files changed
+4447
-1795
No files matched your search
+28
@@ -0,0 +1,28 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { IpcMainEvent } from "electron";
|
||||
import ipcMainInjectable from "../ipc-main/ipc-main.injectable";
|
||||
import { enlistMessageChannelListenerInjectionToken } from "@k8slens/messaging";
|
||||
|
||||
const enlistMessageChannelListenerInjectable = getInjectable({
|
||||
id: "enlist-message-channel-listener",
|
||||
|
||||
instantiate: (di) => {
|
||||
const ipcMain = di.inject(ipcMainInjectable);
|
||||
|
||||
return ({ channel, handler }) => {
|
||||
const nativeOnCallback = (nativeEvent: IpcMainEvent, message: unknown) => {
|
||||
handler(message, { frameId: nativeEvent.frameId, processId: nativeEvent.processId });
|
||||
};
|
||||
|
||||
ipcMain.on(channel.id, nativeOnCallback);
|
||||
|
||||
return () => {
|
||||
ipcMain.off(channel.id, nativeOnCallback);
|
||||
};
|
||||
};
|
||||
},
|
||||
|
||||
injectionToken: enlistMessageChannelListenerInjectionToken,
|
||||
});
|
||||
|
||||
export default enlistMessageChannelListenerInjectable;
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
import ipcMainInjectable from "../ipc-main/ipc-main.injectable";
|
||||
import type { IpcMain, IpcMainEvent } from "electron";
|
||||
import {
|
||||
EnlistMessageChannelListener,
|
||||
enlistMessageChannelListenerInjectionToken,
|
||||
} from "@k8slens/messaging";
|
||||
import { createContainer } from "@ogre-tools/injectable";
|
||||
import { registerFeature } from "@k8slens/feature-core";
|
||||
import { messagingFeatureForMain } from "../feature";
|
||||
|
||||
describe("enlist message channel listener in main", () => {
|
||||
let enlistMessageChannelListener: EnlistMessageChannelListener;
|
||||
let ipcMainStub: IpcMain;
|
||||
let onMock: jest.Mock;
|
||||
let offMock: jest.Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
const di = createContainer("irrelevant");
|
||||
|
||||
registerFeature(di, messagingFeatureForMain);
|
||||
|
||||
onMock = jest.fn();
|
||||
offMock = jest.fn();
|
||||
|
||||
ipcMainStub = {
|
||||
on: onMock,
|
||||
off: offMock,
|
||||
} as unknown as IpcMain;
|
||||
|
||||
di.override(ipcMainInjectable, () => ipcMainStub);
|
||||
|
||||
enlistMessageChannelListener = di.inject(enlistMessageChannelListenerInjectionToken);
|
||||
});
|
||||
|
||||
describe("when called", () => {
|
||||
let handlerMock: jest.Mock;
|
||||
let disposer: () => void;
|
||||
|
||||
beforeEach(() => {
|
||||
handlerMock = jest.fn();
|
||||
|
||||
disposer = enlistMessageChannelListener({
|
||||
id: "some-listener",
|
||||
channel: { id: "some-channel-id" },
|
||||
handler: handlerMock,
|
||||
});
|
||||
});
|
||||
|
||||
it("does not call handler yet", () => {
|
||||
expect(handlerMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("registers the listener", () => {
|
||||
expect(onMock).toHaveBeenCalledWith("some-channel-id", expect.any(Function));
|
||||
});
|
||||
|
||||
it("does not de-register the listener yet", () => {
|
||||
expect(offMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe("when message arrives", () => {
|
||||
beforeEach(() => {
|
||||
onMock.mock.calls[0][1]({ frameId: 42, processId: 84 } as IpcMainEvent, "some-message");
|
||||
});
|
||||
|
||||
it("calls the handler with the message", () => {
|
||||
expect(handlerMock).toHaveBeenCalledWith("some-message", { frameId: 42, processId: 84 });
|
||||
});
|
||||
|
||||
it("when disposing the listener, de-registers the listener", () => {
|
||||
disposer();
|
||||
|
||||
expect(offMock).toHaveBeenCalledWith("some-channel-id", expect.any(Function));
|
||||
});
|
||||
});
|
||||
|
||||
it("given number as message, when message arrives, calls the handler with the message", () => {
|
||||
onMock.mock.calls[0][1]({ frameId: 42, processId: 84 } as IpcMainEvent, 42);
|
||||
|
||||
expect(handlerMock).toHaveBeenCalledWith(42, { frameId: 42, processId: 84 });
|
||||
});
|
||||
|
||||
it("given boolean as message, when message arrives, calls the handler with the message", () => {
|
||||
onMock.mock.calls[0][1]({ frameId: 42, processId: 84 } as IpcMainEvent, true);
|
||||
|
||||
expect(handlerMock).toHaveBeenCalledWith(true, { frameId: 42, processId: 84 });
|
||||
});
|
||||
|
||||
it("given object as message, when message arrives, calls the handler with the message", () => {
|
||||
onMock.mock.calls[0][1]({ frameId: 42, processId: 84 } as IpcMainEvent, { some: "object" });
|
||||
|
||||
expect(handlerMock).toHaveBeenCalledWith({ some: "object" }, { frameId: 42, processId: 84 });
|
||||
});
|
||||
});
|
||||
});
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { IpcMainInvokeEvent } from "electron";
|
||||
import ipcMainInjectable from "../ipc-main/ipc-main.injectable";
|
||||
import type { RequestChannel, RequestChannelListener } from "@k8slens/messaging";
|
||||
import { enlistRequestChannelListenerInjectionToken } from "@k8slens/messaging";
|
||||
|
||||
export type EnlistRequestChannelListener = <TChannel extends RequestChannel<unknown, unknown>>(
|
||||
listener: RequestChannelListener<TChannel>,
|
||||
) => () => void;
|
||||
|
||||
const enlistRequestChannelListenerInjectable = getInjectable({
|
||||
id: "enlist-request-channel-listener-for-main",
|
||||
|
||||
instantiate: (di): EnlistRequestChannelListener => {
|
||||
const ipcMain = di.inject(ipcMainInjectable);
|
||||
|
||||
return ({ channel, handler }) => {
|
||||
const nativeHandleCallback = (_: IpcMainInvokeEvent, request: unknown) => handler(request);
|
||||
|
||||
ipcMain.handle(channel.id, nativeHandleCallback);
|
||||
|
||||
return () => {
|
||||
ipcMain.off(channel.id, nativeHandleCallback);
|
||||
};
|
||||
};
|
||||
},
|
||||
|
||||
injectionToken: enlistRequestChannelListenerInjectionToken,
|
||||
});
|
||||
|
||||
export default enlistRequestChannelListenerInjectable;
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
import ipcMainInjectable from "../ipc-main/ipc-main.injectable";
|
||||
import type { IpcMain, IpcMainInvokeEvent } from "electron";
|
||||
import type { AsyncFnMock } from "@async-fn/jest";
|
||||
import asyncFn from "@async-fn/jest";
|
||||
import type { EnlistRequestChannelListener } from "./enlist-request-channel-listener.injectable";
|
||||
import enlistRequestChannelListenerInjectable from "./enlist-request-channel-listener.injectable";
|
||||
import type { RequestChannel, RequestChannelHandler } from "@k8slens/messaging";
|
||||
import { getPromiseStatus } from "@k8slens/test-utils";
|
||||
import { createContainer } from "@ogre-tools/injectable";
|
||||
import { registerFeature } from "@k8slens/feature-core";
|
||||
import { messagingFeatureForMain } from "../feature";
|
||||
|
||||
type TestRequestChannel = RequestChannel<unknown, unknown>;
|
||||
|
||||
const testRequestChannel: TestRequestChannel = {
|
||||
id: "some-channel-id",
|
||||
};
|
||||
|
||||
describe("enlist request channel listener in main", () => {
|
||||
let enlistRequestChannelListener: EnlistRequestChannelListener;
|
||||
let ipcMainStub: IpcMain;
|
||||
let handleMock: jest.Mock;
|
||||
let offMock: jest.Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
const di = createContainer("irrelevant");
|
||||
|
||||
registerFeature(di, messagingFeatureForMain);
|
||||
|
||||
handleMock = jest.fn();
|
||||
offMock = jest.fn();
|
||||
|
||||
ipcMainStub = {
|
||||
handle: handleMock,
|
||||
off: offMock,
|
||||
} as unknown as IpcMain;
|
||||
|
||||
di.override(ipcMainInjectable, () => ipcMainStub);
|
||||
|
||||
enlistRequestChannelListener = di.inject(enlistRequestChannelListenerInjectable);
|
||||
});
|
||||
|
||||
describe("when called", () => {
|
||||
let handlerMock: AsyncFnMock<RequestChannelHandler<TestRequestChannel>>;
|
||||
let disposer: () => void;
|
||||
|
||||
beforeEach(() => {
|
||||
handlerMock = asyncFn();
|
||||
|
||||
disposer = enlistRequestChannelListener({
|
||||
id: "some-listener",
|
||||
channel: testRequestChannel,
|
||||
handler: handlerMock,
|
||||
});
|
||||
});
|
||||
|
||||
it("does not call handler yet", () => {
|
||||
expect(handlerMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("registers the listener", () => {
|
||||
expect(handleMock).toHaveBeenCalledWith("some-channel-id", expect.any(Function));
|
||||
});
|
||||
|
||||
it("does not de-register the listener yet", () => {
|
||||
expect(offMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe("when request arrives", () => {
|
||||
let actualPromise: Promise<any>;
|
||||
|
||||
beforeEach(() => {
|
||||
actualPromise = handleMock.mock.calls[0][1]({} as IpcMainInvokeEvent, "some-request");
|
||||
});
|
||||
|
||||
it("calls the handler with the request", () => {
|
||||
expect(handlerMock).toHaveBeenCalledWith("some-request");
|
||||
});
|
||||
|
||||
it("does not resolve yet", async () => {
|
||||
const promiseStatus = await getPromiseStatus(actualPromise);
|
||||
|
||||
expect(promiseStatus.fulfilled).toBe(false);
|
||||
});
|
||||
|
||||
describe("when handler resolves with response, listener resolves with the response", () => {
|
||||
beforeEach(async () => {
|
||||
await handlerMock.resolve("some-response");
|
||||
});
|
||||
|
||||
it("resolves with the response", async () => {
|
||||
const actual = await actualPromise;
|
||||
|
||||
expect(actual).toBe("some-response");
|
||||
});
|
||||
|
||||
it("when disposing the listener, de-registers the listener", () => {
|
||||
disposer();
|
||||
|
||||
expect(offMock).toHaveBeenCalledWith("some-channel-id", expect.any(Function));
|
||||
});
|
||||
});
|
||||
|
||||
it("given number as response, when handler resolves with response, listener resolves with stringified response", async () => {
|
||||
await handlerMock.resolve(42);
|
||||
|
||||
const actual = await actualPromise;
|
||||
|
||||
expect(actual).toBe(42);
|
||||
});
|
||||
|
||||
it("given boolean as response, when handler resolves with response, listener resolves with stringified response", async () => {
|
||||
await handlerMock.resolve(true);
|
||||
|
||||
const actual = await actualPromise;
|
||||
|
||||
expect(actual).toBe(true);
|
||||
});
|
||||
|
||||
it("given object as response, when handler resolves with response, listener resolves with response", async () => {
|
||||
await handlerMock.resolve({ some: "object" });
|
||||
|
||||
const actual = await actualPromise;
|
||||
|
||||
expect(actual).toEqual({ some: "object" });
|
||||
});
|
||||
});
|
||||
|
||||
it("given number as request, when request arrives, calls the handler with the request", () => {
|
||||
handleMock.mock.calls[0][1]({} as IpcMainInvokeEvent, 42);
|
||||
|
||||
expect(handlerMock).toHaveBeenCalledWith(42);
|
||||
});
|
||||
|
||||
it("given boolean as request, when request arrives, calls the handler with the request", () => {
|
||||
handleMock.mock.calls[0][1]({} as IpcMainInvokeEvent, true);
|
||||
|
||||
expect(handlerMock).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it("given object as request, when request arrives, calls the handler with the request", () => {
|
||||
handleMock.mock.calls[0][1]({} as IpcMainInvokeEvent, { some: "object" });
|
||||
|
||||
expect(handlerMock).toHaveBeenCalledWith({ some: "object" });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import { autoRegister } from "@ogre-tools/injectable-extension-for-auto-registration";
|
||||
import { getFeature } from "@k8slens/feature-core";
|
||||
import { messagingFeature } from "@k8slens/messaging";
|
||||
|
||||
export const messagingFeatureForMain = getFeature({
|
||||
id: "messaging-for-main",
|
||||
|
||||
register: (di) => {
|
||||
autoRegister({
|
||||
di,
|
||||
targetModule: module,
|
||||
|
||||
getRequireContexts: () => [require.context("./", true, /\.injectable\.(ts|tsx)$/)],
|
||||
});
|
||||
},
|
||||
|
||||
dependencies: [messagingFeature],
|
||||
});
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { ipcMain } from "electron";
|
||||
|
||||
const ipcMainInjectable = getInjectable({
|
||||
id: "ipc-main",
|
||||
instantiate: () => ipcMain,
|
||||
causesSideEffects: true,
|
||||
});
|
||||
|
||||
export default ipcMainInjectable;
|
||||
@@ -0,0 +1,21 @@
|
||||
import { createContainer, DiContainer } from "@ogre-tools/injectable";
|
||||
import { registerFeature } from "@k8slens/feature-core";
|
||||
import ipcMainInjectable from "./ipc-main.injectable";
|
||||
import { ipcMain } from "electron";
|
||||
import { messagingFeatureForMain } from "../feature";
|
||||
|
||||
describe("ipc-main", () => {
|
||||
let di: DiContainer;
|
||||
|
||||
beforeEach(() => {
|
||||
di = createContainer("irrelevant");
|
||||
|
||||
registerFeature(di, messagingFeatureForMain);
|
||||
});
|
||||
|
||||
it("is the actual IPC-main of Electron", () => {
|
||||
const actual = di.inject(ipcMainInjectable);
|
||||
|
||||
expect(actual).toBe(ipcMain);
|
||||
});
|
||||
});
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
/* c8 ignore start */
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import {
|
||||
RequestChannel,
|
||||
RequestFromChannel,
|
||||
requestFromChannelInjectionToken,
|
||||
} from "@k8slens/messaging";
|
||||
|
||||
const requestFromChannelInjectable = getInjectable({
|
||||
id: "request-from-channel",
|
||||
|
||||
instantiate: () =>
|
||||
((channel: RequestChannel<any, any>) => {
|
||||
throw new Error(
|
||||
`Tried to request from channel "${channel.id}" but requesting in "main" it's not supported yet`,
|
||||
);
|
||||
}) as unknown as RequestFromChannel,
|
||||
|
||||
injectionToken: requestFromChannelInjectionToken,
|
||||
});
|
||||
|
||||
export default requestFromChannelInjectable;
|
||||
/* c8 ignore stop */
|
||||
+21
@@ -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;
|
||||
+8
@@ -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;
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
/* c8 ignore start */
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { webContents } from "electron";
|
||||
|
||||
const getWebContentsInjectable = getInjectable({
|
||||
id: "web-contents",
|
||||
instantiate: () => () => webContents.getAllWebContents(),
|
||||
causesSideEffects: true,
|
||||
});
|
||||
|
||||
export default getWebContentsInjectable;
|
||||
/* c8 ignore stop */
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
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";
|
||||
import allowCommunicationListenerInjectable from "./allow-communication-listener.injectable";
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
describe("given web content that is alive", () => {
|
||||
let sendToFrameMock: jest.Mock;
|
||||
let sendMessageMock: jest.Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
sendToFrameMock = jest.fn();
|
||||
sendMessageMock = jest.fn();
|
||||
|
||||
di.override(getWebContentsInjectable, () => () => [
|
||||
{
|
||||
send: (...args: any[]) => sendMessageMock("first", ...args),
|
||||
sendToFrame: (...args: any[]) => sendToFrameMock("first", ...args),
|
||||
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,
|
||||
]);
|
||||
});
|
||||
|
||||
it("when sending message, sends the message to webcontents", () => {
|
||||
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
|
||||
|
||||
sendMessageToChannel(someChannel, "some-message");
|
||||
|
||||
expect(sendMessageMock.mock.calls).toEqual([
|
||||
["first", "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 contents, when sending a message, does not send messages", () => {
|
||||
const sendToWebContentsMock = jest.fn();
|
||||
|
||||
di.override(getWebContentsInjectable, () => () => [
|
||||
{
|
||||
send: sendToWebContentsMock,
|
||||
isDestroyed: () => true,
|
||||
isCrashed: () => false,
|
||||
} as unknown as WebContents,
|
||||
|
||||
{
|
||||
send: sendToWebContentsMock,
|
||||
isDestroyed: () => false,
|
||||
isCrashed: () => true,
|
||||
} as unknown as WebContents,
|
||||
]);
|
||||
|
||||
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
|
||||
|
||||
sendMessageToChannel(someChannel, "irrelevant");
|
||||
|
||||
expect(sendToWebContentsMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
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 { flatMap, reject } from "lodash/fp";
|
||||
import type { WebContents } from "electron";
|
||||
import frameIdsInjectable from "./frameIds.injectable";
|
||||
|
||||
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);
|
||||
const frameIds = di.inject(frameIdsInjectable);
|
||||
|
||||
return ((channel, message) => {
|
||||
pipeline(
|
||||
getWebContents(),
|
||||
reject(isDestroyed),
|
||||
reject(isCrashed),
|
||||
|
||||
flatMap((webContent) => [
|
||||
(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;
|
||||
},
|
||||
|
||||
injectionToken: sendMessageToChannelInjectionToken,
|
||||
});
|
||||
|
||||
export default sendMessageToChannelInjectable;
|
||||
Reference in new issue
Block a user