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

Make Channel abstraction support return values

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-05-20 12:37:55 +03:00
parent 17ca80f895
commit 51afa38d82
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
10 changed files with 24 additions and 17 deletions

View File

@ -6,7 +6,7 @@ import { getInjectable } from "@ogre-tools/injectable";
import type { Channel } from "../channel/channel-injection-token"; import type { Channel } from "../channel/channel-injection-token";
import { channelInjectionToken } from "../channel/channel-injection-token"; import { channelInjectionToken } from "../channel/channel-injection-token";
type AskBooleanAnswerChannel = Channel<{ id: string; value: boolean }>; type AskBooleanAnswerChannel = Channel<{ id: string; value: boolean }, never>;
const askBooleanAnswerChannelInjectable = getInjectable({ const askBooleanAnswerChannelInjectable = getInjectable({
id: "ask-boolean-answer-channel", id: "ask-boolean-answer-channel",

View File

@ -7,7 +7,7 @@ import type { Channel } from "../channel/channel-injection-token";
import { channelInjectionToken } from "../channel/channel-injection-token"; import { channelInjectionToken } from "../channel/channel-injection-token";
export interface AskBooleanQuestionParameters { id: string; title: string; question: string } export interface AskBooleanQuestionParameters { id: string; title: string; question: string }
export type AskBooleanQuestionChannel = Channel<AskBooleanQuestionParameters>; export type AskBooleanQuestionChannel = Channel<AskBooleanQuestionParameters, never>;
const askBooleanQuestionChannelInjectable = getInjectable({ const askBooleanQuestionChannelInjectable = getInjectable({
id: "ask-boolean-question-channel", id: "ask-boolean-question-channel",

View File

@ -5,11 +5,12 @@
import { getInjectionToken } from "@ogre-tools/injectable"; import { getInjectionToken } from "@ogre-tools/injectable";
export interface Channel<MessageTemplate> { export interface Channel<MessageTemplate, ReturnTemplate> {
id: string; id: string;
_messageTemplate?: MessageTemplate; _messageTemplate?: MessageTemplate;
_returnTemplate?: ReturnTemplate;
} }
export const channelInjectionToken = getInjectionToken<Channel<any>>({ export const channelInjectionToken = getInjectionToken<Channel<any, any>>({
id: "channel", id: "channel",
}); });

View File

@ -5,12 +5,12 @@
import { getInjectionToken } from "@ogre-tools/injectable"; import { getInjectionToken } from "@ogre-tools/injectable";
import type { Channel } from "./channel-injection-token"; import type { Channel } from "./channel-injection-token";
export interface ChannelListener<TChannel extends Channel<any>> { export interface ChannelListener<TChannel extends Channel<any, any>> {
channel: TChannel; channel: TChannel;
handler: (value: TChannel["_messageTemplate"]) => void; handler: (value: TChannel["_messageTemplate"]) => TChannel["_returnTemplate"];
} }
export const channelListenerInjectionToken = getInjectionToken<ChannelListener<Channel<any>>>( export const channelListenerInjectionToken = getInjectionToken<ChannelListener<Channel<any, any>>>(
{ {
id: "channel-listener", id: "channel-listener",
}, },

View File

@ -14,7 +14,7 @@ import createLensWindowInjectable from "../../main/start-main-application/lens-w
import type { Channel } from "./channel-injection-token"; import type { Channel } from "./channel-injection-token";
import closeAllWindowsInjectable from "../../main/start-main-application/lens-window/hide-all-windows/close-all-windows.injectable"; import closeAllWindowsInjectable from "../../main/start-main-application/lens-window/hide-all-windows/close-all-windows.injectable";
type TestChannel = Channel<string>; type TestChannel = Channel<string, never>;
describe("channel", () => { describe("channel", () => {
describe("messaging from main to renderer, given listener for channel in a window and application has started", () => { describe("messaging from main to renderer, given listener for channel in a window and application has started", () => {

View File

@ -5,9 +5,9 @@
import { getInjectionToken } from "@ogre-tools/injectable"; import { getInjectionToken } from "@ogre-tools/injectable";
import type { Channel } from "./channel-injection-token"; import type { Channel } from "./channel-injection-token";
export type EnlistChannelListener = <TChannel extends Channel<unknown>>( export type EnlistChannelListener = <TChannel extends Channel<unknown, unknown>>(
channel: TChannel, channel: TChannel,
handler: (value: TChannel["_messageTemplate"]) => void handler: (value: TChannel["_messageTemplate"]) => TChannel["_returnTemplate"]
) => () => void; ) => () => void;
export const enlistChannelListenerInjectionToken = export const enlistChannelListenerInjectionToken =

View File

@ -5,7 +5,7 @@
import { getInjectionToken } from "@ogre-tools/injectable"; import { getInjectionToken } from "@ogre-tools/injectable";
import type { Channel } from "./channel-injection-token"; import type { Channel } from "./channel-injection-token";
export type SendToAgnosticChannel = <TChannel extends Channel<unknown>>( export type SendToAgnosticChannel = <TChannel extends Channel<unknown, unknown>>(
channel: TChannel, channel: TChannel,
message?: TChannel["_messageTemplate"] message?: TChannel["_messageTemplate"]
) => void; ) => void;

View File

@ -6,7 +6,7 @@ import { getInjectable } from "@ogre-tools/injectable";
import type { Channel } from "../channel/channel-injection-token"; import type { Channel } from "../channel/channel-injection-token";
import { channelInjectionToken } from "../channel/channel-injection-token"; import { channelInjectionToken } from "../channel/channel-injection-token";
export type RootFrameRenderedChannel = Channel<void>; export type RootFrameRenderedChannel = Channel<void, never>;
const rootFrameRenderedChannelInjectable = getInjectable({ const rootFrameRenderedChannelInjectable = getInjectable({
id: "root-frame-rendered-channel", id: "root-frame-rendered-channel",

View File

@ -6,7 +6,7 @@ import { getInjectable } from "@ogre-tools/injectable";
import type { Channel } from "../channel/channel-injection-token"; import type { Channel } from "../channel/channel-injection-token";
import { channelInjectionToken } from "../channel/channel-injection-token"; import { channelInjectionToken } from "../channel/channel-injection-token";
export type SyncBoxChannel = Channel<{ id: string; value: unknown }>; export type SyncBoxChannel = Channel<{ id: string; value: unknown }, never>;
const syncBoxChannelInjectable = getInjectable({ const syncBoxChannelInjectable = getInjectable({
id: "sync-box-channel", id: "sync-box-channel",

View File

@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type { IpcMainEvent } from "electron"; import type { IpcMainEvent, IpcMainInvokeEvent } from "electron";
import ipcMainInjectable from "../../app-paths/register-channel/ipc-main/ipc-main.injectable"; import ipcMainInjectable from "../../app-paths/register-channel/ipc-main/ipc-main.injectable";
import { enlistChannelListenerInjectionToken } from "../../../common/channel/enlist-channel-listener-injection-token"; import { enlistChannelListenerInjectionToken } from "../../../common/channel/enlist-channel-listener-injection-token";
@ -14,13 +14,19 @@ const enlistChannelListenerInjectable = getInjectable({
const ipcMain = di.inject(ipcMainInjectable); const ipcMain = di.inject(ipcMainInjectable);
return (channel, handler) => { return (channel, handler) => {
const nativeCallback = (_: IpcMainEvent, message: unknown) => const nativeOnCallback = (_: IpcMainEvent, message: unknown) =>
handler(message); handler(message);
ipcMain.on(channel.id, nativeCallback); ipcMain.on(channel.id, nativeOnCallback);
const nativeHandleCallback = (_: IpcMainInvokeEvent, message: unknown) =>
handler(message);
ipcMain.handle(channel.id, nativeHandleCallback);
return () => { return () => {
ipcMain.off(channel.id, nativeCallback); ipcMain.off(channel.id, nativeOnCallback);
ipcMain.off(channel.id, nativeHandleCallback);
}; };
}; };
}, },