forked from jsnbuchanan/crowd-funder-for-time-pwa
add DB logging for service-worker events
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user