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:
parent
17ca80f895
commit
51afa38d82
@ -6,7 +6,7 @@ import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { Channel } 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({
|
||||
id: "ask-boolean-answer-channel",
|
||||
|
||||
@ -7,7 +7,7 @@ import type { Channel } from "../channel/channel-injection-token";
|
||||
import { channelInjectionToken } from "../channel/channel-injection-token";
|
||||
|
||||
export interface AskBooleanQuestionParameters { id: string; title: string; question: string }
|
||||
export type AskBooleanQuestionChannel = Channel<AskBooleanQuestionParameters>;
|
||||
export type AskBooleanQuestionChannel = Channel<AskBooleanQuestionParameters, never>;
|
||||
|
||||
const askBooleanQuestionChannelInjectable = getInjectable({
|
||||
id: "ask-boolean-question-channel",
|
||||
|
||||
@ -5,11 +5,12 @@
|
||||
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
|
||||
export interface Channel<MessageTemplate> {
|
||||
export interface Channel<MessageTemplate, ReturnTemplate> {
|
||||
id: string;
|
||||
_messageTemplate?: MessageTemplate;
|
||||
_returnTemplate?: ReturnTemplate;
|
||||
}
|
||||
|
||||
export const channelInjectionToken = getInjectionToken<Channel<any>>({
|
||||
export const channelInjectionToken = getInjectionToken<Channel<any, any>>({
|
||||
id: "channel",
|
||||
});
|
||||
|
||||
@ -5,12 +5,12 @@
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
import type { Channel } from "./channel-injection-token";
|
||||
|
||||
export interface ChannelListener<TChannel extends Channel<any>> {
|
||||
export interface ChannelListener<TChannel extends Channel<any, any>> {
|
||||
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",
|
||||
},
|
||||
|
||||
@ -14,7 +14,7 @@ import createLensWindowInjectable from "../../main/start-main-application/lens-w
|
||||
import type { Channel } from "./channel-injection-token";
|
||||
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("messaging from main to renderer, given listener for channel in a window and application has started", () => {
|
||||
|
||||
@ -5,9 +5,9 @@
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
import type { Channel } from "./channel-injection-token";
|
||||
|
||||
export type EnlistChannelListener = <TChannel extends Channel<unknown>>(
|
||||
export type EnlistChannelListener = <TChannel extends Channel<unknown, unknown>>(
|
||||
channel: TChannel,
|
||||
handler: (value: TChannel["_messageTemplate"]) => void
|
||||
handler: (value: TChannel["_messageTemplate"]) => TChannel["_returnTemplate"]
|
||||
) => () => void;
|
||||
|
||||
export const enlistChannelListenerInjectionToken =
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
import type { Channel } from "./channel-injection-token";
|
||||
|
||||
export type SendToAgnosticChannel = <TChannel extends Channel<unknown>>(
|
||||
export type SendToAgnosticChannel = <TChannel extends Channel<unknown, unknown>>(
|
||||
channel: TChannel,
|
||||
message?: TChannel["_messageTemplate"]
|
||||
) => void;
|
||||
|
||||
@ -6,7 +6,7 @@ import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { Channel } 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({
|
||||
id: "root-frame-rendered-channel",
|
||||
|
||||
@ -6,7 +6,7 @@ import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { Channel } 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({
|
||||
id: "sync-box-channel",
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
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 { enlistChannelListenerInjectionToken } from "../../../common/channel/enlist-channel-listener-injection-token";
|
||||
|
||||
@ -14,13 +14,19 @@ const enlistChannelListenerInjectable = getInjectable({
|
||||
const ipcMain = di.inject(ipcMainInjectable);
|
||||
|
||||
return (channel, handler) => {
|
||||
const nativeCallback = (_: IpcMainEvent, message: unknown) =>
|
||||
const nativeOnCallback = (_: IpcMainEvent, message: unknown) =>
|
||||
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 () => {
|
||||
ipcMain.off(channel.id, nativeCallback);
|
||||
ipcMain.off(channel.id, nativeOnCallback);
|
||||
ipcMain.off(channel.id, nativeHandleCallback);
|
||||
};
|
||||
};
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user