Skip to content

Commit

Permalink
feat: webcontentsview extension ipcpromise
Browse files Browse the repository at this point in the history
  • Loading branch information
Venipa committed Nov 25, 2024
1 parent ce691cb commit 618ee75
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
13 changes: 12 additions & 1 deletion src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/// <reference types="vite/client" />
/// <reference types="@modyfi/vite-plugin-yaml/modules" />
/// <reference types="electron-vite/node" />
/// <reference path="../node_modules/electron/electron.d.ts" />

import type { IpcPromiseResult } from "@shared/utils/promises";

interface ImportMetaEnv {
readonly MAIN_APP_SECRET: string;
Expand All @@ -10,4 +13,12 @@ interface ImportMeta {
readonly env: ImportMetaEnv;
}

type StringLiteral<KnownValues extends string> = (string & {}) | KnownValues;
declare global {
declare namespace Electron {
interface WebContentsView {
invoke<T = any>(channelName: string, data: any): IpcPromiseResult<T>;
}
}
}

type StringLiteral<KnownValues extends string> = (string & {}) | KnownValues;
7 changes: 5 additions & 2 deletions src/main/utils/electron.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<T>(channel: string, data: any) {
return ipcPromise<T>(this, channel, data);
}
isInitialized = true;
}
11 changes: 5 additions & 6 deletions src/main/utils/mappedWindow.ts
Original file line number Diff line number Diff line change
@@ -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">;

Check failure on line 4 in src/main/utils/mappedWindow.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.18.0)

Cannot find name 'StringLiteral'. Did you mean 'StringIterator'?
type TrackControlFn = <T = any>(type: TrackControlTypes) => Promise<T>;
export interface BrowserWindowViews<T, TView extends WebContentsView = WebContentsView> {
main: BrowserWindow;
views: { [key: string]: TView } & T;
sendToAllViews(ev: string, ...args: any[]): void;
sendTrackControl: TrackControlFn
sendTrackControl: TrackControlFn;
}

export function getViewObject(bwv: { [key: string]: WebContentsView }) {
Expand All @@ -27,9 +26,9 @@ export function createWindowContext<T, TView extends WebContentsView = WebConten
views: { [key: string]: TView } & T = _data.views || ({} as any);
async sendTrackControl<T = any>(type: TrackControlTypes) {
const view = this.views.youtubeView;
if (!view) return Promise.reject(new Error("View not found"))
return await ipcPromise<T>(view, IPC_EVENT_NAMES.TRACK_CONTROL, {type})
};
if (!view) return Promise.reject(new Error("View not found"));
return await view.invoke<T>(IPC_EVENT_NAMES.TRACK_CONTROL, { type });
}
sendToAllViews(ev: string, ...args: any[]): void {
return (Object.values(this.views) as TView[])
.filter(
Expand Down
3 changes: 2 additions & 1 deletion src/shared/utils/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export async function ipcPromise<T = any, R = T>(view: WebContentsView, channel:
reject(new Error("IPC Promise timeout."))
}, 10000);
})
}
}
export type IpcPromiseResult<T> = ReturnType<typeof ipcPromise<T>>;

0 comments on commit 618ee75

Please sign in to comment.