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

Enhance typing of channels and sync-box

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

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-05-16 16:18:18 +03:00
parent aadd25c9ba
commit 08420fd0c2
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
15 changed files with 52 additions and 29 deletions

View File

@ -4,6 +4,7 @@
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import createSyncBoxInjectable from "../../sync-box/create-sync-box.injectable"; import createSyncBoxInjectable from "../../sync-box/create-sync-box.injectable";
import type { UpdateChannel } from "../../../main/update-app/update-channels";
const discoveredUpdateVersionInjectable = getInjectable({ const discoveredUpdateVersionInjectable = getInjectable({
id: "discovered-update-version", id: "discovered-update-version",
@ -11,7 +12,9 @@ const discoveredUpdateVersionInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const createSyncBox = di.inject(createSyncBoxInjectable); const createSyncBox = di.inject(createSyncBoxInjectable);
return createSyncBox("discovered-update-version"); return createSyncBox<{ version: string; updateChannel: UpdateChannel }>(
"discovered-update-version",
);
}, },
}); });

View File

@ -11,7 +11,7 @@ const progressOfUpdateDownloadInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const createSyncBox = di.inject(createSyncBoxInjectable); const createSyncBox = di.inject(createSyncBoxInjectable);
return createSyncBox("progress-of-update-download"); return createSyncBox<number>("progress-of-update-download");
}, },
}); });

View File

@ -11,7 +11,7 @@ const updateIsBeingDownloadedInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const createSyncBox = di.inject(createSyncBoxInjectable); const createSyncBox = di.inject(createSyncBoxInjectable);
return createSyncBox("update-is-being-downloaded"); return createSyncBox<boolean>("update-is-being-downloaded");
}, },
}); });

View File

@ -11,7 +11,7 @@ const updatesAreBeingDiscoveredInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const createSyncBox = di.inject(createSyncBoxInjectable); const createSyncBox = di.inject(createSyncBoxInjectable);
return createSyncBox("updates-are-being-discovered"); return createSyncBox<boolean>("updates-are-being-discovered");
}, },
}); });

View File

@ -4,10 +4,11 @@
*/ */
import { getInjectionToken } from "@ogre-tools/injectable"; import { getInjectionToken } from "@ogre-tools/injectable";
export interface Channel { export interface Channel<TInstance> {
id: string; id: string;
_template?: TInstance;
} }
export const channelInjectionToken = getInjectionToken<Channel>({ export const channelInjectionToken = getInjectionToken<Channel<unknown>>({
id: "channel", id: "channel",
}); });

View File

@ -3,13 +3,14 @@
* 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 { Channel } from "./channel-injection-token";
export interface ChannelListener { export interface ChannelListener<TChannel extends Channel<unknown>> {
channel: any; channel: TChannel;
handler: (value: any) => void; handler: (value: TChannel["_template"]) => void;
} }
export const channelListenerInjectionToken = getInjectionToken<ChannelListener>( export const channelListenerInjectionToken = getInjectionToken<ChannelListener<Channel<unknown>>>(
{ {
id: "channel-listener", id: "channel-listener",
}, },

View File

@ -6,6 +6,7 @@ import type { DiContainer } from "@ogre-tools/injectable";
import { getInjectable } 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 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 { lensWindowInjectionToken } from "../../main/start-main-application/lens-window/application-window/lens-window-injection-token";
import type { SendToAgnosticChannel } from "./send-to-agnostic-channel-injection-token";
import { sendToAgnosticChannelInjectionToken } from "./send-to-agnostic-channel-injection-token"; import { sendToAgnosticChannelInjectionToken } from "./send-to-agnostic-channel-injection-token";
import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder"; import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder";
import { channelListenerInjectionToken } from "./channel-listener-injection-token"; import { channelListenerInjectionToken } from "./channel-listener-injection-token";
@ -13,12 +14,14 @@ import createLensWindowInjectable from "../../main/start-main-application/lens-w
import type { Channel } from "./channel-injection-token"; import type { Channel } from "./channel-injection-token";
import { channelInjectionToken } from "./channel-injection-token"; import { channelInjectionToken } from "./channel-injection-token";
type TestChannel = Channel<string>;
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", () => {
let testChannel: Channel; let testChannel: TestChannel;
let testListenerInWindowMock: jest.Mock; let testListenerInWindowMock: jest.Mock;
let mainDi: DiContainer; let mainDi: DiContainer;
let sendToAgnosticChannel: (channel: Channel, message: any) => void; let sendToAgnosticChannel: SendToAgnosticChannel;
beforeEach(async () => { beforeEach(async () => {
const applicationBuilder = getApplicationBuilder(); const applicationBuilder = getApplicationBuilder();
@ -96,11 +99,11 @@ describe("channel", () => {
}); });
describe("messaging from renderer to main, given listener for channel in a main and application has started", () => { describe("messaging from renderer to main, given listener for channel in a main and application has started", () => {
let testChannel: Channel; let testChannel: TestChannel;
let testListenerInMainMock: jest.Mock; let testListenerInMainMock: jest.Mock;
let rendererDi: DiContainer; let rendererDi: DiContainer;
let mainDi: DiContainer; let mainDi: DiContainer;
let sendToAgnosticChannel: (channel: Channel, message: any) => void; let sendToAgnosticChannel: SendToAgnosticChannel;
beforeEach(async () => { beforeEach(async () => {
const applicationBuilder = getApplicationBuilder(); const applicationBuilder = getApplicationBuilder();

View File

@ -3,9 +3,14 @@
* 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 { Channel } from "./channel-injection-token";
export const enlistChannelListenerInjectionToken = getInjectionToken< export type EnlistChannelListener = <TChannel extends Channel<unknown>>(
(channel: any, handler: any) => () => void channel: TChannel,
>({ handler: (value: TChannel["_template"]) => void
id: "enlist-channel-listener", ) => () => void;
});
export const enlistChannelListenerInjectionToken =
getInjectionToken<EnlistChannelListener>({
id: "enlist-channel-listener",
});

View File

@ -5,6 +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 const sendToAgnosticChannelInjectionToken = getInjectionToken<(channel: Channel, message: any) => void>({ export type SendToAgnosticChannel = <TChannel extends Channel<unknown>>(
id: "send-to-agnostic-channel", channel: TChannel,
}); message: TChannel["_template"]
) => void;
export const sendToAgnosticChannelInjectionToken =
getInjectionToken<SendToAgnosticChannel>({
id: "send-to-agnostic-channel",
});

View File

@ -17,7 +17,7 @@ const createSyncBoxInjectable = getInjectable({
const sendToAgnosticChannel = di.inject(sendToAgnosticChannelInjectionToken); const sendToAgnosticChannel = di.inject(sendToAgnosticChannelInjectionToken);
const getSyncBoxState = (id: string) => di.inject(syncBoxStateInjectable, id); const getSyncBoxState = (id: string) => di.inject(syncBoxStateInjectable, id);
return (id: string): SyncBox<any> => { return <TData>(id: string): SyncBox<TData> => {
const state = getSyncBoxState(id); const state = getSyncBoxState(id);
return { return {
@ -37,7 +37,6 @@ const createSyncBoxInjectable = getInjectable({
export default createSyncBoxInjectable; export default createSyncBoxInjectable;
export interface SyncBox<TValue> { export interface SyncBox<TValue> {
id: string; id: string;
value: IComputedValue<TValue>; value: IComputedValue<TValue>;

View File

@ -3,14 +3,16 @@
* 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 { SyncBoxChannel } from "./sync-box-channel.injectable";
import syncBoxChannelInjectable from "./sync-box-channel.injectable"; import syncBoxChannelInjectable from "./sync-box-channel.injectable";
import type { ChannelListener } from "../channel/channel-listener-injection-token";
import { channelListenerInjectionToken } from "../channel/channel-listener-injection-token"; import { channelListenerInjectionToken } from "../channel/channel-listener-injection-token";
import syncBoxStateInjectable from "./sync-box-state.injectable"; import syncBoxStateInjectable from "./sync-box-state.injectable";
const syncBoxChannelListenerInjectable = getInjectable({ const syncBoxChannelListenerInjectable = getInjectable({
id: "sync-box-channel-listener", id: "sync-box-channel-listener",
instantiate: (di) => { instantiate: (di): ChannelListener<SyncBoxChannel> => {
const getSyncBoxState = (id: string) => di.inject(syncBoxStateInjectable, id); const getSyncBoxState = (id: string) => di.inject(syncBoxStateInjectable, id);
return { return {

View File

@ -3,12 +3,15 @@
* 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 { 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 }>;
const syncBoxChannelInjectable = getInjectable({ const syncBoxChannelInjectable = getInjectable({
id: "sync-box-channel", id: "sync-box-channel",
instantiate: () => ({ instantiate: (): SyncBoxChannel => ({
id: "sync-box-channel", id: "sync-box-channel",
}), }),

View File

@ -24,7 +24,7 @@ describe("sync-box", () => {
instantiate: (di) => { instantiate: (di) => {
const createSyncBox = di.inject(createSyncBoxInjectable); const createSyncBox = di.inject(createSyncBoxInjectable);
return createSyncBox("some-state"); return createSyncBox<string>("some-state");
}, },
}); });

View File

@ -13,7 +13,7 @@ const enlistChannelListenerInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const ipcMain = di.inject(ipcMainInjectable); const ipcMain = di.inject(ipcMainInjectable);
return (channel: any, handler: any) => { return (channel, handler) => {
const nativeCallback = (_: IpcMainEvent, message: unknown) => const nativeCallback = (_: IpcMainEvent, message: unknown) =>
handler(message); handler(message);

View File

@ -13,7 +13,7 @@ const enlistChannelListenerInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const ipcRenderer = di.inject(ipcRendererInjectable); const ipcRenderer = di.inject(ipcRendererInjectable);
return (channel: any, handler: any) => { return (channel, handler) => {
const nativeCallback = (_: IpcRendererEvent, message: unknown) => const nativeCallback = (_: IpcRendererEvent, message: unknown) =>
handler(message); handler(message);