I'm rather new to electron development and adapted code from this thread to have access to ipc methods in the renderer with nodeIntegration set to false. Everything seems to be working, I just wanted to ask if anyone had some other examples of use cases / limitations for this technique before I go off the rails and just shove every module in there.
Here's my version of the code from the above thread, this time with the electron-promise-ipc module:
preload.js
const { contextBridge, ipcRenderer } = require("electron");
const promiseIpc = require('electron-promise-ipc');
contextBridge.exposeInMainWorld(
"api", {
send: (channel, data) => {
// whitelist channels
let validChannels = [
"toMain",
"/get/comp",
"/post/comp"
];
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, data);
}
},
promise: (channel, data, func) => {
let validChannels = [
"toMain",
"/get/comp",
"/post/comp",
"test"
];
if (validChannels.includes(channel)) {
promiseIpc.send(channel, data).then((res) => {func(res)});
}
},
receive: (channel, func) => {
let validChannels = [
"fromMain",
"/get/comp"
];
if (validChannels.includes(channel)) {
ipcRenderer.on(channel, (event, ...args) => func(...args));
}
},
}
);
renderer / app.js (React app)
getComps = () => {
window.api.promise('/get/comp', { message: "Querying Compendiums..." }, (res) => {
this.setState({ comps: res });
});
}
createComp = (form) => {
window.api.promise('/post/comp', form, (res) => {
this.setState({ comps: res });
});
}