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

Convert catalog entity registry broadcast to injectable IPC

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-02-17 10:06:24 -05:00
parent fe86b79adb
commit a1592ce2a5
9 changed files with 68 additions and 56 deletions

View File

@ -8,11 +8,6 @@
*/
export const catalogEntityRunListener = "catalog-entity:run";
/**
* This is broadcast on whenever there is an update to any catalog item
*/
export const catalogItemsChannel = "catalog:items";
/**
* This can be sent from renderer to main to initialize a broadcast of ITEMS
*/

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 { CatalogEntityData, CatalogEntityKindData } from "../../../../common/catalog";
import type { MessageChannel } from "../../../../common/utils/channel/message-channel-listener-injection-token";
export const currentCatalogEntityRegistryStateChannel: MessageChannel<(CatalogEntityData & CatalogEntityKindData)[]> = {
id: "current-catalog-entity-registry-state",
};

View File

@ -0,0 +1,30 @@
/**
* 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 { MessageChannelSender } from "../../../../common/utils/channel/message-to-channel-injection-token";
import { sendMessageToChannelInjectionToken } from "../../../../common/utils/channel/message-to-channel-injection-token";
import { currentCatalogEntityRegistryStateChannel } from "../common/channel";
export type BroadcastCurrentCatalogEntityRegistryState = MessageChannelSender<typeof currentCatalogEntityRegistryStateChannel>;
const broadcastCurrentCatalogEntityRegistryStateInjectable = getInjectable({
id: "broadcast-current-catalog-entity-registry-state",
instantiate: (di) => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
const handler: BroadcastCurrentCatalogEntityRegistryState = (items) => sendMessageToChannel(currentCatalogEntityRegistryStateChannel, items);
return debounce(
handler,
100,
{
leading: true,
trailing: true,
},
);
},
});
export default broadcastCurrentCatalogEntityRegistryStateInjectable;

View File

@ -0,0 +1,19 @@
/**
* 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 catalogEntityRegistryInjectable from "../../../../renderer/api/catalog/entity/registry.injectable";
import { currentCatalogEntityRegistryStateChannel } from "../common/channel";
const currentCatalogEntityRegistryStateListenerInjectable = getMessageChannelListenerInjectable({
channel: currentCatalogEntityRegistryStateChannel,
id: "main",
handler: (di) => {
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
return (items) => catalogEntityRegistry.updateItems(items);
},
});
export default currentCatalogEntityRegistryStateListenerInjectable;

View File

@ -1,10 +0,0 @@
/**
* 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(() => {}));

View File

@ -1,29 +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 { 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;

View File

@ -9,7 +9,7 @@ import { catalogInitChannel } from "../../common/ipc/catalog";
import { disposer, toJS } from "../../common/utils";
import { getStartableStoppable } from "../../common/utils/get-startable-stoppable";
import catalogEntityRegistryInjectable from "../catalog/entity-registry.injectable";
import catalogSyncBroadcasterInjectable from "./broadcaster.injectable";
import broadcastCurrentCatalogEntityRegistryStateInjectable from "../../features/catalog/entities-sync/main/broadcast.injectable";
const catalogSyncToRendererInjectable = getInjectable({
id: "catalog-sync-to-renderer",
@ -17,19 +17,19 @@ const catalogSyncToRendererInjectable = getInjectable({
instantiate: (di) => {
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
const ipcMain = di.inject(ipcMainInjectionToken);
const catalogSyncBroadcaster = di.inject(catalogSyncBroadcasterInjectable);
const broadcastCurrentCatalogEntityRegistryState = di.inject(broadcastCurrentCatalogEntityRegistryStateInjectable);
return getStartableStoppable(
"catalog-sync",
() => {
const initChannelHandler = () => catalogSyncBroadcaster(toJS(catalogEntityRegistry.items));
const initChannelHandler = () => broadcastCurrentCatalogEntityRegistryState(toJS(catalogEntityRegistry.items));
ipcMain.on(catalogInitChannel, initChannelHandler);
return disposer(
() => ipcMain.off(catalogInitChannel, initChannelHandler),
reaction(() => toJS(catalogEntityRegistry.items), (items) => {
catalogSyncBroadcaster(items);
broadcastCurrentCatalogEntityRegistryState(items);
}, {
fireImmediately: true,
}),

View File

@ -4,16 +4,16 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import { toJS } from "../../common/utils";
import broadcastCurrentCatalogEntityRegistryStateInjectable from "../../features/catalog/entities-sync/main/broadcast.injectable";
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);
const broadcastCurrentCatalogEntityRegistryState = di.inject(broadcastCurrentCatalogEntityRegistryStateInjectable);
return () => catalogSyncBroadcaster(toJS(catalogEntityRegistry.items));
return () => broadcastCurrentCatalogEntityRegistryState(toJS(catalogEntityRegistry.items));
},
});

View File

@ -12,7 +12,7 @@ import type { Disposer } from "../../../utils";
import { once } from "lodash";
import { CatalogRunEvent } from "../../../../common/catalog/catalog-run-event";
import { ipcRenderer } from "electron";
import { catalogInitChannel, catalogItemsChannel, catalogEntityRunListener } from "../../../../common/ipc/catalog";
import { catalogInitChannel, catalogEntityRunListener } from "../../../../common/ipc/catalog";
import { isMainFrame } from "process";
import type { Navigate } from "../../../navigation/navigate.injectable";
import type { Logger } from "../../../../common/logger";
@ -83,10 +83,6 @@ export class CatalogEntityRegistry {
}
init() {
ipcRendererOn(catalogItemsChannel, (event, items: (CatalogEntityData & CatalogEntityKindData)[]) => {
this.updateItems(items);
});
// Make sure that we get items ASAP and not the next time one of them changes
ipcRenderer.send(catalogInitChannel);