Polishing up localStorage helpers

This commit is contained in:
James Barnsley
2017-11-24 09:02:42 +13:00
parent 64bbaf9d1f
commit 15bdb4d7a1
8 changed files with 330 additions and 252 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -22,9 +22,6 @@
<link rel="apple-touch-icon" sizes="192x192" href="/iris/assets/logo-app-192.png">
<link rel="apple-touch-icon" sizes="512x512" href="/iris/assets/logo-app-512.png">
<!-- Raven Sentry -->
<script src="https://cdn.ravenjs.com/3.17.0/raven.min.js" crossorigin="anonymous"></script>
</head>
<body>
@ -38,7 +35,7 @@
// Release details
// These are automatically injected by build.sh
var build = "1511424989";
var build = "1511467173";
var version = "3.8.5";
// Construct the script tag
@ -67,7 +64,7 @@
} else {
js.src = '/iris/app.min.js?v='+build;
css.href = '/iris/app.min.css?v='+build;
// Initiate error tracking. This helps catch errors early so
// patches can accurately and quickly fix issues.
Raven.config(

View File

@ -22,9 +22,6 @@
<link rel="apple-touch-icon" sizes="192x192" href="/iris/assets/logo-app-192.png">
<link rel="apple-touch-icon" sizes="512x512" href="/iris/assets/logo-app-512.png">
<!-- Raven Sentry -->
<script src="https://cdn.ravenjs.com/3.17.0/raven.min.js" crossorigin="anonymous"></script>
</head>
<body>
@ -67,7 +64,7 @@
} else {
js.src = '/iris/app.min.js?v='+build;
css.href = '/iris/app.min.css?v='+build;
// Initiate error tracking. This helps catch errors early so
// patches can accurately and quickly fix issues.
Raven.config(

View File

@ -22,6 +22,12 @@ var storage = (function() {
} catch (exception) {}
}());
/**
* Get a storage value
*
* @param key = string
* @param default_value = mixed (optional, if localStorage key doesn't exist, return this)
**/
export let getStorage = function(key, default_value = {}){
if (storage){
var value = storage.getItem(key);
@ -37,22 +43,34 @@ export let getStorage = function(key, default_value = {}){
}
}
export let setStorage = function(key, value){
/**
* Set a storage value
*
* @param key = string
* @param value = object
* @param replace = boolean (optional, completely replace our local value rather than merging it)
**/
export let setStorage = function(key, value, replace = false){
if (storage){
var stored_value = storage.getItem(key);
if (stored_value){
// We have nothing to merge with, or we want to completely replace previous value
if (!stored_value || replace){
var new_value = value;
// Merge new value with existing
} else {
var new_value = Object.assign(
{},
JSON.parse(stored_value),
value
);
} else {
var new_value = value;
}
storage.setItem(key, JSON.stringify(new_value));
return;
} else {
console.warn("localStorage not available. '"+key+"'' will not perist when you close your browser.");
return false;
return;
}
}

View File

@ -153,17 +153,21 @@ const localstorageMiddleware = (function(){
break
case 'SUPPRESS_BROADCAST':
var ui = JSON.parse(localStorage.getItem('ui'))
if (!ui) ui = {}
var ui = helpers.getStorage('ui');
if (ui.suppressed_broadcasts !== undefined){
var suppressed_broadcasts = ui.suppressed_broadcasts;
} else {
var suppressed_broadcasts = [];
}
var suppressed_broadcasts = (typeof(ui.suppressed_broadcasts) !== 'undefined' ? ui.suppressed_broadcasts : [])
suppressed_broadcasts.push(action.key)
suppressed_broadcasts.push(action.key);
Object.assign(
ui,
{ suppressed_broadcasts: suppressed_broadcasts }
helpers.setStorage(
'ui',
{
suppressed_broadcasts: suppressed_broadcasts
}
);
localStorage.setItem('ui', JSON.stringify(ui))
break
case 'LASTFM_AUTHORIZATION_GRANTED':