diff --git a/www/Insomnia.js b/www/Insomnia.js index f04ab86..5bb2e3e 100755 --- a/www/Insomnia.js +++ b/www/Insomnia.js @@ -1,21 +1,26 @@ -function Insomnia() { -} +var exec = require('cordova/exec'); -Insomnia.prototype.keepAwake = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "Insomnia", "keepAwake", []); +// helper functions +var callNative = function (successCallback, errorCallback, name) { + exec(handleUndefined(successCallback), handleUndefined(errorCallback), 'Insomnia', name, []); }; -Insomnia.prototype.allowSleepAgain = function (successCallback, errorCallback) { - cordova.exec(successCallback, errorCallback, "Insomnia", "allowSleepAgain", []); +var handleUndefined = function (callback) { + return callback || defaultHandler; }; -Insomnia.install = function () { - if (!window.plugins) { - window.plugins = {}; - } +var defaultHandler = function (msg) {}; + + +// interface object +var Insomnia = function () {}; - window.plugins.insomnia = new Insomnia(); - return window.plugins.insomnia; +Insomnia.prototype.keepAwake = function (successCallback, errorCallback) { + callNative(successCallback, errorCallback, 'keepAwake'); +}; + +Insomnia.prototype.allowSleepAgain = function (successCallback, errorCallback) { + callNative(successCallback, errorCallback, 'allowSleepAgain'); }; -cordova.addConstructor(Insomnia.install); \ No newline at end of file +module.exports = new Insomnia(); \ No newline at end of file