Skip to content

Instantly share code, notes, and snippets.

@zeusbaba
Last active May 11, 2018 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeusbaba/7b4acea2730968dfcd483a179bf3f702 to your computer and use it in GitHub Desktop.
Save zeusbaba/7b4acea2730968dfcd483a179bf3f702 to your computer and use it in GitHub Desktop.
abstraction functions for using window.localStorage via store.js
// --- 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