add DB logging for service-worker events

This commit is contained in:
2023-12-20 20:40:00 -07:00
parent d6b1386741
commit f8002c4550
2 changed files with 51 additions and 11 deletions

View File

@@ -404,6 +404,27 @@ async function setMostRecentNotified(id) {
}
}
async function logMessage(message) {
try {
const db = await openIndexedDB("TimeSafari");
const transaction = db.transaction("worker_log", "readwrite");
const store = transaction.objectStore("worker_log");
// will only keep one day's worth of logs
let data = await getRecord(store, new Date().toDateString());
if (!data) {
await store.clear(); // clear out anything older than today
}
data = data || "";
data += `\n${message}`;
await updateRecord(store, data);
transaction.oncomplete = () => db.close();
return true;
} catch (error) {
console.error("IndexedDB logMessage error:", error);
return false;
}
}
function openIndexedDB(dbName) {
return new Promise((resolve, reject) => {
const request = indexedDB.open(dbName);
@@ -420,6 +441,7 @@ function getRecord(store, key) {
});
}
// Note that this assumes there is only one record in the store.
function updateRecord(store, data) {
return new Promise((resolve, reject) => {
const request = store.put(data);
@@ -437,6 +459,9 @@ async function fetchAllAccounts() {
if (!db.objectStoreNames.contains("accounts")) {
db.createObjectStore("accounts", { keyPath: "id" });
}
if (!db.objectStoreNames.contains("worker_log")) {
db.createObjectStore("worker_log");
}
};
openRequest.onsuccess = function (event) {