Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ipcMainInjectionToken #6950

Merged
merged 2 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/common/ipc/ipc-main-injection-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { IpcMain } from "electron";

const ipcMainInjectionToken = getInjectionToken<IpcMain>({
id: "ipc-main-injection-token",
});

export default ipcMainInjectionToken;
6 changes: 3 additions & 3 deletions src/common/ipc/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import { toJS } from "../utils/toJS";
import type { ClusterFrameInfo } from "../cluster-frames";
import { clusterFrameMap } from "../cluster-frames";
import type { Disposer } from "../utils";
import ipcMainInjectable from "../../main/utils/channel/ipc-main/ipc-main.injectable";
import { getLegacyGlobalDiForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
import ipcRendererInjectable from "../../renderer/utils/channel/ipc-renderer.injectable";
import loggerInjectable from "../logger.injectable";
import ipcMainInjectionToken from "./ipc-main-injection-token";

export const broadcastMainChannel = "ipc:broadcast-main";

export function ipcMainHandle(channel: string, listener: (event: Electron.IpcMainInvokeEvent, ...args: any[]) => any) {
const di = getLegacyGlobalDiForExtensionApi();

const ipcMain = di.inject(ipcMainInjectable);
const ipcMain = di.inject(ipcMainInjectionToken);

ipcMain.handle(channel, async (event, ...args) => {
return sanitizePayload(await listener(event, ...args));
Expand Down Expand Up @@ -90,7 +90,7 @@ export async function broadcastMessage(channel: string, ...args: any[]): Promise
export function ipcMainOn(channel: string, listener: (event: Electron.IpcMainEvent, ...args: any[]) => any): Disposer {
const di = getLegacyGlobalDiForExtensionApi();

const ipcMain = di.inject(ipcMainInjectable);
const ipcMain = di.inject(ipcMainInjectionToken);

ipcMain.on(channel, listener);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
*/
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 { getStartableStoppable } from "../../common/utils/get-startable-stoppable";
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({
id: "catalog-sync-to-renderer",

instantiate: (di) => {
const catalogEntityRegistry = di.inject(catalogEntityRegistryInjectable);
const ipcMain = di.inject(ipcMainInjectable);
const ipcMain = di.inject(ipcMainInjectionToken);
const catalogSyncBroadcaster = di.inject(catalogSyncBroadcasterInjectable);

return getStartableStoppable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import ipcMainInjectionToken from "../../../../common/ipc/ipc-main-injection-token";
import { bundledExtensionsLoaded } from "../../../../common/ipc/extension-handling";
import { delay } from "../../../../common/utils";
import ipcMainInjectable from "../../../utils/channel/ipc-main/ipc-main.injectable";

const waitUntilBundledExtensionsAreLoadedInjectable = getInjectable({
id: "wait-until-bundled-extensions-are-loaded",

instantiate: (di) => {
const ipcMain = di.inject(ipcMainInjectable);
const ipcMain = di.inject(ipcMainInjectionToken);

return async () => {
const viewHasLoaded = new Promise<void>((resolve) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { IpcMainEvent } from "electron";
import ipcMainInjectable from "../ipc-main/ipc-main.injectable";
import { enlistMessageChannelListenerInjectionToken } from "../../../../common/utils/channel/enlist-message-channel-listener-injection-token";
import ipcMainInjectionToken from "../../../../common/ipc/ipc-main-injection-token";

const enlistMessageChannelListenerInjectable = getInjectable({
id: "enlist-message-channel-listener-for-main",

instantiate: (di) => {
const ipcMain = di.inject(ipcMainInjectable);
const ipcMain = di.inject(ipcMainInjectionToken);

return ({ channel, handler }) => {
const nativeOnCallback = (_: IpcMainEvent, message: unknown) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { IpcMainInvokeEvent } from "electron";
import ipcMainInjectable from "../ipc-main/ipc-main.injectable";
import type { Disposer } from "../../../../common/utils";
import type { RequestChannel } from "../../../../common/utils/channel/request-channel-listener-injection-token";
import type { RequestChannelListener } from "./listener-tokens";
import ipcMainInjectionToken from "../../../../common/ipc/ipc-main-injection-token";

export type EnlistRequestChannelListener = <TChannel extends RequestChannel<unknown, unknown>>(listener: RequestChannelListener<TChannel>) => Disposer;

const enlistRequestChannelListenerInjectable = getInjectable({
id: "enlist-request-channel-listener-for-main",

instantiate: (di): EnlistRequestChannelListener => {
const ipcMain = di.inject(ipcMainInjectable);
const ipcMain = di.inject(ipcMainInjectionToken);

return ({ channel, handler }) => {
const nativeHandleCallback = (_: IpcMainInvokeEvent, request: unknown) => handler(request);
Expand Down
2 changes: 2 additions & 0 deletions src/main/utils/channel/ipc-main/ipc-main.injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import { ipcMain } from "electron";
import ipcMainInjectionToken from "../../../../common/ipc/ipc-main-injection-token";

const ipcMainInjectable = getInjectable({
id: "ipc-main",
instantiate: () => ipcMain,
causesSideEffects: true,
injectionToken: ipcMainInjectionToken,
});

export default ipcMainInjectable;