From 2012602e6ebbfd7762aa3ef8d5c68aaa350d551f Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 6 Jun 2022 15:25:46 -0400 Subject: [PATCH] Respond to PR comments - Add helper function to improve types with MessageChannelListeners - Remove some debug printing - Improve some typing about application update status Signed-off-by: Sebastian Malton --- ...equest-channel-listener-injection-token.ts | 4 +-- .../listening-of-channels.injectable.ts | 6 ++-- ...essage-channel-listener-injection-token.ts | 35 ++++++++++++++++--- .../request-channel-injection-token.ts | 9 ++++- ...equest-channel-listener-injection-token.ts | 10 +++--- .../sync-box-channel-listener.injectable.ts | 30 ++++------------ ...ths-request-channel-listener.injectable.ts | 22 +++--------- .../open-local-shell-session.injectable.ts | 1 + .../open-node-shell-session.injectable.ts | 1 + ...me-rendered-channel-listener.injectable.ts | 28 +++------------ src/renderer/api/terminal-api.ts | 1 - ...nitial-values-for-sync-boxes.injectable.ts | 5 --- 12 files changed, 66 insertions(+), 86 deletions(-) diff --git a/src/common/utils/channel/enlist-request-channel-listener-injection-token.ts b/src/common/utils/channel/enlist-request-channel-listener-injection-token.ts index d8807ba4da..47ef619bae 100644 --- a/src/common/utils/channel/enlist-request-channel-listener-injection-token.ts +++ b/src/common/utils/channel/enlist-request-channel-listener-injection-token.ts @@ -4,11 +4,11 @@ */ import { getInjectionToken } from "@ogre-tools/injectable"; import type { RequestChannel } from "./request-channel-injection-token"; -import type { RequestChannelHandlerDescriptor } from "./request-channel-listener-injection-token"; +import type { RequestChannelHandler } from "./request-channel-listener-injection-token"; export type EnlistRequestChannelListener = < TChannel extends RequestChannel, ->(listener: RequestChannelHandlerDescriptor) => () => void; +>(listener: RequestChannelHandler) => () => void; export const enlistRequestChannelListenerInjectionToken = getInjectionToken({ diff --git a/src/common/utils/channel/listening-of-channels.injectable.ts b/src/common/utils/channel/listening-of-channels.injectable.ts index 1452a4ebb2..4884ef9ee7 100644 --- a/src/common/utils/channel/listening-of-channels.injectable.ts +++ b/src/common/utils/channel/listening-of-channels.injectable.ts @@ -15,13 +15,13 @@ const listeningOfChannelsInjectable = getInjectable({ instantiate: (di) => { const enlistMessageChannelListener = di.inject(enlistMessageChannelListenerInjectionToken); - const enlistRequestChannelListener = di.inject(enlistRequestChannelListenerInjectionToken); + const enlistRequestChannelHandler = di.inject(enlistRequestChannelListenerInjectionToken); const messageChannelListeners = di.injectMany(messageChannelListenerInjectionToken); - const requestChannelListeners = di.injectMany(requestChannelHandlerInjectionToken); + const requestChannelHandlers = di.injectMany(requestChannelHandlerInjectionToken); return getStartableStoppable("listening-of-channels", () => { const messageChannelDisposers = messageChannelListeners.map(enlistMessageChannelListener); - const requestChannelDisposers = requestChannelListeners.map(enlistRequestChannelListener); + const requestChannelDisposers = requestChannelHandlers.map(enlistRequestChannelHandler); return disposer(...messageChannelDisposers, ...requestChannelDisposers); }); diff --git a/src/common/utils/channel/message-channel-listener-injection-token.ts b/src/common/utils/channel/message-channel-listener-injection-token.ts index 8879e19013..09a54fb9d1 100644 --- a/src/common/utils/channel/message-channel-listener-injection-token.ts +++ b/src/common/utils/channel/message-channel-listener-injection-token.ts @@ -2,13 +2,17 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { getInjectionToken } from "@ogre-tools/injectable"; -import type { SetRequired } from "type-fest"; +import type { DiContainerForInjection, Injectable } from "@ogre-tools/injectable"; +import { getInjectable, getInjectionToken } from "@ogre-tools/injectable"; import type { MessageChannel } from "./message-channel-injection-token"; -export interface MessageChannelListener> { - channel: TChannel; - handler: (value: SetRequired["_messageSignature"]) => void; +export type MessageChannelListenerFunction = Channel extends MessageChannel + ? (message: Message) => void + : never; + +export interface MessageChannelListener { + channel: Channel; + handler: MessageChannelListenerFunction; } export const messageChannelListenerInjectionToken = getInjectionToken>>( @@ -16,3 +20,24 @@ export const messageChannelListenerInjectionToken = getInjectionToken, void> + ? Channel + : never, +>( + channelInjectionToken: ChannelInjectionToken, + instantiate: (di: DiContainerForInjection) => MessageChannelListenerFunction, +) { + const token = channelInjectionToken as unknown as Injectable, MessageChannel, void>; + + return getInjectable({ + id: `${token.id}-listener`, + instantiate: (di) => ({ + channel: di.inject(token), + handler: instantiate(di), + }), + injectionToken: messageChannelListenerInjectionToken, + }); +} diff --git a/src/common/utils/channel/request-channel-injection-token.ts b/src/common/utils/channel/request-channel-injection-token.ts index b9953ea081..c664caa852 100644 --- a/src/common/utils/channel/request-channel-injection-token.ts +++ b/src/common/utils/channel/request-channel-injection-token.ts @@ -6,11 +6,18 @@ import { getInjectionToken } from "@ogre-tools/injectable"; import type { IpcValue } from "./allowed-types"; +/** + * Both of these type parameters are technically unused. Which is fine because we only really + * care about them at type check time since they are here to guide what types other functions + * require (which in turn are just passed on). + */ export interface RequestChannel< + // eslint-disable-next-line unused-imports/no-unused-vars-ts Request extends IpcValue | void = void, + // eslint-disable-next-line unused-imports/no-unused-vars-ts Response extends IpcValue | void = void, > { - id: Request | Response extends boolean ? string : string; + id: string; } export type ChannelRequest = Channel extends RequestChannel diff --git a/src/common/utils/channel/request-channel-listener-injection-token.ts b/src/common/utils/channel/request-channel-listener-injection-token.ts index 88c81fe5b5..d0da656c1f 100644 --- a/src/common/utils/channel/request-channel-listener-injection-token.ts +++ b/src/common/utils/channel/request-channel-listener-injection-token.ts @@ -6,16 +6,16 @@ import type { DiContainerForInjection, Injectable } from "@ogre-tools/injectable import { getInjectable, getInjectionToken } from "@ogre-tools/injectable"; import type { RequestChannel } from "./request-channel-injection-token"; -export type RequestChannelHandler = Channel extends RequestChannel +export type RequestChannelHandlerFunction = Channel extends RequestChannel ? (request: Request) => Response | Promise : never; -export interface RequestChannelHandlerDescriptor { +export interface RequestChannelHandler { channel: Channel; - handler: RequestChannelHandler; + handler: RequestChannelHandlerFunction; } -export const requestChannelHandlerInjectionToken = getInjectionToken>>( +export const requestChannelHandlerInjectionToken = getInjectionToken>>( { id: "request-channel-handler", }, @@ -28,7 +28,7 @@ export function getRequestChannelHandlerInjectable< : never, >( channelInjectionToken: ChannelInjectionToken, - instantiate: (di: DiContainerForInjection) => RequestChannelHandler, + instantiate: (di: DiContainerForInjection) => RequestChannelHandlerFunction, ) { const token = channelInjectionToken as unknown as Injectable, RequestChannel, void>; diff --git a/src/common/utils/sync-box/sync-box-channel-listener.injectable.ts b/src/common/utils/sync-box/sync-box-channel-listener.injectable.ts index b603c85997..df9bf23357 100644 --- a/src/common/utils/sync-box/sync-box-channel-listener.injectable.ts +++ b/src/common/utils/sync-box/sync-box-channel-listener.injectable.ts @@ -2,34 +2,16 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { getInjectable } from "@ogre-tools/injectable"; -import type { SyncBoxChannel } from "./sync-box-channel.injectable"; import syncBoxChannelInjectable from "./sync-box-channel.injectable"; import syncBoxStateInjectable from "./sync-box-state.injectable"; -import type { MessageChannelListener } from "../channel/message-channel-listener-injection-token"; -import { messageChannelListenerInjectionToken } from "../channel/message-channel-listener-injection-token"; +import { getMessageChannelListenerInjectable } from "../channel/message-channel-listener-injection-token"; -const syncBoxChannelListenerInjectable = getInjectable({ - id: "sync-box-channel-listener", +const syncBoxChannelListenerInjectable = getMessageChannelListenerInjectable(syncBoxChannelInjectable, (di) => { + return ({ id, value }) => { + const syncBoxState = di.inject(syncBoxStateInjectable, id); - instantiate: (di): MessageChannelListener => { - const getSyncBoxState = (id: string) => di.inject(syncBoxStateInjectable, id); - const channel = di.inject(syncBoxChannelInjectable); - - return { - channel, - - handler: ({ id, value }) => { - const target = getSyncBoxState(id); - - if (target) { - target.set(value); - } - }, - }; - }, - - injectionToken: messageChannelListenerInjectionToken, + syncBoxState.set(value); + }; }); export default syncBoxChannelListenerInjectable; diff --git a/src/main/app-paths/app-paths-request-channel-listener.injectable.ts b/src/main/app-paths/app-paths-request-channel-listener.injectable.ts index 4f44a0656d..0dc29ce24a 100644 --- a/src/main/app-paths/app-paths-request-channel-listener.injectable.ts +++ b/src/main/app-paths/app-paths-request-channel-listener.injectable.ts @@ -2,26 +2,14 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { getInjectable } from "@ogre-tools/injectable"; -import type { RequestChannelHandlerDescriptor } from "../../common/utils/channel/request-channel-listener-injection-token"; -import { requestChannelHandlerInjectionToken } from "../../common/utils/channel/request-channel-listener-injection-token"; -import type { AppPathsChannel } from "../../common/app-paths/app-paths-channel.injectable"; +import { getRequestChannelHandlerInjectable } from "../../common/utils/channel/request-channel-listener-injection-token"; import appPathsChannelInjectable from "../../common/app-paths/app-paths-channel.injectable"; import appPathsInjectable from "../../common/app-paths/app-paths.injectable"; -const appPathsRequestChannelListenerInjectable = getInjectable({ - id: "app-paths-request-channel-listener", +const appPathsRequestChannelHandlerInjectable = getRequestChannelHandlerInjectable(appPathsChannelInjectable, (di) => { + const appPaths = di.inject(appPathsInjectable); - instantiate: (di): RequestChannelHandlerDescriptor => { - const channel = di.inject(appPathsChannelInjectable); - const appPaths = di.inject(appPathsInjectable); - - return { - channel, - handler: () => appPaths, - }; - }, - injectionToken: requestChannelHandlerInjectionToken, + return () => appPaths; }); -export default appPathsRequestChannelListenerInjectable; +export default appPathsRequestChannelHandlerInjectable; diff --git a/src/main/shell-session/local-shell-session/open-local-shell-session.injectable.ts b/src/main/shell-session/local-shell-session/open-local-shell-session.injectable.ts index 5794cbc3d6..2456e6e139 100644 --- a/src/main/shell-session/local-shell-session/open-local-shell-session.injectable.ts +++ b/src/main/shell-session/local-shell-session/open-local-shell-session.injectable.ts @@ -30,6 +30,7 @@ const openLocalShellSessionInjectable = getInjectable({ return (args) => new LocalShellSession(deps, args).open(); }, + causesSideEffects: true, }); export default openLocalShellSessionInjectable; diff --git a/src/main/shell-session/node-shell-session/open-node-shell-session.injectable.ts b/src/main/shell-session/node-shell-session/open-node-shell-session.injectable.ts index 1281b8516c..3f32bacc30 100644 --- a/src/main/shell-session/node-shell-session/open-node-shell-session.injectable.ts +++ b/src/main/shell-session/node-shell-session/open-node-shell-session.injectable.ts @@ -22,6 +22,7 @@ const openNodeShellSessionInjectable = getInjectable({ id: "node-shell-session", instantiate: (): OpenNodeShellSession => (args) => new NodeShellSession(args).open(), + causesSideEffects: true, }); export default openNodeShellSessionInjectable; diff --git a/src/main/start-main-application/runnables/root-frame-rendered-channel-listener/root-frame-rendered-channel-listener.injectable.ts b/src/main/start-main-application/runnables/root-frame-rendered-channel-listener/root-frame-rendered-channel-listener.injectable.ts index 83ebf7bf91..ca664285f1 100644 --- a/src/main/start-main-application/runnables/root-frame-rendered-channel-listener/root-frame-rendered-channel-listener.injectable.ts +++ b/src/main/start-main-application/runnables/root-frame-rendered-channel-listener/root-frame-rendered-channel-listener.injectable.ts @@ -2,34 +2,16 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { getInjectable } from "@ogre-tools/injectable"; import rootFrameRenderedChannelInjectable from "../../../../common/root-frame-rendered-channel/root-frame-rendered-channel.injectable"; import { runManyFor } from "../../../../common/runnable/run-many-for"; import { afterRootFrameIsReadyInjectionToken } from "../../runnable-tokens/after-root-frame-is-ready-injection-token"; -import { messageChannelListenerInjectionToken } from "../../../../common/utils/channel/message-channel-listener-injection-token"; +import { getMessageChannelListenerInjectable } from "../../../../common/utils/channel/message-channel-listener-injection-token"; -const rootFrameRenderedChannelListenerInjectable = getInjectable({ - id: "root-frame-rendered-channel-listener", +const rootFrameRenderedChannelListenerInjectable = getMessageChannelListenerInjectable(rootFrameRenderedChannelInjectable, (di) => { + const runMany = runManyFor(di); + const runRunnablesAfterRootFrameIsReady = runMany(afterRootFrameIsReadyInjectionToken); - instantiate: (di) => { - const channel = di.inject(rootFrameRenderedChannelInjectable); - - const runMany = runManyFor(di); - - const runRunnablesAfterRootFrameIsReady = runMany( - afterRootFrameIsReadyInjectionToken, - ); - - return { - channel, - - handler: async () => { - await runRunnablesAfterRootFrameIsReady(); - }, - }; - }, - - injectionToken: messageChannelListenerInjectionToken, + return () => runRunnablesAfterRootFrameIsReady(); }); export default rootFrameRenderedChannelListenerInjectable; diff --git a/src/renderer/api/terminal-api.ts b/src/renderer/api/terminal-api.ts index 855ce1503f..73acd4e218 100644 --- a/src/renderer/api/terminal-api.ts +++ b/src/renderer/api/terminal-api.ts @@ -72,7 +72,6 @@ export class TerminalApi extends WebSocketApi { tabId: this.query.id, }); - console.log("authTokenArray", authTokenArray); const { hostname, protocol, port } = location; const socketUrl = url.format({ protocol: protocol.includes("https") ? "wss" : "ws", diff --git a/src/renderer/utils/sync-box/provide-initial-values-for-sync-boxes.injectable.ts b/src/renderer/utils/sync-box/provide-initial-values-for-sync-boxes.injectable.ts index 00899bb7d5..d49ea9dd9c 100644 --- a/src/renderer/utils/sync-box/provide-initial-values-for-sync-boxes.injectable.ts +++ b/src/renderer/utils/sync-box/provide-initial-values-for-sync-boxes.injectable.ts @@ -20,11 +20,6 @@ const provideInitialValuesForSyncBoxesInjectable = getInjectable({ run: async () => { const initialValues = await requestFromChannel(syncBoxInitialValueChannel); - console.log({ - initialValues, - type: typeof initialValues, - }); - initialValues.forEach(({ id, value }) => { setSyncBoxState(id, value); });