From f750165c3e90c906c47c81753270ecc3537e5441 Mon Sep 17 00:00:00 2001 From: Pierre Date: Wed, 14 Jun 2017 11:46:10 +0200 Subject: [PATCH] Added cache system --- assets/js/common/cacheManager.js | 88 +++++++++++++++++++ assets/js/common/functionsSchema.js | 41 +++++++-- assets/js/common/network.js | 14 ++- assets/js/common/page.js | 2 +- assets/js/common/system.js | 47 ++++++++-- .../components/conversations/cachingOpened.js | 41 +++++++++ assets/js/components/conversations/list.js | 10 ++- assets/js/components/conversations/manager.js | 30 +++++++ assets/js/components/friends/friendsBar.js | 3 +- assets/js/components/friends/friendsList.js | 19 +++- assets/js/pages/logout.js | 7 +- assets/js/user/userInfos.js | 17 +++- corePage/config/dev.config.php | 2 + 13 files changed, 295 insertions(+), 26 deletions(-) create mode 100644 assets/js/common/cacheManager.js create mode 100644 assets/js/components/conversations/cachingOpened.js diff --git a/assets/js/common/cacheManager.js b/assets/js/common/cacheManager.js new file mode 100644 index 00000000..5f710a51 --- /dev/null +++ b/assets/js/common/cacheManager.js @@ -0,0 +1,88 @@ +/** + * Global cache management system + * + * @author Pierre HUBERT + */ + +ComunicWeb.common.cacheManager = { + /** + * @var {Array} cachesArray An array that contains all the functions that can empty caches + */ + __cachesCleaners: [], + + /** + * @var {Array} intervalsList A list of all created intervals + */ + __intervalsList: [], + + /** + * Register a new cache cleaner + * + * @param {Function} cacheCleaner The cache cleaner to register + * @param {Boolean} persistant If it is set to true, the cache will have to be cleaned only on user logout + * @return {Boolean} True for a success + */ + registerCacheCleaner: function(cacheCleaner, persistant){ + + //Add the function to the list + this.__cachesCleaners.push([cacheCleaner, persistant]); + + //Success + return true; + + }, + + /** + * Register a new interval + * + * @param {Interval} interval The interval to register + * @return {Boolean} True for a success + */ + registerInterval: function(interval){ + //Add the interval to the list + this.__intervalsList.push(interval); + + //Success + return true; + }, + + /** + * Clean the caches + * + * @param {Boolean} allCaches Specify wether persistent caches has to be cleaned or not + * @return {Boolean} True for a success + */ + cleanCaches: function(allCaches){ + + //Log action + ComunicWeb.debug.logMessage("Empty all caches"); + + //Process each cleaning function + for(i in this.__cachesCleaners){ + if(allCaches || !this.__cachesCleaners[i][1]) + eval(this.__cachesCleaners[i][0]+"()"); + } + + //Success + return true; + }, + + /** + * Unset all intervals + * + * @return {Boolean} True for a success + */ + cleanIntervals: function(){ + //Log action + ComunicWeb.debug.logMessage("Unset all intervals"); + + //Process each cleaning function + for(i in this.__intervalsList){ + //if(allCaches || !this.__intervalsList[i][1]) + clearInterval(this.__intervalsList[i]); + } + + //Success + return true + } +} \ No newline at end of file diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 33164d9a..5e4b9093 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -30,12 +30,17 @@ var ComunicWeb = { /** * Initializate the application */ - init: function(){}, + init: function(openPage){}, /** * Restart the application */ restart: function(){}, + + /** + * Reset the application + */ + reset: function(complete, openPage){}, }, /** @@ -48,6 +53,13 @@ var ComunicWeb = { makeAPIrequest: function(apiURI, params, requireLoginTokens, nextAction){}, }, + /** + * Global cache management system + */ + cacheManager:{ + //TODO : implement + }, + /** * Langs functions */ @@ -203,6 +215,11 @@ var ComunicWeb = { */ getRequest: function(url, cache, GETnextAction){}, + /** + * Empty network cache + */ + emptyCache: function(){}, + /** * Update the status of the network */ @@ -372,6 +389,11 @@ var ComunicWeb = { * Given user IDs (in an array) the function return their names in a string */ getNames: function(usersID, afterNames){}, + + /** + * Empty users cache + */ + emptyCache: function(){}, }, }, @@ -481,14 +503,14 @@ var ComunicWeb = { * Friends list caching system */ list:{ - + //TODO : implement }, /** * Friends bar */ bar:{ - + //TODO : implement }, }, @@ -514,13 +536,22 @@ var ComunicWeb = { * Conversations windows manager */ windows:{ - + //TODO : implement }, /** * Interface between conversation UI and the API */ - interface:{}, + interface:{ + //TODO : implement + }, + + /** + * Opened conversations caching system + */ + cachingOpened:{ + //TODO : implement + }, }, /** diff --git a/assets/js/common/network.js b/assets/js/common/network.js index 89fc71ae..df7f3184 100644 --- a/assets/js/common/network.js +++ b/assets/js/common/network.js @@ -64,6 +64,18 @@ ComunicWeb.common.network = { xhrRequest.send(null); }, + /** + * Empty network cache + * + * @return {Boolean} True for a success + */ + emptyCache: function(){ + this.cache = {}; + + //Success + return true; + }, + /** * Update the status of the network * @@ -102,5 +114,5 @@ ComunicWeb.common.network = { //Make sure the error message is visible on the screen byId("networkErrorMessage").style.display = "block"; } - } + }, }; \ No newline at end of file diff --git a/assets/js/common/page.js b/assets/js/common/page.js index f8c540d9..e63e8392 100644 --- a/assets/js/common/page.js +++ b/assets/js/common/page.js @@ -47,7 +47,7 @@ ComunicWeb.common.page = { this.emptyPage(); //Log message - ComunicWeb.debug.logMessage("Display a wait splash screen the screen."); + ComunicWeb.debug.logMessage("Display a wait splash screen on the screen."); //Create message element (if required) if(message){ diff --git a/assets/js/common/system.js b/assets/js/common/system.js index cae3f224..1f7e9549 100644 --- a/assets/js/common/system.js +++ b/assets/js/common/system.js @@ -8,9 +8,10 @@ ComunicWeb.common.system = { /** * Initializate the application * + * @param {String} Specify a page to open * @return {Boolean} True for a success */ - init: function(){ + init: function(openPage){ //Display Comunic logo ComunicWeb.debug.displayComunicLogo(); @@ -30,7 +31,7 @@ ComunicWeb.common.system = { ComunicWeb.common.page.emptyPage(); //Show a wait splash screen - ComunicWeb.common.page.showWaitSplashScreen(); + ComunicWeb.common.page.showWaitSplashScreen("Starting up..."); /** * Language initator @@ -45,11 +46,16 @@ ComunicWeb.common.system = { /** * Open a page */ - //Get current page URI - var currentPage = ComunicWeb.common.url.getCurrentWebsiteURL(); + if(!openPage){ + //Get current page URI + var currentPage = ComunicWeb.common.url.getCurrentWebsiteURL(); - //Open a page - ComunicWeb.common.page.openPage(currentPage); + //Open a page + ComunicWeb.common.page.openPage(currentPage); + } + else + //Open specified page + ComunicWeb.common.page.openPage(openPage); //End of init ComunicWeb.debug.logMessage("Application is ready !"); @@ -66,8 +72,9 @@ ComunicWeb.common.system = { var autoRefresh = setInterval((function(){ ComunicWeb.user.userLogin.refreshLoginState(); }), 20000); + ComunicWeb.common.cacheManager.registerInterval(autoRefresh); - //Sucess + //Success return true; }, @@ -82,5 +89,29 @@ ComunicWeb.common.system = { //Reload the page location.href = document.location; - } + }, + + /** + * Reset the application + * + * @param {Boolean} complete Specify wether the cache cleaning has to be complete or not (for logout) + * @param {String} openPage Specify a page to open once the application is restarted + * @return {Boolean} True for a success + */ + reset: function(complete, openPage){ + //Show a wait splashscreen message + ComunicWeb.common.page.showWaitSplashScreen("Reset the application in progress..."); + + //Clear intervals + ComunicWeb.common.cacheManager.cleanIntervals(); + + //Clean caches + ComunicWeb.common.cacheManager.cleanCaches(complete); + + //Init the page again + this.init(openPage); + + //Success + return true; + }, }; \ No newline at end of file diff --git a/assets/js/components/conversations/cachingOpened.js b/assets/js/components/conversations/cachingOpened.js new file mode 100644 index 00000000..50685630 --- /dev/null +++ b/assets/js/components/conversations/cachingOpened.js @@ -0,0 +1,41 @@ +/** + * Opened conversations caching system + * + * @author Pierre HUBERT + */ + +ComunicWeb.components.conversations.cachingOpened = { + + __varName: "opened-conversations-ids", + + /** + * Add a new conversation ID in the list + * + * @param {Integer} conversationID The ID of the conversation to add + * @return {Boolean} True for a success + */ + + /** + * Get all conversations ID in the list + * + * @return {Array} An array with all opened conversations ID + */ + + /** + * Empty the storage + * + * @return {Boolean} True for a success + */ + emptyStorage: function(){ + + //Remove variables for session storage + sessionStorage.removeItem(this.__varName); + + //Success + return true; + } + +} + +//Register cache cleaner +ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.components.conversations.cachingOpened.emptyStorage", true); \ No newline at end of file diff --git a/assets/js/components/conversations/list.js b/assets/js/components/conversations/list.js index bdfdeecf..053bf4c1 100644 --- a/assets/js/components/conversations/list.js +++ b/assets/js/components/conversations/list.js @@ -182,7 +182,9 @@ ComunicWeb.components.conversations.list = { infos.listBox.rootElem.remove(); //Open the conversation (under construction) - console.log("Open conversation ID: " + response.conversationID); + ComunicWeb.components.conversations.manager.openConversation({ + conversationID: response.conversationID + }); }) }, @@ -255,8 +257,10 @@ ComunicWeb.components.conversations.list = { //Remove conversations list listBox.rootElem.remove(); - //Show conversation - console.log("Open conversation ID: " + conversationInfos.ID); + //Open conversation + ComunicWeb.components.conversations.manager.openConversation({ + conversationID: conversationInfos.ID + }); } //Add conversations last activity diff --git a/assets/js/components/conversations/manager.js b/assets/js/components/conversations/manager.js index 946971bb..4b8b8d5a 100644 --- a/assets/js/components/conversations/manager.js +++ b/assets/js/components/conversations/manager.js @@ -79,4 +79,34 @@ ComunicWeb.components.conversations.manager = { ComunicWeb.components.conversations.list.display(this); } }, + + /** + * Open a conversation accordingly to specified informations + * + * @param {Object} infos Informations about the conversation to open + * @info {Integer} conversationID The ID of the conversation to open + * @return {Boolean} True or false depending of the success of the operation + */ + openConversation: function(infos){ + + //We check if a conversation ID was specified or not + if(infos.conversationID){ + ComunicWeb.debug.logMessage("Open a conversation based on its ID"); + var conversationID = infos.conversationID; + } + else { + //It is an error + ComunicWeb.debug.logMessage("Don't know which conversation to open !"); + return false; + } + + //Log action + ComunicWeb.debug.logMessage("Opening conversation " + conversationID); + + //Save conversation ID in session storage + + + //Success + return true; + } } \ No newline at end of file diff --git a/assets/js/components/friends/friendsBar.js b/assets/js/components/friends/friendsBar.js index 27bee553..aa4d185c 100644 --- a/assets/js/components/friends/friendsBar.js +++ b/assets/js/components/friends/friendsBar.js @@ -62,8 +62,9 @@ ComunicWeb.components.friends.bar = { this.refresh(listFriendsElem); //Remove previously existing interval - if(this.refreshInterval) + if(this.refreshInterval){ clearInterval(this.refreshInterval); + } //Make the friend bar automaticaly refreshed this.refreshInterval = setInterval(function(){ diff --git a/assets/js/components/friends/friendsList.js b/assets/js/components/friends/friendsList.js index fc6b02e6..2767c12d 100644 --- a/assets/js/components/friends/friendsList.js +++ b/assets/js/components/friends/friendsList.js @@ -89,4 +89,21 @@ ComunicWeb.components.friends.list = { //Success return true; }, -}; \ No newline at end of file + + /** + * Empty friends cache list + * + * @return {Boolean} True for a success + */ + emptyCache: function(){ + + //Empty cache + this.__list = {}; + + //Success + return true; + } +}; + +//Register cache cleaner +ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.components.friends.list.emptyCache"); \ No newline at end of file diff --git a/assets/js/pages/logout.js b/assets/js/pages/logout.js index 5a0ead72..35b21963 100644 --- a/assets/js/pages/logout.js +++ b/assets/js/pages/logout.js @@ -20,11 +20,8 @@ ComunicWeb.pages.logout = { //Perform logout ComunicWeb.user.userLogin.logoutUser(); - //Show a success notification - ComunicWeb.common.notificationSystem.showNotification("Good bye, you successfully terminated your session !", "success", 3); - - //Open login page - ComunicWeb.common.page.openPage("home"); + //Clean all caches + ComunicWeb.common.system.reset(true, "home"); //Remove overlay screenOverlay.remove(); diff --git a/assets/js/user/userInfos.js b/assets/js/user/userInfos.js index 11e0b8f1..5f430a9d 100644 --- a/assets/js/user/userInfos.js +++ b/assets/js/user/userInfos.js @@ -200,4 +200,19 @@ ComunicWeb.user.userInfos = { afterNames(usersName); }); }, -} \ No newline at end of file + + /** + * Empty users cache + * + * @return {Boolean} True for a success + */ + emptyCache: function(){ + this.usersInfos = {}; + + //Success + return true; + } +}; + +//Register cache cleaner +ComunicWeb.common.cacheManager.registerCacheCleaner("ComunicWeb.user.userInfos.emptyCache"); \ No newline at end of file diff --git a/corePage/config/dev.config.php b/corePage/config/dev.config.php index 4b65e2d7..f8242b8c 100644 --- a/corePage/config/dev.config.php +++ b/corePage/config/dev.config.php @@ -56,6 +56,7 @@ $config['JSfiles'] = array( "%PATH_ASSETS%js/common/functionsSchema.js", //App scripts + "%PATH_ASSETS%js/common/cacheManager.js", "%PATH_ASSETS%js/common/network.js", "%PATH_ASSETS%js/pagesList.js", "%PATH_ASSETS%js/common/api.js", @@ -84,6 +85,7 @@ $config['JSfiles'] = array( "%PATH_ASSETS%js/components/conversations/list.js", "%PATH_ASSETS%js/components/conversations/windows.js", "%PATH_ASSETS%js/components/conversations/interface.js", + "%PATH_ASSETS%js/components/conversations/cachingOpened.js", "%PATH_ASSETS%js/components/userSelect/userSelect.js", //User scripts