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

Require messages for MessageChannels be JsonValues for serialization

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-turso 2022-05-30 13:01:11 +03:00
parent 81db46f69c
commit c156dcf4ae
10 changed files with 20 additions and 15 deletions

View File

@ -3,6 +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 { JsonObject } from "type-fest";
import type { MessageChannel } from "../channel/message-channel-injection-token"; import type { MessageChannel } from "../channel/message-channel-injection-token";
import { messageChannelInjectionToken } from "../channel/message-channel-injection-token"; import { messageChannelInjectionToken } from "../channel/message-channel-injection-token";
@ -12,7 +13,7 @@ export type ApplicationUpdateStatusEventId =
| "download-for-update-started" | "download-for-update-started"
| "download-for-update-failed"; | "download-for-update-failed";
export interface ApplicationUpdateStatusChannelMessage { eventId: ApplicationUpdateStatusEventId; version?: string } export interface ApplicationUpdateStatusChannelMessage extends JsonObject { eventId: ApplicationUpdateStatusEventId; version?: string }
export type ApplicationUpdateStatusChannel = MessageChannel<ApplicationUpdateStatusChannelMessage>; export type ApplicationUpdateStatusChannel = MessageChannel<ApplicationUpdateStatusChannelMessage>;
const applicationUpdateStatusChannelInjectable = getInjectable({ const applicationUpdateStatusChannelInjectable = getInjectable({

View File

@ -3,10 +3,11 @@
* 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 { JsonObject } from "type-fest";
import type { MessageChannel } from "../channel/message-channel-injection-token"; import type { MessageChannel } from "../channel/message-channel-injection-token";
import { messageChannelInjectionToken } from "../channel/message-channel-injection-token"; import { messageChannelInjectionToken } from "../channel/message-channel-injection-token";
export interface AskBooleanQuestionParameters { id: string; title: string; question: string } export interface AskBooleanQuestionParameters extends JsonObject { id: string; title: string; question: string }
export type AskBooleanQuestionChannel = MessageChannel<AskBooleanQuestionParameters>; export type AskBooleanQuestionChannel = MessageChannel<AskBooleanQuestionParameters>;
const askBooleanQuestionChannelInjectable = getInjectable({ const askBooleanQuestionChannelInjectable = getInjectable({

View File

@ -7,7 +7,7 @@ import type { MessageChannel } from "./message-channel-injection-token";
import type { MessageChannelListener } from "./message-channel-listener-injection-token"; import type { MessageChannelListener } from "./message-channel-listener-injection-token";
export type EnlistMessageChannelListener = < export type EnlistMessageChannelListener = <
TChannel extends MessageChannel<unknown>, TChannel extends MessageChannel<any>,
>(listener: MessageChannelListener<TChannel>) => () => void; >(listener: MessageChannelListener<TChannel>) => () => void;
export const enlistMessageChannelListenerInjectionToken = export const enlistMessageChannelListenerInjectionToken =

View File

@ -4,8 +4,9 @@
*/ */
import { getInjectionToken } from "@ogre-tools/injectable"; import { getInjectionToken } from "@ogre-tools/injectable";
import type { JsonValue } from "type-fest";
export interface MessageChannel<Message = void> { export interface MessageChannel<Message extends JsonValue | void = void> {
id: string; id: string;
_messageSignature?: Message; _messageSignature?: Message;
} }

View File

@ -3,6 +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 { getInjectionToken } from "@ogre-tools/injectable"; import { getInjectionToken } from "@ogre-tools/injectable";
import type { SetRequired } from "type-fest";
import type { MessageChannel } from "./message-channel-injection-token"; import type { MessageChannel } from "./message-channel-injection-token";
export interface MessageToChannel { export interface MessageToChannel {
@ -10,9 +11,9 @@ export interface MessageToChannel {
channel: TChannel, channel: TChannel,
): void; ): void;
<TChannel extends MessageChannel<TMessage>, TMessage>( <TChannel extends MessageChannel<any>>(
channel: TChannel, channel: TChannel,
message: TMessage message: SetRequired<TChannel, "_messageSignature">["_messageSignature"],
): void; ): void;
} }

View File

@ -17,7 +17,7 @@ const messageToChannelInjectable = getInjectable({
// TODO: Figure out way to improve typing in internals // TODO: Figure out way to improve typing in internals
// Notice that this should be injected using "messageToChannelInjectionToken" which is typed correctly. // Notice that this should be injected using "messageToChannelInjectionToken" which is typed correctly.
return (channel: MessageChannel<unknown>, message?: unknown) => { return (channel: MessageChannel<any>, message?: unknown) => {
const visibleWindows = pipeline( const visibleWindows = pipeline(
getAllLensWindows(), getAllLensWindows(),
filter((lensWindow) => !!lensWindow.visible), filter((lensWindow) => !!lensWindow.visible),

View File

@ -15,7 +15,7 @@ const messageToChannelInjectable = getInjectable({
// TODO: Figure out way to improve typing in internals // TODO: Figure out way to improve typing in internals
// Notice that this should be injected using "messageToChannelInjectionToken" which is typed correctly. // Notice that this should be injected using "messageToChannelInjectionToken" which is typed correctly.
return (channel: MessageChannel<unknown>, message?: unknown) => { return (channel: MessageChannel<any>, message?: unknown) => {
sendToMain(channel.id, message); sendToMain(channel.id, message);
}; };
}, },

View File

@ -3,6 +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 { JsonValue } from "type-fest";
import ipcRendererInjectable from "./ipc-renderer.injectable"; import ipcRendererInjectable from "./ipc-renderer.injectable";
const sendToMainInjectable = getInjectable({ const sendToMainInjectable = getInjectable({
@ -12,7 +13,7 @@ const sendToMainInjectable = getInjectable({
const ipcRenderer = di.inject(ipcRendererInjectable); const ipcRenderer = di.inject(ipcRendererInjectable);
// TODO: Figure out way to improve typing in internals // TODO: Figure out way to improve typing in internals
return (channelId: string, message: any) => { return <T>(channelId: string, message: JsonValue extends T ? T : never ) => {
ipcRenderer.send(channelId, ...(message ? [message] : [])); ipcRenderer.send(channelId, ...(message ? [message] : []));
}; };
}, },

View File

@ -13,7 +13,7 @@ import assert from "assert";
export const overrideMessagingFromMainToWindow = (mainDi: DiContainer) => { export const overrideMessagingFromMainToWindow = (mainDi: DiContainer) => {
const messageChannelListenerFakesForRenderer = new Map< const messageChannelListenerFakesForRenderer = new Map<
string, string,
Set<MessageChannelListener<MessageChannel<unknown>>> Set<MessageChannelListener<MessageChannel<any>>>
>(); >();
mainDi.override( mainDi.override(
@ -73,14 +73,14 @@ export const overrideMessagingFromMainToWindow = (mainDi: DiContainer) => {
// TODO: Figure out typing // TODO: Figure out typing
listeners.add( listeners.add(
listener as unknown as MessageChannelListener<MessageChannel<unknown>>, listener as unknown as MessageChannelListener<MessageChannel<any>>,
); );
return () => { return () => {
// TODO: Figure out typing // TODO: Figure out typing
listeners.delete( listeners.delete(
listener as unknown as MessageChannelListener< listener as unknown as MessageChannelListener<
MessageChannel<unknown> MessageChannel<any>
>, >,
); );
}; };

View File

@ -12,7 +12,7 @@ import sendToMainInjectable from "../../renderer/channel/send-to-main.injectable
export const overrideMessagingFromWindowToMain = (mainDi: DiContainer) => { export const overrideMessagingFromWindowToMain = (mainDi: DiContainer) => {
const messageChannelListenerFakesForMain = new Map< const messageChannelListenerFakesForMain = new Map<
string, string,
Set<MessageChannelListener<MessageChannel<unknown>>> Set<MessageChannelListener<MessageChannel<any>>>
>(); >();
mainDi.override( mainDi.override(
@ -33,13 +33,13 @@ export const overrideMessagingFromWindowToMain = (mainDi: DiContainer) => {
// TODO: Figure out typing // TODO: Figure out typing
listeners.add( listeners.add(
listener as unknown as MessageChannelListener<MessageChannel<unknown>>, listener as unknown as MessageChannelListener<MessageChannel<any>>,
); );
return () => { return () => {
// TODO: Figure out typing // TODO: Figure out typing
listeners.delete( listeners.delete(
listener as unknown as MessageChannelListener<MessageChannel<unknown>>, listener as unknown as MessageChannelListener<MessageChannel<any>>,
); );
}; };
}, },