mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix injecting side effects by using more injectables
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
94d0e1172d
commit
7d9f1e00e5
@ -1,32 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { reaction } from "mobx";
|
|
||||||
import { broadcastMessage, ipcMainOn } from "../common/ipc";
|
|
||||||
import type { CatalogEntityRegistry } from "./catalog";
|
|
||||||
import "../common/catalog-entities/kubernetes-cluster";
|
|
||||||
import { disposer, toJS } from "../common/utils";
|
|
||||||
import { debounce } from "lodash";
|
|
||||||
import type { CatalogEntity } from "../common/catalog";
|
|
||||||
import { catalogInitChannel, catalogItemsChannel } from "../common/ipc/catalog";
|
|
||||||
|
|
||||||
const broadcaster = debounce((items: CatalogEntity[]) => {
|
|
||||||
broadcastMessage(catalogItemsChannel, items);
|
|
||||||
}, 100, { leading: true, trailing: true });
|
|
||||||
|
|
||||||
export function pushCatalogToRenderer(catalog: CatalogEntityRegistry) {
|
|
||||||
broadcaster(toJS(catalog.items));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function startCatalogSyncToRenderer(catalog: CatalogEntityRegistry) {
|
|
||||||
return disposer(
|
|
||||||
ipcMainOn(catalogInitChannel, () => broadcaster(toJS(catalog.items))),
|
|
||||||
reaction(() => toJS(catalog.items), (items) => {
|
|
||||||
broadcaster(items);
|
|
||||||
}, {
|
|
||||||
fireImmediately: true,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { debounce } from "lodash";
|
||||||
|
import { getGlobalOverride } from "../../common/test-utils/get-global-override";
|
||||||
|
import catalogSyncBroadcasterInjectable from "./broadcaster.injectable";
|
||||||
|
|
||||||
|
export default getGlobalOverride(catalogSyncBroadcasterInjectable, () => debounce(() => {}));
|
||||||
29
src/main/catalog-sync-to-renderer/broadcaster.injectable.ts
Normal file
29
src/main/catalog-sync-to-renderer/broadcaster.injectable.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/**
|
||||||
|
* 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 { debounce } from "lodash";
|
||||||
|
import type { CatalogEntity } from "../../common/catalog";
|
||||||
|
import broadcastMessageInjectable from "../../common/ipc/broadcast-message.injectable";
|
||||||
|
import { catalogItemsChannel } from "../../common/ipc/catalog";
|
||||||
|
|
||||||
|
const catalogSyncBroadcasterInjectable = getInjectable({
|
||||||
|
id: "catalog-sync-broadcaster",
|
||||||
|
instantiate: (di) => {
|
||||||
|
const broadcastMessage = di.inject(broadcastMessageInjectable);
|
||||||
|
|
||||||
|
return debounce(
|
||||||
|
(items: CatalogEntity[]) => {
|
||||||
|
broadcastMessage(catalogItemsChannel, items);
|
||||||
|
},
|
||||||
|
100,
|
||||||
|
{
|
||||||
|
leading: true,
|
||||||
|
trailing: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default catalogSyncBroadcasterInjectable;
|
||||||
@ -3,23 +3,40 @@
|
|||||||
* 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 { startCatalogSyncToRenderer } from "../catalog-pusher";
|
import { reaction } from "mobx";
|
||||||
|
import { catalogInitChannel } from "../../common/ipc/catalog";
|
||||||
|
import { disposer, toJS } from "../../common/utils";
|
||||||
import { getStartableStoppable } from "../../common/utils/get-startable-stoppable";
|
import { getStartableStoppable } from "../../common/utils/get-startable-stoppable";
|
||||||
import catalogEntityRegistryInjectable from "../catalog/entity-registry.injectable";
|
import catalogEntityRegistryInjectable from "../catalog/entity-registry.injectable";
|
||||||
|
import ipcMainInjectable from "../utils/channel/ipc-main/ipc-main.injectable";
|
||||||
|
import catalogSyncBroadcasterInjectable from "./broadcaster.injectable";
|
||||||
|
|
||||||
const catalogSyncToRendererInjectable = getInjectable({
|
const catalogSyncToRendererInjectable = getInjectable({
|
||||||
id: "catalog-sync-to-renderer",
|
id: "catalog-sync-to-renderer",
|
||||||
|
|
||||||
instantiate: (di) => {
|
instantiate: (di) => {
|
||||||
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
|
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
|
||||||
|
const ipcMain = di.inject(ipcMainInjectable);
|
||||||
|
const catalogSyncBroadcaster = di.inject(catalogSyncBroadcasterInjectable);
|
||||||
|
|
||||||
return getStartableStoppable(
|
return getStartableStoppable(
|
||||||
"catalog-sync",
|
"catalog-sync",
|
||||||
() => startCatalogSyncToRenderer(catalogEntityRegistry),
|
() => {
|
||||||
|
const initChannelHandler = () => catalogSyncBroadcaster(toJS(catalogEntityRegistry.items));
|
||||||
|
|
||||||
|
ipcMain.on(catalogInitChannel, initChannelHandler);
|
||||||
|
|
||||||
|
return disposer(
|
||||||
|
() => ipcMain.off(catalogInitChannel, initChannelHandler),
|
||||||
|
reaction(() => toJS(catalogEntityRegistry.items), (items) => {
|
||||||
|
catalogSyncBroadcaster(items);
|
||||||
|
}, {
|
||||||
|
fireImmediately: true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
causesSideEffects: true,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default catalogSyncToRendererInjectable;
|
export default catalogSyncToRendererInjectable;
|
||||||
|
|||||||
@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* 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 { toJS } from "../../common/utils";
|
||||||
|
import catalogEntityRegistryInjectable from "../catalog/entity-registry.injectable";
|
||||||
|
import catalogSyncBroadcasterInjectable from "./broadcaster.injectable";
|
||||||
|
|
||||||
|
const pushCatalogToRendererInjectable = getInjectable({
|
||||||
|
id: "push-catalog-to-renderer",
|
||||||
|
instantiate: (di) => {
|
||||||
|
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
|
||||||
|
const catalogSyncBroadcaster = di.inject(catalogSyncBroadcasterInjectable);
|
||||||
|
|
||||||
|
return () => catalogSyncBroadcaster(toJS(catalogEntityRegistry.items));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default pushCatalogToRendererInjectable;
|
||||||
@ -6,14 +6,19 @@
|
|||||||
import { getGlobalOverride } from "../../common/test-utils/get-global-override";
|
import { getGlobalOverride } from "../../common/test-utils/get-global-override";
|
||||||
import electronAppInjectable from "./electron-app.injectable";
|
import electronAppInjectable from "./electron-app.injectable";
|
||||||
|
|
||||||
export default getGlobalOverride(electronAppInjectable, () => ({
|
export default getGlobalOverride(electronAppInjectable, () => {
|
||||||
getVersion: () => "6.0.0",
|
const app = ({
|
||||||
setLoginItemSettings: () => {},
|
getVersion: () => "6.0.0",
|
||||||
commandLine: {
|
setLoginItemSettings: () => { },
|
||||||
appendArgument: () => {},
|
on: () => app,
|
||||||
appendSwitch: () => {},
|
commandLine: {
|
||||||
getSwitchValue: () => "",
|
appendArgument: () => {},
|
||||||
hasSwitch: () => false,
|
appendSwitch: () => {},
|
||||||
removeSwitch: () => {},
|
getSwitchValue: () => "",
|
||||||
},
|
hasSwitch: () => false,
|
||||||
} as Partial<Electron.App> as Electron.App));
|
removeSwitch: () => {},
|
||||||
|
},
|
||||||
|
} as Partial<Electron.App> as Electron.App);
|
||||||
|
|
||||||
|
return app;
|
||||||
|
});
|
||||||
|
|||||||
@ -7,10 +7,10 @@ import { setupIpcMainHandlers } from "./setup-ipc-main-handlers";
|
|||||||
import loggerInjectable from "../../../../common/logger.injectable";
|
import loggerInjectable from "../../../../common/logger.injectable";
|
||||||
import clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
|
import clusterStoreInjectable from "../../../../common/cluster-store/cluster-store.injectable";
|
||||||
import { onLoadOfApplicationInjectionToken } from "../../../start-main-application/runnable-tokens/on-load-of-application-injection-token";
|
import { onLoadOfApplicationInjectionToken } from "../../../start-main-application/runnable-tokens/on-load-of-application-injection-token";
|
||||||
import catalogEntityRegistryInjectable from "../../../catalog/entity-registry.injectable";
|
|
||||||
import applicationMenuItemCompositeInjectable from "../../../../features/application-menu/main/application-menu-item-composite.injectable";
|
import applicationMenuItemCompositeInjectable from "../../../../features/application-menu/main/application-menu-item-composite.injectable";
|
||||||
import emitAppEventInjectable from "../../../../common/app-event-bus/emit-event.injectable";
|
import emitAppEventInjectable from "../../../../common/app-event-bus/emit-event.injectable";
|
||||||
import getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable";
|
import getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable";
|
||||||
|
import pushCatalogToRendererInjectable from "../../../catalog-sync-to-renderer/push-catalog-to-renderer.injectable";
|
||||||
|
|
||||||
const setupIpcMainHandlersInjectable = getInjectable({
|
const setupIpcMainHandlersInjectable = getInjectable({
|
||||||
id: "setup-ipc-main-handlers",
|
id: "setup-ipc-main-handlers",
|
||||||
@ -18,7 +18,7 @@ const setupIpcMainHandlersInjectable = getInjectable({
|
|||||||
instantiate: (di) => {
|
instantiate: (di) => {
|
||||||
const logger = di.inject(loggerInjectable);
|
const logger = di.inject(loggerInjectable);
|
||||||
const applicationMenuItemComposite = di.inject(applicationMenuItemCompositeInjectable);
|
const applicationMenuItemComposite = di.inject(applicationMenuItemCompositeInjectable);
|
||||||
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
|
const pushCatalogToRenderer = di.inject(pushCatalogToRendererInjectable);
|
||||||
const clusterStore = di.inject(clusterStoreInjectable);
|
const clusterStore = di.inject(clusterStoreInjectable);
|
||||||
const emitAppEvent = di.inject(emitAppEventInjectable);
|
const emitAppEvent = di.inject(emitAppEventInjectable);
|
||||||
const getClusterById = di.inject(getClusterByIdInjectable);
|
const getClusterById = di.inject(getClusterByIdInjectable);
|
||||||
@ -30,7 +30,7 @@ const setupIpcMainHandlersInjectable = getInjectable({
|
|||||||
|
|
||||||
setupIpcMainHandlers({
|
setupIpcMainHandlers({
|
||||||
applicationMenuItemComposite,
|
applicationMenuItemComposite,
|
||||||
catalogEntityRegistry,
|
pushCatalogToRenderer,
|
||||||
clusterStore,
|
clusterStore,
|
||||||
emitAppEvent,
|
emitAppEvent,
|
||||||
getClusterById,
|
getClusterById,
|
||||||
|
|||||||
@ -9,8 +9,6 @@ import { clusterActivateHandler, clusterSetFrameIdHandler, clusterDisconnectHand
|
|||||||
import type { ClusterId } from "../../../../common/cluster-types";
|
import type { ClusterId } from "../../../../common/cluster-types";
|
||||||
import type { ClusterStore } from "../../../../common/cluster-store/cluster-store";
|
import type { ClusterStore } from "../../../../common/cluster-store/cluster-store";
|
||||||
import { broadcastMainChannel, broadcastMessage, ipcMainHandle, ipcMainOn } from "../../../../common/ipc";
|
import { broadcastMainChannel, broadcastMessage, ipcMainHandle, ipcMainOn } from "../../../../common/ipc";
|
||||||
import type { CatalogEntityRegistry } from "../../../catalog";
|
|
||||||
import { pushCatalogToRenderer } from "../../../catalog-pusher";
|
|
||||||
import type { IComputedValue } from "mobx";
|
import type { IComputedValue } from "mobx";
|
||||||
import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppMenuAsContextMenuChannel } from "../../../../common/ipc/window";
|
import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppMenuAsContextMenuChannel } from "../../../../common/ipc/window";
|
||||||
import { handleWindowAction, onLocationChange } from "../../../ipc/window";
|
import { handleWindowAction, onLocationChange } from "../../../ipc/window";
|
||||||
@ -22,18 +20,18 @@ import type { EmitAppEvent } from "../../../../common/app-event-bus/emit-event.i
|
|||||||
import type { GetClusterById } from "../../../../common/cluster-store/get-by-id.injectable";
|
import type { GetClusterById } from "../../../../common/cluster-store/get-by-id.injectable";
|
||||||
interface Dependencies {
|
interface Dependencies {
|
||||||
applicationMenuItemComposite: IComputedValue<Composite<ApplicationMenuItemTypes | MenuItemRoot>>;
|
applicationMenuItemComposite: IComputedValue<Composite<ApplicationMenuItemTypes | MenuItemRoot>>;
|
||||||
catalogEntityRegistry: CatalogEntityRegistry;
|
|
||||||
clusterStore: ClusterStore;
|
clusterStore: ClusterStore;
|
||||||
emitAppEvent: EmitAppEvent;
|
emitAppEvent: EmitAppEvent;
|
||||||
getClusterById: GetClusterById;
|
getClusterById: GetClusterById;
|
||||||
|
pushCatalogToRenderer: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const setupIpcMainHandlers = ({
|
export const setupIpcMainHandlers = ({
|
||||||
applicationMenuItemComposite,
|
applicationMenuItemComposite,
|
||||||
catalogEntityRegistry,
|
|
||||||
clusterStore,
|
clusterStore,
|
||||||
emitAppEvent,
|
emitAppEvent,
|
||||||
getClusterById,
|
getClusterById,
|
||||||
|
pushCatalogToRenderer,
|
||||||
}: Dependencies) => {
|
}: Dependencies) => {
|
||||||
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
|
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
|
||||||
return getClusterById(clusterId)
|
return getClusterById(clusterId)
|
||||||
@ -45,8 +43,7 @@ export const setupIpcMainHandlers = ({
|
|||||||
|
|
||||||
if (cluster) {
|
if (cluster) {
|
||||||
clusterFrameMap.set(cluster.id, { frameId: event.frameId, processId: event.processId });
|
clusterFrameMap.set(cluster.id, { frameId: event.frameId, processId: event.processId });
|
||||||
|
pushCatalogToRenderer();
|
||||||
pushCatalogToRenderer(catalogEntityRegistry);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user