daltonmenezes / interprocess

💬 A scalable and type-safe Electron IPC management tool with enhanced DX
https://interprocess.daltonmenezes.com
MIT License
95 stars 2 forks source link

Using main objects in the handler #27

Closed axel7083 closed 7 months ago

axel7083 commented 10 months ago

I am not sure to fully understand the correct usage of the interprocess library.

Let's have a situation where your main electron process have some objects instance, for example a connection and allow to get some data from a database. This connection object is created in the src/main/index.ts file inside the createWindow function.

Now, I want to create a getData channel, fully typed, however in my src/shared/ipcs/index.ts, when I am running the createInterprocess I do not have access to the main objects.

On this desktop example there is the following

https://github.com/daltonmenezes/interprocess/blob/59f63b02fb50b1c42099fdd3b510ef27a4e57053/apps/desktop/src/main/ipc/index.ts#L5-L19

It seems that it is possible to redefine the handler, but what's should the other one return if it is something that should never be used (because no access to the database object) ?

daltonmenezes commented 7 months ago

Hi @axel7083 in the redefined context, the other handler only will be called when you explicitly call it like:

handle.getPing(async (_, { getPing, data }) => {
  const response = await getPing(_, data)  // here
})

if you don't call it, that handler will serve just for the type safe purpose and only the redefined one will be called!

In other hand, you can try to make it in the interprocess object without the redefinition, but to be able to handle main processes apis without conflict with renderer process in the same file, you must use dynamic imports:

createInterprocess({
  main: {
   async openDialog() {
     const { dialog } = await import('electron')
   }
  }