Avoid call service when no conversation is opened

This commit is contained in:
Pierre 2017-06-25 16:06:16 +02:00
parent ce4502b571
commit 05c67afe9f

View File

@ -18,6 +18,11 @@ ComunicWeb.components.conversations.service = {
*/ */
__serviceCache: false, __serviceCache: false,
/**
* @var {Boolean} __serviceLock Specify wether the service is already in use or not
*/
__serviceLock: false,
/** /**
* Initializate conversation service * Initializate conversation service
* *
@ -32,6 +37,9 @@ ComunicWeb.components.conversations.service = {
if(this.__intervalID) if(this.__intervalID)
clearInterval(this.__intervalID); //Remove old interval clearInterval(this.__intervalID); //Remove old interval
//Force the service to unlock
this.__serviceLock = false;
//Initializate interval //Initializate interval
this.__intervalID = setInterval(function(){ this.__intervalID = setInterval(function(){
ComunicWeb.components.conversations.service.call(); ComunicWeb.components.conversations.service.call();
@ -51,11 +59,30 @@ ComunicWeb.components.conversations.service = {
//Check if the conversation element still exists or not //Check if the conversation element still exists or not
if(!byId("conversationsElem")){ if(!byId("conversationsElem")){
ComunicWeb.debug.logMessage("Conversation Service : Couldn't locate conversation element, unregistering service !"); ComunicWeb.debug.logMessage("Conversation Service : Couldn't locate conversations element, unregistering service !");
clearInterval(this.__intervalID); clearInterval(this.__intervalID);
return false; return false;
} }
//Check at least one conversation is opened
if(!this.__serviceCache){
ComunicWeb.debug.logMessage("Conversation Service : task skipped : the service cache is empty (equals to false).");
return false;
}
if(JSON.stringify(this.__serviceCache) == "{}"){
ComunicWeb.debug.logMessage("Conversation Service : task skipped : the service cache is empty. (equals to {})");
return false;
}
//Check if the service is locked or not
if(this.__serviceLock){
ComunicWeb.debug.logMessage("Conversation Service : task skipped : the service is locked.");
return false;
}
//Lock service
this.__serviceLock = true;
//Perform service task //Perform service task
this.performTask(); this.performTask();
}, },
@ -66,6 +93,7 @@ ComunicWeb.components.conversations.service = {
* @return {Boolean} True for a success * @return {Boolean} True for a success
*/ */
performTask: function(){ performTask: function(){
console.log(this.__serviceCache); console.log(this.__serviceCache);
//Prepare API request //Prepare API request
var newConversations = []; var newConversations = [];
@ -96,6 +124,21 @@ ComunicWeb.components.conversations.service = {
return true; return true;
}, },
/**
* Service callback function
*
* @param {Object}
* @return {Boolean} True for a success
*/
callback: function(){
//Unlock service
this.__serviceLock = false;
//Success
return true;
},
/** /**
* Register a new conversation * Register a new conversation
* *
@ -149,6 +192,9 @@ ComunicWeb.components.conversations.service = {
clearObject(this.__serviceCache); clearObject(this.__serviceCache);
} }
//Unlock service
this.__serviceLock = false;
//Success //Success
return true; return true;
}, },