Sunday, 30 April 2017

'mainWindow.webContents.send()' not called when promise done

In the code snippet below I am using fs-jetpack to asynchronously copy files listed in an array. When all files have been copied, I try to send a message to my main Electron window but it doesn't get sent.

I've set up and tested the message receiver of the ipcRenderer so this seems to have something to do with using a promise. I haven't used those much so I am probably doing it wrong. What am I missing here?


In main.js

copyObj.imagegallery.forEach(function (file, index) {
    destination = path.join(projectPath, 'assets', 'images', path.basename(file))
    jetpack.copyAsync(file, destination, { matching: ['*.jpg', '*.jpeg', '*.png'], overwrite: true })
        .then((data) => {
             // test if all files copied
            if (index == copyObj.imagegallery.length - 1) {

                // this gets called
                console.log('copy done -imagegallery');

                // this doesn't get called
                mainWindow.webContents.send('asset-selected', callbackParam);
            }
        })
});


In my mainWindow.webcontents:

ipcRenderer.on('asset-selected', (event, arg) => {
    console.log('arg', arg.caller, arg.filename);
});



via No Grabbing

No comments:

Post a Comment