// preload.cjs
selectFolder: () => ipcRenderer.invoke('dialog:selectFolder'),
// main.cjs
app.whenReady().then(() => {
const CrFileInstance = new CrFile({
appDebugDir: "/Volumes/DanHDD4TB1/Don't Believe The Hype/2022.12.31 - 2023.01.08 - Wellington to Acheron, St James, Rainbow, to Wellington/Day 04 - 2023.01.03 - Aratere Valley to Acheron Campsite"
});
ipcMain.handle('dialog:selectFolder', () => { CrFileInstance.handleSelectFolder(); });
});
// renderer.mjs
async function uiSelectFolder() {
const data = await window.electronAPI.selectFolder();
...
}
// CrFile.cjs
/**
* @function handleSelectFolder
* @returns {object} folderData
* @memberof CrFile
*/
async handleSelectFolder() {
const { appDebugDir } = this;
...
}
Result: Data retrieved but nothing returned to window.electronAPI.selectFolder
Test 3
// preload.cjs
selectFolder: () => ipcRenderer.invoke('dialog:selectFolder'),
// main.cjs
app.whenReady().then(() => {
// Use a global for appDebugDir
ipcMain.handle('dialog:selectFolder', CrFile.handleSelectFolder);
});
// renderer.mjs
async function uiSelectFolder() {
const data = await window.electronAPI.selectFolder();
...
}
// CrFile.cjs
async handleSelectFolder() {
// appDebugDir is accessible as a global
...
}
Result: works
Test 4
// preload.cjs
contextBridge.exposeInMainWorld('electronAPI', {
selectFolder: (data) => ipcRenderer.invoke('dialog:selectFolder', data), // pass data
});
// main.cjs
ipcMain.handle('dialog:selectFolder', CrFile.handleSelectFolder);
// renderer.mjs
// could also be in a class with global as a property instead
// as some of my classes are using this pattern
const appDebugDir = "/Volumes/DanHDD4TB1/Don't Believe The Hype/2022.12.31 - 2023.01.08 - Wellington to Acheron, St James, Rainbow, to Wellington/Day 04 - 2023.01.03 - Aratere Valley to Acheron Campsite";
async function uiSelectFolder() {
const data = await window.electronAPI.selectFolder({
appDebugDir
}); // pass data
...
}
// CrFile.cjs
static async handleSelectFolder(data) {
const { appDebugDir } = data; // receive data
...
}
Test 1
Result:
CrFileInstance
cannot accessthis.appDebugDir
Test 2
Result: Data retrieved but nothing returned to
window.electronAPI.selectFolder
Test 3
Result: works
Test 4
Result: Works