typed ipc invoke and handle

This commit is contained in:
Labhansh Agrawal 2023-06-26 14:52:59 +05:30
parent 2b644e1fbb
commit b90d37bd9c
5 changed files with 49 additions and 12 deletions

View file

@ -1,11 +1,13 @@
import {ipcRenderer} from 'electron';
import electron from 'electron';
import type {IpcRendererWithCommands} from '../../common';
const ipcRenderer = electron.ipcRenderer as IpcRendererWithCommands;
export function exec(command: string, options?: any, callback?: (..._args: any) => void) {
if (typeof options === 'function') {
callback = options;
options = {};
}
ipcRenderer.invoke('child_process.exec', {command, options}).then(
ipcRenderer.invoke('child_process.exec', command, options).then(
({stdout, stderr}) => callback?.(null, stdout, stderr),
(error) => callback?.(error, '', '')
);
@ -25,7 +27,7 @@ export function execFile(file: string, args?: any, options?: any, callback?: (..
args = null;
options = null;
}
ipcRenderer.invoke('child_process.execFile', {file, args, options}).then(
ipcRenderer.invoke('child_process.execFile', file, args, options).then(
({stdout, stderr}) => callback?.(null, stdout, stderr),
(error) => callback?.(error, '', '')
);

View file

@ -46,8 +46,8 @@ export default class Client {
return this;
};
emit<U extends Exclude<keyof MainEvents, FilterNever<MainEvents>>>(ch: U): boolean;
emit<U extends FilterNever<MainEvents>>(ch: U, data: MainEvents[U]): boolean;
emit<U extends Exclude<keyof MainEvents, FilterNever<MainEvents>>>(ev: U): boolean;
emit<U extends FilterNever<MainEvents>>(ev: U, data: MainEvents[U]): boolean;
emit<U extends keyof MainEvents>(ev: U, data?: MainEvents[U]) {
if (!this.id) {
throw new Error('Not ready');