diff --git a/.gitignore b/.gitignore index 5546d15..2b1a982 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ # Editors .vscode +.idea # Testing, code coverage, and linting .nyc_output diff --git a/bin/templates/platform_www/cdv-electron-main.js b/bin/templates/platform_www/cdv-electron-main.js index 19b852c..50f347a 100644 --- a/bin/templates/platform_www/cdv-electron-main.js +++ b/bin/templates/platform_www/cdv-electron-main.js @@ -151,7 +151,22 @@ ipcMain.handle('cdv-plugin-exec', async (_, serviceName, action, ...args) => { const plugin = require(cordova.services[serviceName]); return plugin[action] - ? plugin[action](args) + ? new Promise((resolve, reject) => { + // Added a success and error function as second and third argument, in order to allow to pass to the error callback a copy of the error argument. This was not possible if this function returns a rejected promise: see https://github.com/electron/electron/issues/24427 + const legacyresult = plugin[action](args, (result) => { + resolve({ success: true, result: result }); + }, (error) => { + resolve({ success: false, result: error }); + }); + + // For backward compatibility with cordova-electron 3.x, if plugin[action] returns (or fulfills the returned promise) before calling one of the two functions passed as arguments, it works as in previous cordova-electron versions + Promise.resolve(legacyresult) + .then((legacyresult) => { + resolve({ success: true, result: legacyresult }); + }, (legacyerror) => { + reject(legacyerror); + }); + }) : Promise.reject(new Error(`The action "${action}" for the requested plugin service "${serviceName}" does not exist.`)); } else { return Promise.reject(new Error(`The requested plugin service "${serviceName}" does not exist have native support.`)); diff --git a/bin/templates/platform_www/cdv-electron-preload.js b/bin/templates/platform_www/cdv-electron-preload.js index ac00ab2..e1c9fd1 100644 --- a/bin/templates/platform_www/cdv-electron-preload.js +++ b/bin/templates/platform_www/cdv-electron-preload.js @@ -24,8 +24,16 @@ contextBridge.exposeInMainWorld('_cdvElectronIpc', { exec: (success, error, serviceName, action, args) => { return ipcRenderer.invoke('cdv-plugin-exec', serviceName, action, args) .then( - success, - error + (response) => { + if (response.success) { + (typeof success === 'function') && success(response.result); + } else { + (typeof error === 'function') && error(response.result); + } + }, + (e) => { + (typeof error === 'function') && error(e); + } ); }, diff --git a/package-lock.json b/package-lock.json index 1da225f..8773a33 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cordova-electron", - "version": "3.1.0", + "version": "3.2.0-dev", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "cordova-electron", - "version": "3.1.0", + "version": "3.2.0-dev", "license": "Apache-2.0", "dependencies": { "cordova-common": "^4.0.2", diff --git a/package.json b/package.json index f9c9f03..88a7a13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-electron", - "version": "3.1.0", + "version": "3.2.0-dev", "description": "electron apps as a target for cordova developers", "main": "lib/Api.js", "repository": "github:apache/cordova-electron",