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
+25
@@ -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;
|
||||
+35
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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 messagingFeatureForRenderer = getFeature({
|
||||
id: "messaging-for-renderer",
|
||||
|
||||
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 { ipcRenderer } from "electron";
|
||||
|
||||
const ipcRendererInjectable = getInjectable({
|
||||
id: "ipc-renderer",
|
||||
instantiate: () => ipcRenderer,
|
||||
causesSideEffects: true,
|
||||
});
|
||||
|
||||
export default ipcRendererInjectable;
|
||||
@@ -0,0 +1,27 @@
|
||||
import { createContainer, DiContainer } from "@ogre-tools/injectable";
|
||||
import { registerFeature } from "@k8slens/feature-core";
|
||||
import ipcRendererInjectable from "./ipc-renderer.injectable";
|
||||
import { messagingFeatureForRenderer } from "../feature";
|
||||
import { ipcRenderer } from "electron";
|
||||
|
||||
describe("ipc-renderer", () => {
|
||||
let di: DiContainer;
|
||||
|
||||
beforeEach(() => {
|
||||
di = createContainer("irrelevant");
|
||||
|
||||
registerFeature(di, messagingFeatureForRenderer);
|
||||
});
|
||||
|
||||
it("is not undefined", () => {
|
||||
const actual = di.inject(ipcRendererInjectable);
|
||||
|
||||
expect(actual).not.toBeUndefined();
|
||||
});
|
||||
|
||||
it("is IPC-renderer of Electron", () => {
|
||||
const actual = di.inject(ipcRendererInjectable);
|
||||
|
||||
expect(actual).toBe(ipcRenderer);
|
||||
});
|
||||
});
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
import ipcRendererInjectable from "../ipc/ipc-renderer.injectable";
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { IpcRendererEvent } from "electron";
|
||||
import { enlistMessageChannelListenerInjectionToken } from "@k8slens/messaging";
|
||||
|
||||
const enlistMessageChannelListenerInjectable = getInjectable({
|
||||
id: "enlist-message-channel-listener-for-renderer",
|
||||
|
||||
instantiate: (di) => {
|
||||
const ipcRenderer = di.inject(ipcRendererInjectable);
|
||||
|
||||
return ({ channel, handler }) => {
|
||||
const nativeCallback = (_: IpcRendererEvent, message: unknown) => {
|
||||
handler(message);
|
||||
};
|
||||
|
||||
ipcRenderer.on(channel.id, nativeCallback);
|
||||
|
||||
return () => {
|
||||
ipcRenderer.off(channel.id, nativeCallback);
|
||||
};
|
||||
};
|
||||
},
|
||||
|
||||
injectionToken: enlistMessageChannelListenerInjectionToken,
|
||||
});
|
||||
|
||||
export default enlistMessageChannelListenerInjectable;
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
import type { IpcRendererEvent, IpcRenderer } from "electron";
|
||||
import ipcRendererInjectable from "../ipc/ipc-renderer.injectable";
|
||||
import {
|
||||
EnlistMessageChannelListener,
|
||||
enlistMessageChannelListenerInjectionToken,
|
||||
} from "@k8slens/messaging";
|
||||
import { createContainer } from "@ogre-tools/injectable";
|
||||
import { registerFeature } from "@k8slens/feature-core";
|
||||
import { messagingFeatureForRenderer } from "../feature";
|
||||
|
||||
describe("enlist message channel listener in renderer", () => {
|
||||
let enlistMessageChannelListener: EnlistMessageChannelListener;
|
||||
let ipcRendererStub: IpcRenderer;
|
||||
let onMock: jest.Mock;
|
||||
let offMock: jest.Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
const di = createContainer("irrelevant");
|
||||
|
||||
registerFeature(di, messagingFeatureForRenderer);
|
||||
|
||||
onMock = jest.fn();
|
||||
offMock = jest.fn();
|
||||
|
||||
ipcRendererStub = {
|
||||
on: onMock,
|
||||
off: offMock,
|
||||
} as unknown as IpcRenderer;
|
||||
|
||||
di.override(ipcRendererInjectable, () => ipcRendererStub);
|
||||
|
||||
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]({} as IpcRendererEvent, "some-message");
|
||||
});
|
||||
|
||||
it("calls the handler with the message", () => {
|
||||
expect(handlerMock).toHaveBeenCalledWith("some-message");
|
||||
});
|
||||
|
||||
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]({} as IpcRendererEvent, 42);
|
||||
|
||||
expect(handlerMock).toHaveBeenCalledWith(42);
|
||||
});
|
||||
|
||||
it("given boolean as message, when message arrives, calls the handler with the message", () => {
|
||||
onMock.mock.calls[0][1]({} as IpcRendererEvent, true);
|
||||
|
||||
expect(handlerMock).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it("given object as message, when message arrives, calls the handler with the message", () => {
|
||||
onMock.mock.calls[0][1]({} as IpcRendererEvent, { some: "object" });
|
||||
|
||||
expect(handlerMock).toHaveBeenCalledWith({ some: "object" });
|
||||
});
|
||||
});
|
||||
});
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
/* c8 ignore start */
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { enlistRequestChannelListenerInjectionToken } from "@k8slens/messaging";
|
||||
|
||||
const enlistRequestChannelListenerInjectable = getInjectable({
|
||||
id: "enlist-request-channel-listener-for-renderer",
|
||||
|
||||
instantiate: () => (listener) => {
|
||||
throw new Error(
|
||||
`Tried to enlist request channel "${listener.channel.id}" in "renderer", but requesting it's not supported yet.`,
|
||||
);
|
||||
},
|
||||
|
||||
injectionToken: enlistRequestChannelListenerInjectionToken,
|
||||
});
|
||||
|
||||
export default enlistRequestChannelListenerInjectable;
|
||||
/* c8 ignore end */
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import ipcRendererInjectable from "../ipc/ipc-renderer.injectable";
|
||||
|
||||
const invokeIpcInjectable = getInjectable({
|
||||
id: "invoke-ipc",
|
||||
|
||||
instantiate: (di) => di.inject(ipcRendererInjectable).invoke,
|
||||
});
|
||||
|
||||
export default invokeIpcInjectable;
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import { createContainer, DiContainer } from "@ogre-tools/injectable";
|
||||
import { registerFeature } from "@k8slens/feature-core";
|
||||
import { messagingFeatureForRenderer } from "../feature";
|
||||
import { ipcRenderer } from "electron";
|
||||
import invokeIpcInjectable from "./invoke-ipc.injectable";
|
||||
|
||||
describe("ipc-renderer", () => {
|
||||
let di: DiContainer;
|
||||
|
||||
beforeEach(() => {
|
||||
di = createContainer("irrelevant");
|
||||
|
||||
registerFeature(di, messagingFeatureForRenderer);
|
||||
});
|
||||
|
||||
it("is IPC-renderer invoke of Electron", () => {
|
||||
const actual = di.inject(invokeIpcInjectable);
|
||||
|
||||
expect(actual).toBe(ipcRenderer.invoke);
|
||||
});
|
||||
});
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { RequestFromChannel } from "@k8slens/messaging";
|
||||
import { requestFromChannelInjectionToken } from "@k8slens/messaging";
|
||||
import invokeIpcInjectable from "./invoke-ipc.injectable";
|
||||
|
||||
const requestFromChannelInjectable = getInjectable({
|
||||
id: "request-from-channel",
|
||||
|
||||
instantiate: (di) => {
|
||||
const invokeIpc = di.inject(invokeIpcInjectable);
|
||||
|
||||
return ((channel, request) => invokeIpc(channel.id, request)) as RequestFromChannel;
|
||||
},
|
||||
|
||||
injectionToken: requestFromChannelInjectionToken,
|
||||
});
|
||||
|
||||
export default requestFromChannelInjectable;
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
import { createContainer, DiContainer } from "@ogre-tools/injectable";
|
||||
import { registerFeature } from "@k8slens/feature-core";
|
||||
import { requestFromChannelInjectionToken } from "@k8slens/messaging";
|
||||
import { messagingFeatureForRenderer } from "../feature";
|
||||
import type { RequestChannel } from "@k8slens/messaging";
|
||||
import invokeIpcInjectable from "./invoke-ipc.injectable";
|
||||
import type { AsyncFnMock } from "@async-fn/jest";
|
||||
import asyncFn from "@async-fn/jest";
|
||||
|
||||
describe("request-from-channel", () => {
|
||||
let di: DiContainer;
|
||||
let invokeIpcMock: AsyncFnMock<() => Promise<number>>;
|
||||
|
||||
beforeEach(() => {
|
||||
di = createContainer("irrelevant");
|
||||
|
||||
registerFeature(di, messagingFeatureForRenderer);
|
||||
|
||||
invokeIpcMock = asyncFn();
|
||||
di.override(invokeIpcInjectable, () => invokeIpcMock);
|
||||
});
|
||||
|
||||
describe("when called", () => {
|
||||
let actualPromise: Promise<number>;
|
||||
|
||||
beforeEach(() => {
|
||||
const requestFromChannel = di.inject(requestFromChannelInjectionToken);
|
||||
|
||||
const someChannel: RequestChannel<string, number> = {
|
||||
id: "some-channel-id",
|
||||
};
|
||||
|
||||
actualPromise = requestFromChannel(someChannel, "some-request-payload");
|
||||
});
|
||||
|
||||
it("invokes ipcRenderer of Electron", () => {
|
||||
expect(invokeIpcMock).toHaveBeenCalledWith("some-channel-id", "some-request-payload");
|
||||
});
|
||||
|
||||
it("when invoke resolves with response, resolves with said response", async () => {
|
||||
await invokeIpcMock.resolve(42);
|
||||
|
||||
expect(await actualPromise).toBe(42);
|
||||
});
|
||||
});
|
||||
});
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import sendToIpcInjectable from "./send-to-ipc.injectable";
|
||||
import { SendMessageToChannel, sendMessageToChannelInjectionToken } from "@k8slens/messaging";
|
||||
|
||||
const messageToChannelInjectable = getInjectable({
|
||||
id: "message-to-channel",
|
||||
|
||||
instantiate: (di) => {
|
||||
const sendToIpc = di.inject(sendToIpcInjectable);
|
||||
|
||||
return ((channel, message) => {
|
||||
sendToIpc(channel.id, message);
|
||||
}) as SendMessageToChannel;
|
||||
},
|
||||
|
||||
injectionToken: sendMessageToChannelInjectionToken,
|
||||
});
|
||||
|
||||
export default messageToChannelInjectable;
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
import { createContainer, DiContainer } from "@ogre-tools/injectable";
|
||||
import { registerFeature } from "@k8slens/feature-core";
|
||||
import { sendMessageToChannelInjectionToken } from "@k8slens/messaging";
|
||||
import { messagingFeatureForRenderer } from "../feature";
|
||||
import type { MessageChannel } from "@k8slens/messaging";
|
||||
import sendToIpcInjectable from "./send-to-ipc.injectable";
|
||||
import type { AsyncFnMock } from "@async-fn/jest";
|
||||
import asyncFn from "@async-fn/jest";
|
||||
|
||||
describe("message-from-channel", () => {
|
||||
let di: DiContainer;
|
||||
let sendToIpcMock: AsyncFnMock<() => Promise<number>>;
|
||||
|
||||
beforeEach(() => {
|
||||
di = createContainer("irrelevant");
|
||||
|
||||
registerFeature(di, messagingFeatureForRenderer);
|
||||
|
||||
sendToIpcMock = asyncFn();
|
||||
di.override(sendToIpcInjectable, () => sendToIpcMock);
|
||||
});
|
||||
|
||||
describe("when called", () => {
|
||||
beforeEach(() => {
|
||||
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
|
||||
|
||||
const someChannel: MessageChannel<number> = {
|
||||
id: "some-channel-id",
|
||||
};
|
||||
|
||||
sendMessageToChannel(someChannel, 42);
|
||||
});
|
||||
|
||||
it("sends to ipcRenderer of Electron", () => {
|
||||
expect(sendToIpcMock).toHaveBeenCalledWith("some-channel-id", 42);
|
||||
});
|
||||
});
|
||||
});
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import ipcRendererInjectable from "../ipc/ipc-renderer.injectable";
|
||||
|
||||
const sendToIpcInjectable = getInjectable({
|
||||
id: "send-to-ipc",
|
||||
|
||||
instantiate: (di) => di.inject(ipcRendererInjectable).send,
|
||||
});
|
||||
|
||||
export default sendToIpcInjectable;
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import { createContainer, DiContainer } from "@ogre-tools/injectable";
|
||||
import { registerFeature } from "@k8slens/feature-core";
|
||||
import { messagingFeatureForRenderer } from "../feature";
|
||||
import { ipcRenderer } from "electron";
|
||||
import sendToIpcInjectable from "./send-to-ipc.injectable";
|
||||
|
||||
describe("ipc-renderer", () => {
|
||||
let di: DiContainer;
|
||||
|
||||
beforeEach(() => {
|
||||
di = createContainer("irrelevant");
|
||||
|
||||
registerFeature(di, messagingFeatureForRenderer);
|
||||
});
|
||||
|
||||
it("is IPC-renderer send of Electron", () => {
|
||||
const actual = di.inject(sendToIpcInjectable);
|
||||
|
||||
expect(actual).toBe(ipcRenderer.send);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user