#Cannot pass parameters to function that returns promise via IPC (using TypeScript)

68 messages · Page 1 of 1 (latest)

strong rain
#

I have defined 2 IPC renderer functions but no matter which one I call they both resolve to same function

In index:

const createWindow = (): void => {
  // setup inter process communication (ipc)
  ipcMain.handle('mainproc:getComPorts', getComPorts);
  ipcMain.handle('mainproc:connectModbus', (event, comPort: string) => connectModbus(event, comPort));

In preload:

import { contextBridge, ipcRenderer } from 'electron';

contextBridge.exposeInMainWorld('electron', {
  getComPorts: () => ipcRenderer.invoke('mainproc:getComPorts'),
  connectModbus: (comPort: string) => ipcRenderer.invoke('mainproc:connectModbus', [comPort])
})

In renderer:

declare global {
    interface Window {
      electron: {
        getComPorts: () => Promise<string[]>;
        connectModbus(comPort: string): Promise<boolean>;
      };
    }
  }

In React component:

function connectToModbus(){
        debugger;
        window.electron.connectModbus('seans test').then((value => {
            debugger;
            // do something with return
            let qwerty = value;
            }))
            .catch((error) => {
                debugger;
                console.error('Promise rejected with error: ' + error);
            });
    }
north nimbus
#

I'm not good with TS, but shouldn't it be:

declare global {
  interface Window {
    electron: {
      getComPorts: () => Promise<string[]>;
      connectModbus: (comPort: string) => Promise<boolean>;
    };
  }
}

?

What error do you get?

strong rain
#

I have tried that and a few variations,

#

The error I get is that when I call the IPC function for

connectModbus('some string')

#

it actually calls the
getComPorts()

function in my main process

#

no idea why

north nimbus
#

well yes, this code looks incorrect

#
connectModbus: (comPort: string) => Promise<boolean>;
strong rain
#

this was the one you suggested

north nimbus
#

not on the screenshot you shared

strong rain
#

aaa im an idiot

#

okay i tried this

connectModbus: (comPort: string) => Promise<boolean>;

#
 connectModbus: (comPort: string) => Promise<boolean>;
#

but its still calling the other function somehow

tranquil grail
#

What other function means?

strong rain
#

getComPorts

#

its calling into getComPorts some how

tranquil grail
#

so?

strong rain
#

I dont understand

north nimbus
#

What make you think that?

strong rain
#

I have a debugger statement in there

tranquil grail
#

Js cant call other function you didnt call

strong rain
#

the debugger stops in getComPorts when I call window.electron.connectModbus('seans test')

tranquil grail
#

Then debugger is lying

#

or you interpret it wrong

strong rain
tranquil grail
#

this is main process not preload

strong rain
#

this is where debugger stop when I call the function

tranquil grail
#

or not?

strong rain
#

yes its main process

tranquil grail
#

you cant call a function from main in preload

strong rain
#

in electron-forge template index.ts replace main.js for some reason

tranquil grail
#

only passing ipc message

strong rain
tranquil grail
#

and preload/renderer function?

strong rain
tranquil grail
#

renderer code now

strong rain
tranquil grail
#

when you call getComPorts?

strong rain
#

I commented out all code that calls getComPorts

#

but it would be on component render usually

tranquil grail
#

may be code didnt update?

strong rain
#

i will exit vscode and try again

#

still calls into getComPorts

#

somehow

#

for reference, the commented out code looks like:

tranquil grail
#

and console log and show output

#

without debugger

#

in preload and in main process

strong rain
#

running it now

#

first this

#

but I dont see back end console log anywhere...

#

anyway

#

okay... so its calling the correct backend function but debugger doesnt stop there

#

interesting

#

okay with console logs i can see it calls there and is 'working' for what i put there

#

but something must be wrong with my debug configuration i guess

#

thanks @tranquil grail

#

and thanks @north nimbus

#

I still dont know whats wrong exactly here but I know its in my debug confiration at least

tranquil grail
#

probably with mapping ts src vs js

strong rain
#

When I look in google it says debugger is running different code to my application

#

TypeScript trying to ruin my composure yet again

#

maybe