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

Replace last use of customRequestPromise with di

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-03 15:47:28 -04:00
parent 3c9791e81e
commit c3402562a1
20 changed files with 130 additions and 136 deletions

View File

@ -5,7 +5,6 @@
export const windowActionHandleChannel = "window:window-action";
export const windowOpenAppMenuAsContextMenuChannel = "window:open-app-context-menu";
export const windowLocationChangedChannel = "window:location-changed";
/**
* The supported actions on the current window. The argument for `windowActionHandleChannel`

View File

@ -5,8 +5,8 @@
import { getInjectionToken } from "@ogre-tools/injectable";
import type { IComputedValue } from "mobx";
export interface SyncBox<Value> {
id: string;
value: IComputedValue<Value>;
readonly id: string;
readonly value: IComputedValue<Value>;
set: (value: Value) => void;
}

View File

@ -43,7 +43,7 @@ describe("listing active helm repositories in preferences", () => {
builder.beforeApplicationStart((mainDi) => {
mainDi.override(readYamlFileInjectable, () => readYamlFileMock);
mainDi.override(execFileInjectable, () => execFileMock);
mainDi.override(exrequestPublicHelmRepositoriesInjectable
mainDi.override(helmBinaryPathInjectable, () => "some-helm-binary-path");
mainDi.override(loggerInjectable, () => loggerStub);
});

View File

@ -30,7 +30,7 @@ describe("preferences - navigation to kubernetes preferences", () => {
mainDi.override(
getActiveHelmRepositoriesInjectable,
() => async () => ({ callWasSuccessful: true, response: [] }),
);
);requestPublicHelmRepositoriesInjectable;
});
builder.beforeWindowStart((windowDi) => {

View File

@ -0,0 +1,27 @@
/**
* 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 createSyncBoxInjectable from "../../../common/utils/sync-box/create-sync-box.injectable";
import { syncBoxInjectionToken } from "../../../common/utils/sync-box/sync-box-injection-token";
export interface TopBarState {
prevEnabled: boolean;
nextEnabled: boolean;
}
const topBarStateInjectable = getInjectable({
id: "top-bar-state",
instantiate: (di) => {
const createSyncBox = di.inject(createSyncBoxInjectable);
return createSyncBox<TopBarState>("top-bar-state", {
prevEnabled: false,
nextEnabled: false,
});
},
injectionToken: syncBoxInjectionToken,
});
export default topBarStateInjectable;

View File

@ -0,0 +1,11 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { MessageChannel } from "../../../common/utils/channel/message-channel-listener-injection-token";
import type { Location } from "history";
export const windowLocationChangedChannel: MessageChannel<Location> = {
id: "window-location-changed",
};

View File

@ -0,0 +1,28 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getMessageChannelListenerInjectable } from "../../../common/utils/channel/message-channel-listener-injection-token";
import getVisibleWindowsInjectable from "../../../main/start-main-application/lens-window/get-visible-windows.injectable";
import topBarStateInjectable from "../../top-bar/common/state.injectable";
import { windowLocationChangedChannel } from "../common/channel";
const windowLocationHasChangedForTopBarStateHandlerInjectable = getMessageChannelListenerInjectable({
id: "for-top-bar-state",
channel: windowLocationChangedChannel,
handler: (di) => {
const getVisibleWindows = di.inject(getVisibleWindowsInjectable);
const topBarState = di.inject(topBarStateInjectable);
return () => {
const windows = getVisibleWindows();
topBarState.set({
prevEnabled: windows.some(window => window.canGoBack()),
nextEnabled: windows.some(window => window.canGoForward()),
});
};
},
});
export default windowLocationHasChangedForTopBarStateHandlerInjectable;

View File

@ -0,0 +1,21 @@
/**
* 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 { MessageChannelSender } from "../../../common/utils/channel/message-to-channel-injection-token";
import { sendMessageToChannelInjectionToken } from "../../../common/utils/channel/message-to-channel-injection-token";
import { windowLocationChangedChannel } from "../common/channel";
export type EmitWindowLocationChanged = MessageChannelSender<typeof windowLocationChangedChannel>;
const emitWindowLocationChangedInjectable = getInjectable({
id: "emit-window-location-changed",
instantiate: (di): EmitWindowLocationChanged => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
return (location) => sendMessageToChannel(windowLocationChangedChannel, location);
},
});
export default emitWindowLocationChangedInjectable;

View File

@ -10,8 +10,8 @@ import type { ClusterId } from "../../../../common/cluster-types";
import type { ClusterStore } from "../../../../common/cluster-store/cluster-store";
import { broadcastMainChannel, broadcastMessage, ipcMainHandle, ipcMainOn } from "../../../../common/ipc";
import type { IComputedValue } from "mobx";
import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppMenuAsContextMenuChannel } from "../../../../common/ipc/window";
import { handleWindowAction, onLocationChange } from "../../../ipc/window";
import { windowActionHandleChannel, windowOpenAppMenuAsContextMenuChannel } from "../../../../common/ipc/window";
import { handleWindowAction } from "../../../ipc/window";
import type { ApplicationMenuItemTypes } from "../../../../features/application-menu/main/menu-items/application-menu-item-injection-token";
import type { Composite } from "../../../../common/utils/composite/get-composite/get-composite";
import { getApplicationMenuTemplate } from "../../../../features/application-menu/main/populate-application-menu.injectable";
@ -59,8 +59,6 @@ export const setupIpcMainHandlers = ({
ipcMainHandle(windowActionHandleChannel, (event, action) => handleWindowAction(action));
ipcMainOn(windowLocationChangedChannel, () => onLocationChange());
ipcMainHandle(broadcastMainChannel, (event, channel, ...args) => broadcastMessage(channel, ...args));
ipcMainOn(windowOpenAppMenuAsContextMenuChannel, async (event) => {

View File

@ -3,8 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { BrowserWindow, webContents } from "electron";
import { broadcastMessage } from "../../common/ipc";
import { BrowserWindow } from "electron";
import { WindowAction } from "../../common/ipc/window";
export function handleWindowAction(action: WindowAction) {
@ -46,26 +45,3 @@ export function handleWindowAction(action: WindowAction) {
throw new Error(`Attemped window action ${action} is unknown`);
}
}
export function onLocationChange(): void {
const getAllWebContents = webContents.getAllWebContents();
const canGoBack = getAllWebContents.some((webContent) => {
if (webContent.getType() === "window") {
return webContent.canGoBack();
}
return false;
});
const canGoForward = getAllWebContents.some((webContent) => {
if (webContent.getType() === "window") {
return webContent.canGoForward();
}
return false;
});
broadcastMessage("history:can-go-back", canGoBack);
broadcastMessage("history:can-go-forward", canGoForward);
}

View File

@ -7,7 +7,6 @@ import { timingSafeEqual, X509Certificate } from "crypto";
import loggerInjectable from "../../../../common/logger.injectable";
import applicationWindowStateInjectable from "./application-window-state.injectable";
import { BrowserWindow } from "electron";
import type { ElectronWindow } from "./create-lens-window.injectable";
import type { RequireExactlyOne } from "type-fest";
import openLinkInBrowserInjectable from "../../../../common/utils/open-link-in-browser.injectable";
import getAbsolutePathInjectable from "../../../../common/path/get-absolute-path.injectable";
@ -16,6 +15,24 @@ import isLinuxInjectable from "../../../../common/vars/is-linux.injectable";
import applicationInformationToken from "../../../../common/vars/application-information-token";
import pathExistsSyncInjectable from "../../../../common/fs/path-exists-sync.injectable";
import lensProxyCertificateInjectable from "../../../../common/certificate/lens-proxy-certificate.injectable";
import type { ClusterFrameInfo } from "../../../../common/cluster-frames";
export interface SendToViewArgs {
channel: string;
frameInfo?: ClusterFrameInfo;
data?: unknown;
}
export interface ElectronWindow {
show: () => void;
close: () => void;
send: (args: SendToViewArgs) => void;
loadFile: (filePath: string) => Promise<void>;
loadUrl: (url: string) => Promise<void>;
reload: () => void;
canGoBack: () => boolean;
canGoForward: () => boolean;
}
export type ElectronWindowTitleBarStyle = "hiddenInset" | "hidden" | "default" | "customButtonsOnHover";
@ -165,7 +182,6 @@ const createElectronWindowInjectable = getInjectable({
await browserWindow.loadFile(filePath);
},
loadUrl: async (url) => {
logger.info(
`[CREATE-ELECTRON-WINDOW]: Loading content for window "${configuration.id}" from url: ${url}...`,
@ -173,7 +189,8 @@ const createElectronWindowInjectable = getInjectable({
await browserWindow.loadURL(url);
},
canGoBack: () => browserWindow.webContents.canGoBack(),
canGoForward: () => browserWindow.webContents.canGoForward(),
show: () => browserWindow.show(),
close: () => browserWindow.close(),
send: ({ channel, data, frameInfo }) => {
@ -187,7 +204,6 @@ const createElectronWindowInjectable = getInjectable({
browserWindow.webContents.send(channel, data);
}
},
reload: () => {
const wc = browserWindow.webContents;

View File

@ -3,24 +3,8 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { ContentSource, ElectronWindowTitleBarStyle } from "./create-electron-window.injectable";
import type { ContentSource, ElectronWindow, ElectronWindowTitleBarStyle, SendToViewArgs } from "./create-electron-window.injectable";
import createElectronWindowForInjectable from "./create-electron-window.injectable";
import type { ClusterFrameInfo } from "../../../../common/cluster-frames";
export interface ElectronWindow {
show: () => void;
close: () => void;
send: (args: SendToViewArgs) => void;
loadFile: (filePath: string) => Promise<void>;
loadUrl: (url: string) => Promise<void>;
reload: () => void;
}
export interface SendToViewArgs {
channel: string;
frameInfo?: ClusterFrameInfo;
data?: unknown;
}
export interface LensWindow {
id: string;
@ -31,6 +15,8 @@ export interface LensWindow {
isVisible: boolean;
isStarting: boolean;
reload: () => void;
canGoBack: () => boolean;
canGoForward: () => boolean;
}
export interface LensWindowConfiguration {
@ -78,15 +64,14 @@ const createLensWindowInjectable = getInjectable({
return {
id: configuration.id,
get isVisible() {
return windowIsShown;
},
get isStarting() {
return windowIsStarting;
},
canGoBack: () => Boolean(browserWindow?.canGoBack()),
canGoForward: () => Boolean(browserWindow?.canGoForward()),
start: async () => {
if (!browserWindow) {
windowIsStarting = true;

View File

@ -1,33 +0,0 @@
/**
* 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 { action } from "mobx";
import { beforeFrameStartsSecondInjectionToken } from "../../../before-frame-starts/tokens";
import ipcRendererInjectable from "../../../utils/channel/ipc-renderer.injectable";
import topBarStateInjectable from "./state.injectable";
// TODO: replace with a SyncBox
const startTopbarStateSyncInjectable = getInjectable({
id: "start-topbar-state-sync",
instantiate: (di) => ({
id: "start-topbar-state-sync",
run: () => {
const state = di.inject(topBarStateInjectable);
const ipcRenderer = di.inject(ipcRendererInjectable);
ipcRenderer.on("history:can-go-back", action((event, canGoBack: boolean) => {
state.prevEnabled = canGoBack;
}));
ipcRenderer.on("history:can-go-forward", action((event, canGoForward: boolean) => {
state.nextEnabled = canGoForward;
}));
},
}),
injectionToken: beforeFrameStartsSecondInjectionToken,
causesSideEffects: true,
});
export default startTopbarStateSyncInjectable;

View File

@ -1,16 +0,0 @@
/**
* 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 { observable } from "mobx";
const topBarStateInjectable = getInjectable({
id: "top-bar-state",
instantiate: () => observable.object({
prevEnabled: false,
nextEnabled: false,
}),
});
export default topBarStateInjectable;

View File

@ -4,14 +4,14 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import { computed } from "mobx";
import topBarStateInjectable from "../../state.injectable";
import topBarStateInjectable from "../../../../../../features/top-bar/common/state.injectable";
const topBarPrevEnabledInjectable = getInjectable({
id: "top-bar-prev-enabled",
instantiate: (di) => {
const state = di.inject(topBarStateInjectable);
return computed(() => state.prevEnabled);
return computed(() => state.value.get().prevEnabled);
},
});

View File

@ -4,14 +4,14 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import { computed } from "mobx";
import topBarStateInjectable from "../../state.injectable";
import topBarStateInjectable from "../../../../../../features/top-bar/common/state.injectable";
const topBarNextEnabledInjectable = getInjectable({
id: "top-bar-next-enabled",
instantiate: (di) => {
const state = di.inject(topBarStateInjectable);
return computed(() => state.nextEnabled);
return computed(() => state.value.get().nextEnabled);
},
});

View File

@ -11,17 +11,17 @@ import { getDiForUnitTesting } from "../../../getDiForUnitTesting";
import type { DiContainer } from "@ogre-tools/injectable";
import type { DiRender } from "../../test-utils/renderFor";
import { renderFor } from "../../test-utils/renderFor";
import { computed, observable } from "mobx";
import { computed } from "mobx";
import rendererExtensionsInjectable from "../../../../extensions/renderer-extensions.injectable";
import closeWindowInjectable from "./top-bar-items/window-controls/close-window/close-window.injectable";
import goBackInjectable from "./top-bar-items/navigation-to-back/go-back/go-back.injectable";
import maximizeWindowInjectable from "./top-bar-items/window-controls/maximize-window/maximize-window.injectable";
import openAppContextMenuInjectable from "./top-bar-items/context-menu/open-app-context-menu/open-app-context-menu.injectable";
import toggleMaximizeWindowInjectable from "./toggle-maximize-window/toggle-maximize-window.injectable";
import topBarStateInjectable from "./state.injectable";
import platformInjectable from "../../../../common/vars/platform.injectable";
import goForwardInjectable from "./top-bar-items/navigation-to-forward/go-forward/go-forward.injectable";
import currentlyInClusterFrameInjectable from "../../../routes/currently-in-cluster-frame.injectable";
import topBarStateInjectable from "../../../../features/top-bar/common/state.injectable";
describe("<TopBar/>", () => {
let di: DiContainer;
@ -50,10 +50,10 @@ describe("<TopBar/>", () => {
describe("with both previous and next history enabled", () => {
beforeEach(() => {
di.override(topBarStateInjectable, () => observable.object({
di.inject(topBarStateInjectable).set({
prevEnabled: true,
nextEnabled: true,
}));
});
});
it("renders w/o errors", () => {

View File

@ -12,9 +12,7 @@ import { getOverrideFsWithFakes } from "../test-utils/override-fs-with-fakes";
import hostedClusterIdInjectable from "./cluster-frame-context/hosted-cluster-id.injectable";
import { runInAction } from "mobx";
import requestAnimationFrameInjectable from "./components/animate/request-animation-frame.injectable";
import startTopbarStateSyncInjectable from "./components/layout/top-bar/start-state-sync.injectable";
import { registerMobX } from "@ogre-tools/injectable-extension-for-mobx";
import watchHistoryStateInjectable from "./remote-helpers/watch-history-state.injectable";
import legacyOnChannelListenInjectable from "./ipc/legacy-channel-listen.injectable";
import type { GlobalOverride } from "../common/test-utils/get-global-override";
import applicationInformationInjectable from "../common/vars/application-information-injectable";
@ -60,21 +58,11 @@ export const getDiForUnitTesting = (
di.override(globalOverride.injectable, globalOverride.overridingInstantiate);
}
[
startTopbarStateSyncInjectable,
].forEach((injectable) => {
di.override(injectable, () => ({
id: injectable.id,
run: () => {},
}));
});
di.override(hostedClusterIdInjectable, () => undefined);
di.override(legacyOnChannelListenInjectable, () => () => noop);
di.override(requestAnimationFrameInjectable, () => (callback) => callback());
di.override(watchHistoryStateInjectable, () => () => () => {});
di.override(requestFromChannelInjectable, () => () => Promise.resolve(undefined as never));

View File

@ -5,12 +5,11 @@
import { clusterActivateHandler, clusterDisconnectHandler, clusterSetFrameIdHandler, clusterStates } from "../../common/ipc/cluster";
import type { ClusterId, ClusterState } from "../../common/cluster-types";
import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppMenuAsContextMenuChannel, type WindowAction } from "../../common/ipc/window";
import { windowActionHandleChannel, windowOpenAppMenuAsContextMenuChannel, type WindowAction } from "../../common/ipc/window";
import { extensionDiscoveryStateChannel, extensionLoaderFromMainChannel } from "../../common/ipc/extension-handling";
import type { InstalledExtension } from "../../extensions/extension-discovery/extension-discovery";
import type { LensExtensionId } from "../../extensions/lens-extension";
import { toJS } from "../utils";
import type { Location } from "history";
import { getLegacyGlobalDiForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
import ipcRendererInjectable from "../utils/channel/ipc-renderer.injectable";
@ -34,10 +33,6 @@ export function emitOpenAppMenuAsContextMenu(): void {
emitToMain(windowOpenAppMenuAsContextMenuChannel);
}
export function emitWindowLocationChanged(location: Location): void {
emitToMain(windowLocationChangedChannel, location);
}
export function requestWindowAction(type: WindowAction): Promise<void> {
return requestMain(windowActionHandleChannel, type);
}

View File

@ -4,23 +4,22 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import { emitWindowLocationChanged } from "../ipc";
import { reaction } from "mobx";
import observableHistoryInjectable from "../navigation/observable-history.injectable";
import emitWindowLocationChangedInjectable from "../../features/window-location/renderer/emit.injectable";
const watchHistoryStateInjectable = getInjectable({
id: "watch-history-state",
instantiate: (di) => {
const observableHistory = di.inject(observableHistoryInjectable);
const emitWindowLocationChanged = di.inject(emitWindowLocationChangedInjectable);
return () => reaction(
() => observableHistory.location,
emitWindowLocationChanged,
);
},
causesSideEffects: true,
});
export default watchHistoryStateInjectable;