diff --git a/src/env.d.ts b/src/env.d.ts index 5eb8455..b97a928 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -1,6 +1,9 @@ /// /// /// +/// + +import type { IpcPromiseResult } from "@shared/utils/promises"; interface ImportMetaEnv { readonly MAIN_APP_SECRET: string; @@ -10,4 +13,12 @@ interface ImportMeta { readonly env: ImportMetaEnv; } -type StringLiteral = (string & {}) | KnownValues; \ No newline at end of file +declare global { + declare namespace Electron { + interface WebContentsView { + invoke(channelName: string, data: any): IpcPromiseResult; + } + } +} + +type StringLiteral = (string & {}) | KnownValues; diff --git a/src/main/utils/electron.ts b/src/main/utils/electron.ts index 5dad185..60748b1 100644 --- a/src/main/utils/electron.ts +++ b/src/main/utils/electron.ts @@ -1,7 +1,8 @@ import { is } from "@electron-toolkit/utils"; import { Logger } from "@shared/utils/console"; import logger from "@shared/utils/Logger"; -import { app } from "electron"; +import { ipcPromise } from "@shared/utils/promises"; +import { app, WebContentsView } from "electron"; import { isProduction } from "./devUtils"; let isInitialized = false; @@ -21,6 +22,8 @@ export function initializeCustomElectronEnvironment() { if (import.meta.env.PROD && !process.env.DEBUG) Logger.enableProductionMode(); process.env.NODE_ENV = import.meta.env.MODE; - + WebContentsView.prototype.invoke = function(channel: string, data: any) { + return ipcPromise(this, channel, data); + } isInitialized = true; } diff --git a/src/main/utils/mappedWindow.ts b/src/main/utils/mappedWindow.ts index 1dd5594..dfe2985 100644 --- a/src/main/utils/mappedWindow.ts +++ b/src/main/utils/mappedWindow.ts @@ -1,14 +1,13 @@ -import { ipcPromise } from "@shared/utils/promises"; import { BrowserWindow, WebContentsView } from "electron"; import IPC_EVENT_NAMES from "./eventNames"; -type TrackControlTypes = StringLiteral<("play" | "pause" | "next" | "prev" | "toggle")> +type TrackControlTypes = StringLiteral<"play" | "pause" | "next" | "prev" | "toggle">; type TrackControlFn = (type: TrackControlTypes) => Promise; export interface BrowserWindowViews { main: BrowserWindow; views: { [key: string]: TView } & T; sendToAllViews(ev: string, ...args: any[]): void; - sendTrackControl: TrackControlFn + sendTrackControl: TrackControlFn; } export function getViewObject(bwv: { [key: string]: WebContentsView }) { @@ -27,9 +26,9 @@ export function createWindowContext(type: TrackControlTypes) { const view = this.views.youtubeView; - if (!view) return Promise.reject(new Error("View not found")) - return await ipcPromise(view, IPC_EVENT_NAMES.TRACK_CONTROL, {type}) - }; + if (!view) return Promise.reject(new Error("View not found")); + return await view.invoke(IPC_EVENT_NAMES.TRACK_CONTROL, { type }); + } sendToAllViews(ev: string, ...args: any[]): void { return (Object.values(this.views) as TView[]) .filter( diff --git a/src/shared/utils/promises.ts b/src/shared/utils/promises.ts index 47ccf90..2a0daa4 100644 --- a/src/shared/utils/promises.ts +++ b/src/shared/utils/promises.ts @@ -26,4 +26,5 @@ export async function ipcPromise(view: WebContentsView, channel: reject(new Error("IPC Promise timeout.")) }, 10000); }) -} \ No newline at end of file +} +export type IpcPromiseResult = ReturnType>; \ No newline at end of file