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

Convert request catalog entity reg state to injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-02-17 10:18:42 -05:00
parent a1592ce2a5
commit b0e2660dfa
6 changed files with 58 additions and 28 deletions

View File

@ -7,8 +7,3 @@
* This is used to activate a specific entity in the renderer main frame
*/
export const catalogEntityRunListener = "catalog-entity:run";
/**
* This can be sent from renderer to main to initialize a broadcast of ITEMS
*/
export const catalogInitChannel = "catalog:init";

View File

@ -9,3 +9,7 @@ import type { MessageChannel } from "../../../../common/utils/channel/message-ch
export const currentCatalogEntityRegistryStateChannel: MessageChannel<(CatalogEntityData & CatalogEntityKindData)[]> = {
id: "current-catalog-entity-registry-state",
};
export const requestCatalogEntityRegistryStateToBeSentChannel: MessageChannel<void> = {
id: "request-catalog-entity-registry-state-to-be-sent",
};

View File

@ -0,0 +1,22 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { toJS } from "../../../../common/utils";
import { getMessageChannelListenerInjectable } from "../../../../common/utils/channel/message-channel-listener-injection-token";
import catalogEntityRegistryInjectable from "../../../../main/catalog/entity-registry.injectable";
import { requestCatalogEntityRegistryStateToBeSentChannel } from "../common/channel";
import broadcastCurrentCatalogEntityRegistryStateInjectable from "./broadcast.injectable";
const requestForCurrentCatalogEntityStateToBeSentListenerInjectable = getMessageChannelListenerInjectable({
id: "main",
channel: requestCatalogEntityRegistryStateToBeSentChannel,
handler: (di) => {
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
const broadcastCurrentCatalogEntityRegistryState = di.inject(broadcastCurrentCatalogEntityRegistryStateInjectable);
return () => broadcastCurrentCatalogEntityRegistryState(toJS(catalogEntityRegistry.items));
},
});
export default requestForCurrentCatalogEntityStateToBeSentListenerInjectable;

View File

@ -0,0 +1,25 @@
/**
* 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 { sendMessageToChannelInjectionToken } from "../../../../common/utils/channel/message-to-channel-injection-token";
import { beforeFrameStartsSecondInjectionToken } from "../../../../renderer/before-frame-starts/tokens";
import { requestCatalogEntityRegistryStateToBeSentChannel } from "../common/channel";
import startListeningOfChannelsInjectable from "../../../../renderer/utils/channel/channel-listeners/start-listening-of-channels.injectable";
const requestCatalogEntityRegistryStateToBeSentInjectable = getInjectable({
id: "request-catalog-entity-registry-state-to-be-sent",
instantiate: (di) => ({
id: "request-catalog-entity-registry-state-to-be-sent",
run: () => {
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
sendMessageToChannel(requestCatalogEntityRegistryStateToBeSentChannel);
},
runAfter: di.inject(startListeningOfChannelsInjectable),
}),
injectionToken: beforeFrameStartsSecondInjectionToken,
});
export default requestCatalogEntityRegistryStateToBeSentInjectable;

View File

@ -4,9 +4,7 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import { reaction } from "mobx";
import ipcMainInjectionToken from "../../common/ipc/ipc-main-injection-token";
import { catalogInitChannel } from "../../common/ipc/catalog";
import { disposer, toJS } from "../../common/utils";
import { toJS } from "../../common/utils";
import { getStartableStoppable } from "../../common/utils/get-startable-stoppable";
import catalogEntityRegistryInjectable from "../catalog/entity-registry.injectable";
import broadcastCurrentCatalogEntityRegistryStateInjectable from "../../features/catalog/entities-sync/main/broadcast.injectable";
@ -16,25 +14,15 @@ const catalogSyncToRendererInjectable = getInjectable({
instantiate: (di) => {
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
const ipcMain = di.inject(ipcMainInjectionToken);
const broadcastCurrentCatalogEntityRegistryState = di.inject(broadcastCurrentCatalogEntityRegistryStateInjectable);
return getStartableStoppable(
"catalog-sync",
() => {
const initChannelHandler = () => broadcastCurrentCatalogEntityRegistryState(toJS(catalogEntityRegistry.items));
ipcMain.on(catalogInitChannel, initChannelHandler);
return disposer(
() => ipcMain.off(catalogInitChannel, initChannelHandler),
reaction(() => toJS(catalogEntityRegistry.items), (items) => {
broadcastCurrentCatalogEntityRegistryState(items);
}, {
fireImmediately: true,
}),
);
},
() => reaction(() => toJS(catalogEntityRegistry.items), (items) => {
broadcastCurrentCatalogEntityRegistryState(items);
}, {
fireImmediately: true,
}),
);
},
});

View File

@ -11,8 +11,7 @@ import { iter } from "../../../utils";
import type { Disposer } from "../../../utils";
import { once } from "lodash";
import { CatalogRunEvent } from "../../../../common/catalog/catalog-run-event";
import { ipcRenderer } from "electron";
import { catalogInitChannel, catalogEntityRunListener } from "../../../../common/ipc/catalog";
import { catalogEntityRunListener } from "../../../../common/ipc/catalog";
import { isMainFrame } from "process";
import type { Navigate } from "../../../navigation/navigate.injectable";
import type { Logger } from "../../../../common/logger";
@ -83,9 +82,6 @@ export class CatalogEntityRegistry {
}
init() {
// Make sure that we get items ASAP and not the next time one of them changes
ipcRenderer.send(catalogInitChannel);
if (isMainFrame) {
ipcRendererOn(catalogEntityRunListener, (event, id: string) => {
const entity = this.getById(id);