refactor and module resolution for appStorage

This commit is contained in:
vitorsemeano 2019-02-26 23:28:16 +00:00
parent be505da8ac
commit 9ad29733bf
5 changed files with 53 additions and 85 deletions

View File

@ -0,0 +1,52 @@
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);
};
this.removeItem = function(name) {
localStorage.removeItem(name);
if (this.localData) {
this.localData[name] = null;
delete this.localData[name];
updateCache(this);
}
};
try {
self.caches && caches.open("embydata").then(onCacheOpened.bind(this));
} catch (err) {
console.log("Error opening cache: " + err);
}
}
return new MyStore;
});

View File

@ -1,23 +0,0 @@
define([], function() {
"use strict";
function MyStore() {}
function updateCache(instance) {
instance.cache.put("data", new Response(JSON.stringify(instance.localData)))
}
return MyStore.prototype.init = function() {
var instance = this;
return caches.open("embydata").then(function(result) {
instance.cache = result, instance.localData = {}
})
}, MyStore.prototype.setItem = function(name, value) {
if (this.localData) {
this.localData[name] !== value && (this.localData[name] = value, updateCache(this))
}
}, MyStore.prototype.getItem = function(name) {
if (this.localData) return this.localData[name]
}, MyStore.prototype.removeItem = function(name) {
this.localData && (this.localData[name] = null, delete this.localData[name], updateCache(this))
}, new MyStore
});

View File

@ -1,37 +0,0 @@
define([], function() {
"use strict";
function onCachePutFail(e) {
console.log(e)
}
function updateCache(instance) {
var cache = instance.cache;
cache && cache.put("data", new Response(JSON.stringify(instance.localData))).catch(onCachePutFail)
}
function onCacheOpened(result) {
this.cache = result, this.localData = {}
}
function MyStore() {
try {
self.caches && caches.open("embydata").then(onCacheOpened.bind(this))
} catch (err) {
console.log("Error opening cache: " + err)
}
}
return MyStore.prototype.setItem = function(name, value) {
localStorage.setItem(name, value);
var localData = this.localData;
if (localData) {
localData[name] !== value && (localData[name] = value, updateCache(this))
}
}, MyStore.prototype.getItem = function(name) {
return localStorage.getItem(name)
}, MyStore.prototype.removeItem = function(name) {
localStorage.removeItem(name);
var localData = this.localData;
localData && (localData[name] = null, delete localData[name], updateCache(this))
}, new MyStore
});

View File

@ -1,14 +0,0 @@
define([], function() {
"use strict";
function MyStore() {
this.localData = {}
}
return MyStore.prototype.setItem = function(name, value) {
this.localData[name] = value
}, MyStore.prototype.getItem = function(name) {
return this.localData[name]
}, MyStore.prototype.removeItem = function(name) {
this.localData[name] = null
}, new MyStore
});

View File

@ -357,16 +357,6 @@ var AppInfo = {};
return layoutManager;
}
function getAppStorage(basePath) {
try {
localStorage.setItem("_test", "0");
localStorage.removeItem("_test");
return basePath + "/appstorage-localstorage";
} catch (e) {
return basePath + "/appstorage-memory";
}
}
function createWindowHeadroom(Headroom) {
var headroom = new Headroom([], {});
headroom.init();
@ -858,7 +848,7 @@ var AppInfo = {};
});
paths.apphost = "components/apphost";
paths.appStorage = getAppStorage(apiClientBowerPath);
define('appStorage', [apiClientBowerPath + '/appStorage'], returnFirstDependency);
requirejs.config({
waitSeconds: 0,