jellyfin-web/dashboard-ui/bower_components/emby-apiclient/appstorage-localstorage.js
2016-12-01 12:38:13 -05:00

57 lines
1.1 KiB
JavaScript

define([], function () {
'use strict';
var myStore = {};
var cache;
var localData;
function updateCache() {
if (cache) {
cache.put('data', new Response(JSON.stringify(localData)));
}
}
myStore.setItem = function (name, value) {
localStorage.setItem(name, value);
if (localData) {
var changed = localData[name] !== value;
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 {
if (self.caches) {
caches.open('embydata').then(function (result) {
cache = result;
localData = {};
});
}
} catch (err) {
console.log('Error opening cache: ' + err);
}
return myStore;
});