Introduce competition for channel abstraction

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne SavolainenandMikko Aspiala committed 2022-06-03 07:48:00 +03:00
1 parent 3cb4a11f1b
commit f6e05cb724
14 files changed
+504 -1

No files matched your search

@@ -0,0 +1,13 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
export interface Channel {
id: string;
}
export const channelInjectionToken = getInjectionToken<Channel>({
id: "channel",
});
@@ -0,0 +1,16 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
export interface ChannelListener {
channel: any;
handler: (value: any) => void;
}
export const channelListenerInjectionToken = getInjectionToken<ChannelListener>(
{
id: "channel-listener",
},
);
+187
View File
@@ -0,0 +1,187 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DiContainer } from "@ogre-tools/injectable";
import { getInjectable } from "@ogre-tools/injectable";
import type { LensWindow } from "../../../main/start-main-application/lens-window/application-window/lens-window-injection-token";
import { lensWindowInjectionToken } from "../../../main/start-main-application/lens-window/application-window/lens-window-injection-token";
import { sendToAgnosticChannelInjectionToken } from "./send-to-agnostic-channel-injection-token";
import { getApplicationBuilder } from "../../../renderer/components/test-utils/get-application-builder";
import { channelListenerInjectionToken } from "./channel-listener-injection-token";
import createLensWindowInjectable from "../../../main/start-main-application/lens-window/application-window/create-lens-window.injectable";
import type { Channel } from "./channel-injection-token";
import { channelInjectionToken } from "./channel-injection-token";
describe("channel", () => {
describe("messaging from main to renderer, given listener for channel in a window and application has started", () => {
let testChannel: Channel;
let testListenerInWindowMock: jest.Mock;
let mainDi: DiContainer;
let sendToAgnosticChannel: (channel: Channel, message: any) => void;
beforeEach(async () => {
const applicationBuilder = getApplicationBuilder();
mainDi = applicationBuilder.dis.mainDi;
const rendererDi = applicationBuilder.dis.rendererDi;
testListenerInWindowMock = jest.fn();
const testChannelListenerInTestWindowInjectable = getInjectable({
id: "test-channel-listener-in-test-window",
instantiate: (di) => ({
channel: di.inject(testChannelInjectable),
handler: testListenerInWindowMock,
}),
injectionToken: channelListenerInjectionToken,
});
rendererDi.register(testChannelListenerInTestWindowInjectable);
// Notice how test channel has presence in both DIs, being from common
mainDi.register(testChannelInjectable);
rendererDi.register(testChannelInjectable);
testChannel = mainDi.inject(testChannelInjectable);
sendToAgnosticChannel = mainDi.inject(
sendToAgnosticChannelInjectionToken,
);
await applicationBuilder.render();
});
describe("given window is shown", () => {
let someWindowFake: LensWindow;
beforeEach(async () => {
someWindowFake = createTestWindow(mainDi, "some-window");
await someWindowFake.show();
});
it("when sending message, triggers listener in window", () => {
sendToAgnosticChannel(testChannel, "some-message");
expect(testListenerInWindowMock).toHaveBeenCalledWith("some-message");
});
it("given window is hidden, when sending message, does not trigger listener in window", () => {
someWindowFake.close();
sendToAgnosticChannel(testChannel, "some-message");
expect(testListenerInWindowMock).not.toHaveBeenCalled();
});
});
it("given multiple shown windows, when sending message, triggers listeners in all windows", async () => {
const someWindowFake = createTestWindow(mainDi, "some-window");
const someOtherWindowFake = createTestWindow(mainDi, "some-other-window");
await someWindowFake.show();
await someOtherWindowFake.show();
sendToAgnosticChannel(testChannel, "some-message");
expect(testListenerInWindowMock.mock.calls).toEqual([
["some-message"],
["some-message"],
]);
});
});
describe("messaging from renderer to main, given listener for channel in a main and application has started", () => {
let testChannel: Channel;
let testListenerInMainMock: jest.Mock;
let rendererDi: DiContainer;
let mainDi: DiContainer;
let sendToAgnosticChannel: (channel: Channel, message: any) => void;
beforeEach(async () => {
const applicationBuilder = getApplicationBuilder();
mainDi = applicationBuilder.dis.mainDi;
rendererDi = applicationBuilder.dis.rendererDi;
testListenerInMainMock = jest.fn();
const testChannelListenerInMainInjectable = getInjectable({
id: "test-channel-listener-in-main",
instantiate: (di) => ({
channel: di.inject(testChannelInjectable),
handler: testListenerInMainMock,
}),
injectionToken: channelListenerInjectionToken,
});
mainDi.register(testChannelListenerInMainInjectable);
// Notice how test channel has presence in both DIs, being from common
mainDi.register(testChannelInjectable);
rendererDi.register(testChannelInjectable);
testChannel = rendererDi.inject(testChannelInjectable);
sendToAgnosticChannel = rendererDi.inject(
sendToAgnosticChannelInjectionToken,
);
await applicationBuilder.render();
});
it("when sending message, triggers listener in main", () => {
sendToAgnosticChannel(testChannel, "some-message");
expect(testListenerInMainMock).toHaveBeenCalledWith("some-message");
});
});
});
const testChannelInjectable = getInjectable({
id: "some-test-channel",
instantiate: () => {
const channelId = "some-channel-id";
return {
id: channelId,
};
},
injectionToken: channelInjectionToken,
});
const createTestWindow = (di: DiContainer, id: string) => {
const testWindowInjectable = getInjectable({
id,
instantiate: (di) => {
const createLensWindow = di.inject(createLensWindowInjectable);
return createLensWindow({
id,
title: "Some test window",
defaultHeight: 42,
defaultWidth: 42,
getContentUrl: () => "some-content-url",
resizable: true,
windowFrameUtilitiesAreShown: false,
centered: false,
});
},
injectionToken: lensWindowInjectionToken,
});
di.register(testWindowInjectable);
return di.inject(testWindowInjectable);
};
@@ -0,0 +1,11 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
export const enlistChannelListenerInjectionToken = getInjectionToken<
(channel: any, handler: any) => () => void
>({
id: "enlist-channel-listener",
});
@@ -0,0 +1,32 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { getStartableStoppable } from "../../utils/get-startable-stoppable";
import { channelListenerInjectionToken } from "./channel-listener-injection-token";
import { enlistChannelListenerInjectionToken } from "./enlist-channel-listener-injection-token";
const listeningOfChannelsInjectable = getInjectable({
id: "listening-of-channels",
instantiate: (di) => {
const enlistChannelListener = di.inject(enlistChannelListenerInjectionToken);
const channelListeners = di.injectMany(channelListenerInjectionToken);
return getStartableStoppable("listening-of-channels", () => {
const disposers = channelListeners.map(({ channel, handler }) =>
enlistChannelListener(channel, handler),
);
return () => {
disposers.forEach((disposer) => {
disposer();
});
};
});
},
});
export default listeningOfChannelsInjectable;
@@ -0,0 +1,10 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { Channel } from "./channel-injection-token";
export const sendToAgnosticChannelInjectionToken = getInjectionToken<(channel: Channel, message: any) => void>({
id: "send-to-agnostic-channel",
});