sparnatural
Version:
Visual client-side SPARQL query builder and knowledge graph exploration tool
66 lines • 2.02 kB
JavaScript
class LocalDataStorage {
constructor() {
this.privateArray = new Array();
}
// Private methods and variables
get(name) {
if (this.storageAvailable()) {
if (!localStorage.getItem(name)) {
return null;
}
else {
return localStorage.getItem(name);
}
}
else {
if (this.privateArray[name]) {
return this.privateArray[name];
}
else {
return null;
}
}
}
set(name, value) {
this.privateArray[name] = value;
if (this.storageAvailable()) {
localStorage.setItem(name, value);
}
else {
this.privateArray[name] = value;
}
}
// Get the Singleton instance if one exists
// or create one if it doesn't
getInstance() {
if (!LocalDataStorage.instance) {
LocalDataStorage.instance = new LocalDataStorage();
}
return LocalDataStorage.instance;
}
// Singleton
storageAvailable() {
try {
var storage = window.localStorage, x = "__storage_test__";
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch (e) {
return (e instanceof DOMException &&
// everything except Firefox
(e.code === 22 ||
// Firefox
e.code === 1014 ||
// test name field too, because code might not be present
// everything except Firefox
e.name === "QuotaExceededError" ||
// Firefox
e.name === "NS_ERROR_DOM_QUOTA_REACHED") &&
// acknowledge QuotaExceededError only if there's something already stored
storage.length !== 0);
}
}
}
export default LocalDataStorage;
//# sourceMappingURL=LocalDataStorage.js.map