2019-02-26 23:28:16 +00:00
|
|
|
define([], function() {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function onCachePutFail(e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateCache(instance) {
|
|
|
|
if (instance.cache) {
|
|
|
|
instance.cache.put("data", new Response(JSON.stringify(instance.localData))).catch(onCachePutFail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onCacheOpened(result) {
|
|
|
|
this.cache = result;
|
|
|
|
this.localData = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function MyStore() {
|
|
|
|
|
|
|
|
this.setItem = function(name, value) {
|
|
|
|
localStorage.setItem(name, value);
|
|
|
|
|
|
|
|
if (this.localData && this.localData[name] !== value) {
|
|
|
|
this.localData[name] = value;
|
|
|
|
updateCache(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getItem = function(name) {
|
|
|
|
return localStorage.getItem(name);
|
|
|
|
};
|
2019-02-26 23:28:16 +00:00
|
|
|
|
2019-02-26 23:28:16 +00:00
|
|
|
this.removeItem = function(name) {
|
|
|
|
localStorage.removeItem(name);
|
|
|
|
|
|
|
|
if (this.localData) {
|
|
|
|
delete this.localData[name];
|
|
|
|
updateCache(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2019-03-18 23:20:29 +00:00
|
|
|
if (self.caches) {
|
|
|
|
self.caches.open("embydata").then(onCacheOpened.bind(this));
|
|
|
|
}
|
2019-02-26 23:28:16 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.log("Error opening cache: " + err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MyStore;
|
|
|
|
});
|