Closed thehans closed 1 year ago
I finally resolved my issue. I had not realized that ipcRenderer.send
is asynchronous, therefore it cannot return a value. (I'm still very new and learning a lot of electron/node ecosystem and APIs)
In order to do things synchronously, you have to use ipcRenderer.sendSync
and the listener should set event.returnValue
.
src/main/index.js
...
ipcMain.on('config-load', (event) => { event.returnValue = store.get(); });
ipcMain.on('config-get', (event, key, defaultValue) => { event.returnValue = store.get(key, defaultValue); });
ipcMain.on('config-set', (event, key, value) => { store.set(key, value); });
ipcMain.on('config-has', (event, key) => { event.returnValue = store.has(key); });
src/preload.js
...
contextBridge.exposeInMainWorld('config', {
load: () => ipcRenderer.sendSync('config-load'),
get: (key, defaultValue) => ipcRenderer.sendSync('config-get', key, defaultValue),
set: (key, value) => ipcRenderer.sendSync('config-set', key, value),
has: (key) => ipcRenderer.sendSync('config-has', key),
})
Now everything seems to be functioning as expected for me.
I am trying to add electron-store to my project and have been struggling for days to get it to function at all, when using all the oppressive modern security defaults:
Can anyone see what I might be doing wrong? Here is a rough summary of the code I'm trying to use
src/main/index.js
src/preload.js
src/renderer/index.js
I am able to build and start the app, but cannot read or write any config values.
I open a dev console and first try to see if I can view the whole store as one object:
Then I test what happens if I try to read a value:
If I try to write a value:
And try to check for the existence of a value: