jellyfin-web/dashboard-ui/bower_components/emby-apiclient/appstorage-localstorage.js

51 lines
1.0 KiB
JavaScript
Raw Normal View History

2016-08-13 21:30:59 -07:00
define([], function () {
2016-10-01 23:13:04 -07:00
'use strict';
2016-08-13 21:30:59 -07:00
var myStore = {};
var cache;
var localData;
function updateCache() {
cache.put('data', new Response(JSON.stringify(localData)));
}
myStore.setItem = function (name, value) {
localStorage.setItem(name, value);
if (localData) {
2016-10-01 23:13:04 -07:00
var changed = localData[name] !== value;
2016-08-13 21:30:59 -07:00
if (changed) {
localData[name] = value;
updateCache();
}
}
};
myStore.getItem = function (name) {
return localStorage.getItem(name);
};
myStore.removeItem = function (name) {
localStorage.removeItem(name);
if (localData) {
localData[name] = null;
delete localData[name];
updateCache();
}
};
try {
caches.open('embydata').then(function (result) {
cache = result;
localData = {};
});
} catch (err) {
console.log('Error opening cache: ' + err);
}
return myStore;
});