Skip to content

Commit

Permalink
feat: keep callback
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-bueno committed Jan 23, 2022
1 parent b140687 commit fb1bd89
Show file tree
Hide file tree
Showing 7 changed files with 7,116 additions and 26 deletions.
43 changes: 43 additions & 0 deletions bin/templates/platform_www/cdv-electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,48 @@ ipcMain.handle('cdv-plugin-exec', async (_, serviceName, action, ...args) => {
}
});

function cdvPluginExec$Reply (event, request, error, data, keepCallback) {
event.reply('cdv-plugin-exec$', {
id: request.id,
serviceName: request.serviceName,
action: request.action,
data,
error,
keepCallback
});
}

ipcMain.on('cdv-plugin-exec$', (event, request) => {
const { serviceName, action, args } = request;

if (!cordova || !cordova.services || !cordova.services[serviceName]) {
const error = new Error(`The requested plugin service "${serviceName}" does not exist have native support.`);
return cdvPluginExec$Reply(event, request, error);
}

const plugin = require(cordova.services[serviceName]);

if (!plugin[action]) {
const error = new Error(`The action "${action}" for the requested plugin service "${serviceName}" does not exist.`);
return cdvPluginExec$Reply(event, request, error);
}

function successCallback (data, keepCallback) {
cdvPluginExec$Reply(event, request, null, data, keepCallback);
}

function errorCallback (error) {
cdvPluginExec$Reply(event, request, error);
}

try {
plugin[action](successCallback, errorCallback, args);
} catch (e) {
console.error(e);
const error = new Error(`An error occured executing action "${action}" for the requested plugin service "${serviceName}".`);
cdvPluginExec$Reply(event, request, error);
}
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
24 changes: 21 additions & 3 deletions bin/templates/platform_www/cdv-electron-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,26 @@ contextBridge.exposeInMainWorld('_cdvElectronIpc', {
error
);
},

exec$: (success, error, serviceName, action, args) => {
const id = Date.now() + Math.random();
const listener = (event, response) => {
if (id !== response.id) {
return;
}
if (response.error) {
ipcRenderer.removeListener('cdv-plugin-exec$', listener);
error(response.error);
return;
}
if (!response.keepCallback) {
ipcRenderer.removeListener('cdv-plugin-exec$', listener);
}
success(response.data);
};
ipcRenderer.on('cdv-plugin-exec$', listener);
ipcRenderer.send('cdv-plugin-exec$', { id, serviceName, action, args });
},
hasService: (serviceName) => cordova &&
cordova.services &&
cordova.services[serviceName]
cordova.services &&
cordova.services[serviceName]
});
5 changes: 4 additions & 1 deletion cordova-js-src/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const execProxy = require('cordova/exec/proxy');
module.exports = function (success, fail, service, action, args) {
if (window._cdvElectronIpc.hasService(service)) {
// Electron based plugin support
window._cdvElectronIpc.exec(success, fail, service, action, args);
const f = action.endsWith('$')
? window._cdvElectronIpc.exec$
: window._cdvElectronIpc.exec;
f(success, fail, service, action, args);
} else {
// Fall back for browser based plugin support...
const proxy = execProxy.get(service, action);
Expand Down
Loading

0 comments on commit fb1bd89

Please sign in to comment.