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

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 <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-06-06 15:25:46 -04:00
parent 299bc475a2
commit 2012602e6e
12 changed files with 66 additions and 86 deletions

View File

@ -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<any, any>,
>(listener: RequestChannelHandlerDescriptor<TChannel>) => () => void;
>(listener: RequestChannelHandler<TChannel>) => () => void;
export const enlistRequestChannelListenerInjectionToken =
getInjectionToken<EnlistRequestChannelListener>({

View File

@ -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);
});

View File

@ -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<TChannel extends MessageChannel<any>> {
channel: TChannel;
handler: (value: SetRequired<TChannel, "_messageSignature">["_messageSignature"]) => void;
export type MessageChannelListenerFunction<Channel> = Channel extends MessageChannel<infer Message>
? (message: Message) => void
: never;
export interface MessageChannelListener<Channel> {
channel: Channel;
handler: MessageChannelListenerFunction<Channel>;
}
export const messageChannelListenerInjectionToken = getInjectionToken<MessageChannelListener<MessageChannel<any>>>(
@ -16,3 +20,24 @@ export const messageChannelListenerInjectionToken = getInjectionToken<MessageCha
id: "message-channel-listener",
},
);
export function getMessageChannelListenerInjectable<
ChannelInjectionToken,
Channel = ChannelInjectionToken extends Injectable<infer Channel, MessageChannel<any>, void>
? Channel
: never,
>(
channelInjectionToken: ChannelInjectionToken,
instantiate: (di: DiContainerForInjection) => MessageChannelListenerFunction<Channel>,
) {
const token = channelInjectionToken as unknown as Injectable<MessageChannel<any>, MessageChannel<any>, void>;
return getInjectable({
id: `${token.id}-listener`,
instantiate: (di) => ({
channel: di.inject(token),
handler: instantiate(di),
}),
injectionToken: messageChannelListenerInjectionToken,
});
}

View File

@ -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> = Channel extends RequestChannel<infer Request, infer Response>

View File

@ -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> = Channel extends RequestChannel<infer Request, infer Response>
export type RequestChannelHandlerFunction<Channel> = Channel extends RequestChannel<infer Request, infer Response>
? (request: Request) => Response | Promise<Response>
: never;
export interface RequestChannelHandlerDescriptor<Channel> {
export interface RequestChannelHandler<Channel> {
channel: Channel;
handler: RequestChannelHandler<Channel>;
handler: RequestChannelHandlerFunction<Channel>;
}
export const requestChannelHandlerInjectionToken = getInjectionToken<RequestChannelHandlerDescriptor<RequestChannel<any, any>>>(
export const requestChannelHandlerInjectionToken = getInjectionToken<RequestChannelHandler<RequestChannel<any, any>>>(
{
id: "request-channel-handler",
},
@ -28,7 +28,7 @@ export function getRequestChannelHandlerInjectable<
: never,
>(
channelInjectionToken: ChannelInjectionToken,
instantiate: (di: DiContainerForInjection) => RequestChannelHandler<Channel>,
instantiate: (di: DiContainerForInjection) => RequestChannelHandlerFunction<Channel>,
) {
const token = channelInjectionToken as unknown as Injectable<RequestChannel<any, any>, RequestChannel<any, any>, void>;

View File

@ -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<SyncBoxChannel> => {
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;

View File

@ -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<AppPathsChannel> => {
const channel = di.inject(appPathsChannelInjectable);
const appPaths = di.inject(appPathsInjectable);
return {
channel,
handler: () => appPaths,
};
},
injectionToken: requestChannelHandlerInjectionToken,
return () => appPaths;
});
export default appPathsRequestChannelListenerInjectable;
export default appPathsRequestChannelHandlerInjectable;

View File

@ -30,6 +30,7 @@ const openLocalShellSessionInjectable = getInjectable({
return (args) => new LocalShellSession(deps, args).open();
},
causesSideEffects: true,
});
export default openLocalShellSessionInjectable;

View File

@ -22,6 +22,7 @@ const openNodeShellSessionInjectable = getInjectable({
id: "node-shell-session",
instantiate: (): OpenNodeShellSession => (args) => new NodeShellSession(args).open(),
causesSideEffects: true,
});
export default openNodeShellSessionInjectable;

View File

@ -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;

View File

@ -72,7 +72,6 @@ export class TerminalApi extends WebSocketApi<TerminalEvents> {
tabId: this.query.id,
});
console.log("authTokenArray", authTokenArray);
const { hostname, protocol, port } = location;
const socketUrl = url.format({
protocol: protocol.includes("https") ? "wss" : "ws",

View File

@ -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);
});