I'm trying to ask the main application to find some kind of device, I have been trying to do it with ipc but couldn't make it work either with asynchronous send and sync send. I suspect that the main is trying to reply the promise.
Renderer:
const recognizedDevices = ipcRenderer.sendSync('findDevice');
console.log(recognizedDevices);
Main:
ipcMain.on('findDevice', (event) => findDevice(event));
const findDevice = async (event) => {
let recognizedDevices = await findConnectedDevices();
if(recognizedDevices){
console.log("found");
console.log(recognizedDevices);
return event.returnValue = recognizedDevices;
}
//TODO: If no device found
}
Result in main:
found
[
HID {
_events: [Object: null prototype] { newListener: [Function (anonymous)] },
_eventsCount: 1,
_maxListeners: undefined,
_raw: HID {},
write: [Function: bound write],
getFeatureReport: [Function: bound getFeatureReport],
sendFeatureReport: [Function: bound sendFeatureReport],
setNonBlocking: [Function: bound setNonBlocking],
readSync: [Function: bound readSync],
readTimeout: [Function: bound readTimeout],
getDeviceInfo: [Function: bound getDeviceInfo],
_paused: true,
[Symbol(kCapture)]: false
}
]
I was hoping to receive the same log result in the renderer, but unfortunately I am getting back Error: An object could not be cloned.
If I try to reply back with recognizedDevice.length I will indeed receieve "1" in the front end, so it seem like the communication between them is good. Seems like the problem is with sending the object.
Data exchanged between the main process and the renderer process via IPC channels are serialized via the Structured Clone Algorithm. Functions and symbols are not serialisable yet you're trying to ship them over:
[
HID {
˟ _events: [Object: null prototype] { newListener: [Function (anonymous)] },
_eventsCount: 1,
_maxListeners: undefined,
_raw: HID {},
˟ write: [Function: bound write],
˟ getFeatureReport: [Function: bound getFeatureReport],
˟ sendFeatureReport: [Function: bound sendFeatureReport],
˟ setNonBlocking: [Function: bound setNonBlocking],
˟ readSync: [Function: bound readSync],
˟ readTimeout: [Function: bound readTimeout],
˟ getDeviceInfo: [Function: bound getDeviceInfo],
_paused: true,
˟ [Symbol(kCapture)]: false
}
]