Register events via backgroundMode.on(event)

This commit is contained in:
Sebastián Katzer
2017-01-01 16:58:08 +01:00
parent 11f5d0404d
commit bb72a5e8c8
2 changed files with 141 additions and 44 deletions

View File

@@ -23,45 +23,9 @@ var exec = require('cordova/exec'),
channel = require('cordova/channel');
// Called before 'deviceready' listener will be called
channel.onCordovaReady.subscribe(function () {
// Device plugin is ready now
channel.onCordovaInfoReady.subscribe(function () {
// Set the defaults
exports.setDefaults({});
});
});
/**
* @private
*
* Flag indicated if the mode is enabled.
*/
exports._isEnabled = false;
/**
* @private
*
* Flag indicated if the mode is active.
*/
exports._isActive = false;
/**
* @private
*
* Default values of all available options.
*/
exports._defaults = {
title: 'App is running in background',
text: 'Doing heavy tasks.',
ticker: 'Running in background',
resume: true,
silent: false,
color: undefined,
icon: 'icon'
};
/*************
* INTERFACE *
*************/
/**
* Activates the background mode. When activated the application
@@ -144,17 +108,100 @@ exports.isActive = function () {
return this._isActive;
};
/**********
* EVENTS *
**********/
exports._listener = {};
/**
* Fire event with given arguments.
*
* @param [ String ] event The event's name.
* @param {args*} The callback's arguments.
*
* @return [ Void ]
*/
exports.fireEvent = function (event) {
var args = Array.apply(null, arguments).slice(1),
listener = this._listener[event];
if (!listener)
return;
for (var i = 0; i < listener.length; i++) {
var fn = listener[i][0],
scope = listener[i][1];
fn.apply(scope, args);
}
};
/**
* Register callback for given event.
*
* @param [ String ] event The event's name.
* @param [ Function ] callback The function to be exec as callback.
* @param [ Object ] scope The callback function's scope.
*
* @return [ Void ]
*/
exports.on = function (event, callback, scope) {
if (typeof callback !== "function")
return;
if (!this._listener[event]) {
this._listener[event] = [];
}
var item = [callback, scope || window];
this._listener[event].push(item);
};
/**
* Unregister callback for given event.
*
* @param [ String ] event The event's name.
* @param [ Function ] callback The function to be exec as callback.
*
* @return [ Void ]
*/
exports.un = function (event, callback) {
var listener = this._listener[event];
if (!listener)
return;
for (var i = 0; i < listener.length; i++) {
var fn = listener[i][0];
if (fn == callback) {
listener.splice(i, 1);
break;
}
}
};
/**
* @deprecated
*
* Called when the background mode has been activated.
*/
exports.onactivate = function () {};
/**
* @deprecated
*
* Called when the background mode has been deaktivated.
*/
exports.ondeactivate = function () {};
/**
* @deprecated
*
* Called when the background mode could not been activated.
*
* @param {Integer} errorCode
@@ -162,6 +209,11 @@ exports.ondeactivate = function () {};
*/
exports.onfailure = function () {};
/*********
* UTILS *
*********/
/**
* @private
*
@@ -186,3 +238,44 @@ exports.mergeWithDefaults = function (options) {
return options;
};
/***********
* PRIVATE *
***********/
/**
* @private
*
* Flag indicates if the mode is enabled.
*/
exports._isEnabled = false;
/**
* @private
*
* Flag indicates if the mode is active.
*/
exports._isActive = false;
/**
* @private
*
* Default values of all available options.
*/
exports._defaults = {
title: 'App is running in background',
text: 'Doing heavy tasks.',
ticker: 'Running in background',
resume: true,
silent: false,
color: undefined,
icon: 'icon'
};
// Called before 'deviceready' listener will be called
channel.onCordovaReady.subscribe(function () {
channel.onCordovaInfoReady.subscribe(function () {
exports.setDefaults({});
});
});