server auth
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
const STORE_CONTENT = "content";
|
||||
const STORE_PAGES = "pages";
|
||||
const STORE_PENDING_EDITS = "pending_edits";
|
||||
const CACHE_ENTRY_LIMIT = 100;
|
||||
|
||||
function openDB() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
@@ -39,6 +40,27 @@
|
||||
return path.replace(/\/$/, "") || "/";
|
||||
}
|
||||
|
||||
function pruneStore(store, limit) {
|
||||
const request = store.getAll();
|
||||
request.onsuccess = function () {
|
||||
const records = request.result || [];
|
||||
if (records.length <= limit) {
|
||||
return;
|
||||
}
|
||||
|
||||
records
|
||||
.sort(function (a, b) {
|
||||
return (a.cachedAt || 0) - (b.cachedAt || 0);
|
||||
})
|
||||
.slice(0, records.length - limit)
|
||||
.forEach(function (record) {
|
||||
if (record && record.path) {
|
||||
store.delete(record.path);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function cacheDocuments(documents) {
|
||||
return openDB().then(function (db) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
@@ -63,6 +85,7 @@
|
||||
const tx = db.transaction(STORE_CONTENT, "readwrite");
|
||||
const store = tx.objectStore(STORE_CONTENT);
|
||||
store.put({ path: path, html: html, title: title, cachedAt: Date.now() });
|
||||
pruneStore(store, CACHE_ENTRY_LIMIT);
|
||||
tx.oncomplete = function () {
|
||||
resolve();
|
||||
};
|
||||
@@ -117,6 +140,7 @@
|
||||
title: title,
|
||||
cachedAt: Date.now(),
|
||||
});
|
||||
pruneStore(store, CACHE_ENTRY_LIMIT);
|
||||
tx.oncomplete = function () {
|
||||
resolve();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user