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

breaking: improve support for plugins code running in main, fixing quirks of 3.x, and making it more coherent with Cordova APIs #239

Open
wants to merge 1 commit into
base: master
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
16 changes: 12 additions & 4 deletions bin/templates/platform_www/cdv-electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,23 @@ app.on('activate', () => {
}
});

ipcMain.handle('cdv-plugin-exec', async (_, serviceName, action, ...args) => {
ipcMain.handle('cdv-plugin-exec', async (_, serviceName, action, args) => {
// This function should never return a rejected promise or throw an exception, as otherwise ipcRenderer callback will convert the parameter to a string incapsulated in an Error. See https://github.com/electron/electron/issues/24427
if (cordova && cordova.services && cordova.services[serviceName]) {
const plugin = require(cordova.services[serviceName]);

return plugin[action]
? plugin[action](args)
: Promise.reject(new Error(`The action "${action}" for the requested plugin service "${serviceName}" does not exist.`));
? new Promise((resolve) => {
// New parameters for an interface more coherent with Cordova standards
plugin[action]((result) => {
resolve({ success: true, result: result });
}, (error) => {
resolve({ success: false, result: error });
}, args);
})
: { success: false, result: new Error(`The action "${action}" for the requested plugin service "${serviceName}" does not exist.`) }; // This should not happen, maybe it should throw the error instead
} else {
return Promise.reject(new Error(`The requested plugin service "${serviceName}" does not exist have native support.`));
return { success: false, result: new Error(`The requested plugin service "${serviceName}" does not exist have native support.`) }; // This should not happen, maybe it should throw the error instead
}
});

Expand Down
13 changes: 11 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,17 @@ 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) => {
// This should not happen, maybe it should throw the error instead
(typeof error === 'function') && error(e);
}
);
},

Expand Down