Convert catalog entity registry broadcast to injectable IPC

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2023-03-01 13:34:46 -05:00
1 parent fe86b79adb
commit a1592ce2a5
9 files changed
+68 -56

No files matched your search

@@ -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(() => {}));
@@ -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;
@@ -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,
}),
@@ -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));
},
});