Last active
May 11, 2018 04:47
-
-
Save zeusbaba/7b4acea2730968dfcd483a179bf3f702 to your computer and use it in GitHub Desktop.
abstraction functions for using window.localStorage via store.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --- abstraction funcs for using LocalStorage via https://github.com/marcuswestin/store.js/ --- | |
var base_url = window.location.protocol + '//' + window.location.hostname; | |
var appConfig = { | |
duration: { | |
expiry: 3 * 60 *1000, // session-expiry period | |
warning: 1 * 60 * 1000, // duration before displaying popup | |
idle: 1 * 60 * 1000, // IDLE if user stays idle for this period | |
checker: 42*1000, // periodic duration used by timeoutChecker | |
countdown: 90, // secs... used for interaction to display downCounter | |
} | |
} | |
// NOTE we use "localStoreData" as jsonObject, and keep it synced with localStorage | |
var key_localStoreData = "localStoreData"; | |
function getLocalStoreData() { | |
var localStoreData = store.get(key_localStoreData); | |
if (!localStoreData) { | |
localStoreData = {}; | |
} | |
return localStoreData; | |
} | |
function setLocalStoreData(localStoreData) { | |
if (localStoreData) { // if (localStoreData !== undefined) {// avoid 'undefined' | |
store.set(key_localStoreData, localStoreData); | |
} | |
} | |
function deleteAllLocalStoreData() { | |
store.remove(key_localStoreData); // store.clearAll(); | |
} | |
function getFromLocalStoreData(key) { | |
var localStoreData = getLocalStoreData(); | |
return localStoreData[key]; | |
} | |
function deleteMultipleFromLocalStoreData(itemKeys) { | |
itemKeys.forEach(function(itemKey) { | |
deleteFromLocalStoreData(itemKey); | |
}); | |
if (DEBUG) { | |
console.log("post-DELETE-multiple... itemKeys -> " + JSON.stringify(itemKeys)); | |
} | |
} | |
function deleteFromLocalStoreData(key) { | |
var localStoreData = getLocalStoreData(); | |
delete localStoreData[key]; | |
setLocalStoreData(localStoreData); | |
if (DEBUG) { | |
console.log("post-DELETE... localStoreData -> " + JSON.stringify(localStoreData)); | |
} | |
return localStoreData; | |
} | |
function updateLocalStoreData(key, val) { | |
var localStoreData = getLocalStoreData(); | |
if (key && val) {// UPDATE given key-val | |
localStoreData[key] = val; | |
setLocalStoreData(localStoreData); | |
if (DEBUG) { | |
console.log("post-UPDATE... localStoreData -> " + JSON.stringify(localStoreData)); | |
} | |
return localStoreData; | |
} | |
if (localStoreData['duration']) { | |
// already INITed, return | |
return localStoreData; | |
} | |
// else.. INIT all the basics with key-val required for the flow | |
var currMoment = moment(); | |
var serverMoment = currMoment; // TODO get this from your server | |
localStoreData['duration'] = appConfig.duration; | |
localStoreData['offset'] = Number( currMoment.diff(serverMoment) ); | |
setLocalStoreData(localStoreData); | |
if (DEBUG) { | |
console.log("post-INIT... localStoreData -> " + JSON.stringify(localStoreData)); | |
} | |
return localStoreData; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment