From 51afa38d823b8ca19ac7add7ff523514e8e0ad7b Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Fri, 20 May 2022 12:37:55 +0300 Subject: [PATCH] Make Channel abstraction support return values Signed-off-by: Janne Savolainen --- .../ask-boolean-answer-channel.injectable.ts | 2 +- .../ask-boolean-question-channel.injectable.ts | 2 +- src/common/channel/channel-injection-token.ts | 5 +++-- .../channel/channel-listener-injection-token.ts | 6 +++--- src/common/channel/channel.test.ts | 2 +- .../enlist-channel-listener-injection-token.ts | 4 ++-- .../send-to-agnostic-channel-injection-token.ts | 2 +- .../root-frame-rendered-channel.injectable.ts | 2 +- src/common/sync-box/sync-box-channel.injectable.ts | 2 +- .../enlist-channel-listener.injectable.ts | 14 ++++++++++---- 10 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/common/ask-boolean/ask-boolean-answer-channel.injectable.ts b/src/common/ask-boolean/ask-boolean-answer-channel.injectable.ts index abdaa31a53..dfe2a7d2a2 100644 --- a/src/common/ask-boolean/ask-boolean-answer-channel.injectable.ts +++ b/src/common/ask-boolean/ask-boolean-answer-channel.injectable.ts @@ -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", diff --git a/src/common/ask-boolean/ask-boolean-question-channel.injectable.ts b/src/common/ask-boolean/ask-boolean-question-channel.injectable.ts index 10cdf4511e..63e9cdfb39 100644 --- a/src/common/ask-boolean/ask-boolean-question-channel.injectable.ts +++ b/src/common/ask-boolean/ask-boolean-question-channel.injectable.ts @@ -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; +export type AskBooleanQuestionChannel = Channel; const askBooleanQuestionChannelInjectable = getInjectable({ id: "ask-boolean-question-channel", diff --git a/src/common/channel/channel-injection-token.ts b/src/common/channel/channel-injection-token.ts index 586f5eeb35..5d5da231c9 100644 --- a/src/common/channel/channel-injection-token.ts +++ b/src/common/channel/channel-injection-token.ts @@ -5,11 +5,12 @@ import { getInjectionToken } from "@ogre-tools/injectable"; -export interface Channel { +export interface Channel { id: string; _messageTemplate?: MessageTemplate; + _returnTemplate?: ReturnTemplate; } -export const channelInjectionToken = getInjectionToken>({ +export const channelInjectionToken = getInjectionToken>({ id: "channel", }); diff --git a/src/common/channel/channel-listener-injection-token.ts b/src/common/channel/channel-listener-injection-token.ts index df7219c634..b148ab0317 100644 --- a/src/common/channel/channel-listener-injection-token.ts +++ b/src/common/channel/channel-listener-injection-token.ts @@ -5,12 +5,12 @@ import { getInjectionToken } from "@ogre-tools/injectable"; import type { Channel } from "./channel-injection-token"; -export interface ChannelListener> { +export interface ChannelListener> { channel: TChannel; - handler: (value: TChannel["_messageTemplate"]) => void; + handler: (value: TChannel["_messageTemplate"]) => TChannel["_returnTemplate"]; } -export const channelListenerInjectionToken = getInjectionToken>>( +export const channelListenerInjectionToken = getInjectionToken>>( { id: "channel-listener", }, diff --git a/src/common/channel/channel.test.ts b/src/common/channel/channel.test.ts index f3c70bd2a3..b71329e1be 100644 --- a/src/common/channel/channel.test.ts +++ b/src/common/channel/channel.test.ts @@ -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; +type TestChannel = Channel; describe("channel", () => { describe("messaging from main to renderer, given listener for channel in a window and application has started", () => { diff --git a/src/common/channel/enlist-channel-listener-injection-token.ts b/src/common/channel/enlist-channel-listener-injection-token.ts index 718bf923a5..b4728fb20d 100644 --- a/src/common/channel/enlist-channel-listener-injection-token.ts +++ b/src/common/channel/enlist-channel-listener-injection-token.ts @@ -5,9 +5,9 @@ import { getInjectionToken } from "@ogre-tools/injectable"; import type { Channel } from "./channel-injection-token"; -export type EnlistChannelListener = >( +export type EnlistChannelListener = >( channel: TChannel, - handler: (value: TChannel["_messageTemplate"]) => void + handler: (value: TChannel["_messageTemplate"]) => TChannel["_returnTemplate"] ) => () => void; export const enlistChannelListenerInjectionToken = diff --git a/src/common/channel/send-to-agnostic-channel-injection-token.ts b/src/common/channel/send-to-agnostic-channel-injection-token.ts index 6bc5b97107..a56e1fbb8b 100644 --- a/src/common/channel/send-to-agnostic-channel-injection-token.ts +++ b/src/common/channel/send-to-agnostic-channel-injection-token.ts @@ -5,7 +5,7 @@ import { getInjectionToken } from "@ogre-tools/injectable"; import type { Channel } from "./channel-injection-token"; -export type SendToAgnosticChannel = >( +export type SendToAgnosticChannel = >( channel: TChannel, message?: TChannel["_messageTemplate"] ) => void; diff --git a/src/common/root-frame-rendered-channel/root-frame-rendered-channel.injectable.ts b/src/common/root-frame-rendered-channel/root-frame-rendered-channel.injectable.ts index ab16382f7a..189c582ca0 100644 --- a/src/common/root-frame-rendered-channel/root-frame-rendered-channel.injectable.ts +++ b/src/common/root-frame-rendered-channel/root-frame-rendered-channel.injectable.ts @@ -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; +export type RootFrameRenderedChannel = Channel; const rootFrameRenderedChannelInjectable = getInjectable({ id: "root-frame-rendered-channel", diff --git a/src/common/sync-box/sync-box-channel.injectable.ts b/src/common/sync-box/sync-box-channel.injectable.ts index 7b9262f607..15a090d002 100644 --- a/src/common/sync-box/sync-box-channel.injectable.ts +++ b/src/common/sync-box/sync-box-channel.injectable.ts @@ -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", diff --git a/src/main/channel/channel-listeners/enlist-channel-listener.injectable.ts b/src/main/channel/channel-listeners/enlist-channel-listener.injectable.ts index 87ddf28860..5f2c8d79ff 100644 --- a/src/main/channel/channel-listeners/enlist-channel-listener.injectable.ts +++ b/src/main/channel/channel-listeners/enlist-channel-listener.injectable.ts @@ -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); }; }; },