Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve support for plugins code running in main, keeping backward compatibility #238

Open
wants to merge 2 commits into
base: 3.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Editors
.vscode
.idea

# Testing, code coverage, and linting
.nyc_output
Expand Down
17 changes: 16 additions & 1 deletion bin/templates/platform_www/cdv-electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:/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.`));
Expand Down
12 changes: 10 additions & 2 deletions bin/templates/platform_www/cdv-electron-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
},

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down