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

[question] is there a relatively good Typescript encapsulation for the communication between ipcMain and ipcRenderer? #746

Open
liaodalin19903 opened this issue Sep 4, 2024 · 0 comments

Comments

@liaodalin19903
Copy link

Excuse me, is there a relatively good Typescript encapsulation for the communication between ipcMain and ipcRenderer?

According to the code written in the official documentation:

In the main process:

ipcMain.on('ping', () => console.log('pong'))

In the renderer process:

window.electron.ipcRenderer.send('ping')

This usage is not very good because:

  1. Sometimes in the renderer process, the calls are: send/sendSync/sendTo/invoke. And sometimes when the main process listens, it is:
on/handle
  1. When defining preload.ts:
    For the transmitted content:
declare global {
  interface Window {
    electron: ElectronAPI
    api: unknown
    IPC: {
      invoke: (channel: string, data: unknown[]) => Promise<unknown>
      ipcOn: (
        channel: string,
        fun: (event: IpcRendererEvent, data: unknown[]) => void
      ) => Electron.IpcRenderer
      removeAllListeners: (channel: string) => Electron.IpcRenderer
    }
  }
}

Generally, data: unknown[] is defined. When transmitting, it is very difficult to know in the main process what data type is passed in. This should be optimized. Determine a data structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant