Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send several arguments with ipcRenderer

I'd like to know how to send several arguments via ipcRenderer on an Electron app. Should I send an array of arguments or just all the arguments separated by comma?

Thanks,

like image 728
Otto Gutierrez Avatar asked Aug 12 '18 03:08

Otto Gutierrez


2 Answers

I would recommend an object for parameter transfer. In consequence, you can also think about implementing a consistent API for your application:

var _myreq = {
  state: 0, //0 is no error, 4 is error with message, etc.
  message: "", //can include error message (if any)
  data: [0,4,6] //application data for request (String, Array, Object)
};
ipc.send('mychannel-functiona', _myreq);
like image 159
11AND2 Avatar answered Oct 13 '22 03:10

11AND2


Docs clearly shows that you can pass any number of argument to send.

Send a message to the main process asynchronously via channel, you can also send arbitrary arguments. Arguments will be serialized in JSON internally and hence no functions or prototype chain will be included.

From that point on you have no restrictions on how to use those arbitrary arguments. It depends on your needs, your codebase style etc.

like image 37
pergy Avatar answered Oct 13 '22 03:10

pergy