Skip to content

Commit

Permalink
Offline data storage
Browse files Browse the repository at this point in the history
  • Loading branch information
CMEONE committed Mar 5, 2021
1 parent 1451918 commit 908e9af
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions tApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class tApp {
static started = false;
static database;
static get version() {
return "v0.4.2";
return "v0.5.0";
}
static configure(params) {
if(params == null) {
Expand Down Expand Up @@ -219,6 +219,73 @@ class tApp {
resolve();
});
}
static getOfflineData(key) {
return new Promise((resolve, reject) => {
let request = tApp.database.transaction(["offlineStorage"], "readwrite").objectStore("offlineStorage").get(key);
request.onerror = (event) => {
reject("tAppError: Offline storage is not available in this browser.");
};
request.onsuccess = (event) => {
resolve(request.result);
};
});
}
static setOfflineData(key, value) {
return new Promise((resolve, reject) => {
let request = tApp.database.transaction(["offlineStorage"], "readwrite").objectStore("offlineStorage").put(value, key);
request.onerror = (event) => {
reject("tAppError: Offline storage is not available in this browser.");
};
request.onsuccess = (event) => {
resolve(true);
}
});
}
static removeOfflineData(key) {
return new Promise(async (resolve, reject) => {
let tmp = await tApp.getOfflineData(key);
let request = tApp.database.transaction(["offlineStorage"], "readwrite").objectStore("offlineStorage").delete(key);
request.onerror = (event) => {
reject("tAppError: Offline storage is not available in this browser.");
};
request.onsuccess = (event) => {
resolve(tmp);
};
});
}
static getAllOfflineDataKeys() {
return new Promise((resolve, reject) => {
let request = tApp.database.transaction(["offlineStorage"], "readwrite").objectStore("offlineStorage").getAllKeys();
request.onerror = (event) => {
reject("tAppError: Offline storage is not available in this browser.");
};
request.onsuccess = (event) => {
resolve(request.result);
};
});
}
static getAllOfflineData() {
return new Promise(async (resolve, reject) => {
let keys = await tApp.getAllOfflineDataKeys();
let offline = {};
for(let i = 0; i < keys.length; i++) {
offline[keys[i]] = await tApp.getOfflineData(keys[i]);
}
resolve(offline);
});
}
static clearOfflineData() {
return new Promise((resolve, reject) => {
let request = tApp.database.transaction(["offlineStorage"], "readwrite").objectStore("offlineStorage").clear();
request.onerror = (event) => {
reject("tAppError: Offline storage is not available in this browser.");
};
request.onsuccess = (event) => {
resolve(true);
};
});

}
static get(path) {
return new Promise(async (resolve, reject) => {
let fullPath = new URL(path, window.location.href).href;
Expand Down Expand Up @@ -316,7 +383,7 @@ class tApp {
Object.defineProperty(window, 'IDBKeyRange', {
value: window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange
});
let request = window.indexedDB.open("tAppCache", 4);
let request = window.indexedDB.open("tAppCache", 5);
request.onerror = (event) => {
console.warn("tAppWarning: Persistent caching is not available in this browser.");
tApp.config.caching.persistent = false;
Expand All @@ -341,6 +408,9 @@ class tApp {
if(!tApp.database.objectStoreNames.contains("cachedPages")) {
tApp.database.createObjectStore("cachedPages");
}
if(!tApp.database.objectStoreNames.contains("offlineStorage")) {
tApp.database.createObjectStore("offlineStorage");
}
};
} else {
window.addEventListener("hashchange", () => {
Expand Down

0 comments on commit 908e9af

Please sign in to comment.