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 Dec 4, 2022
1 parent 7053f35 commit 3fa3b9e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,29 @@ module.exports = {
} catch (e) {
console.log(e);
}
},
countdown$: (successCallback, errorCallback, args) => {
if (!args || !args.length || typeof args[0] != "number") {
errorCallback("NUMERIC_ARG_NEEDED");
return;
}

let num = args[0];

if (num <= 0) {
successCallback(0, false);
}

successCallback(num, true);

const interval = setInterval(() => {
num--;
if (num > 0) {
successCallback(num, true);
} else {
clearInterval(interval);
successCallback(num, false);
}
}, 1000);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ Sample.prototype.getInfo = function (successCallback, errorCallback) {
exec(successCallback, errorCallback, 'Sample', 'getSampleInfo', []);
};

/**
* Count down
*
* @param {Function} successCallback The function to call when the heading data is available
* @param {Function} errorCallback The function to call when there is an error getting the heading data.
* @param {Function} num Number to start the countdown from
*/
Sample.prototype.countdown = function (successCallback, errorCallback, num) {
argscheck.checkArgs("fF", "Sample.countDown", arguments);
exec(successCallback, errorCallback, "Sample", "countdown$", [num]);
};

module.exports = new Sample();

0 comments on commit 3fa3b9e

Please sign in to comment.