cleanup old data at startup

This commit is contained in:
Kieran 2023-02-01 20:34:23 +00:00
parent 59ff5de651
commit 162ab55445
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
1 changed files with 19 additions and 4 deletions

View File

@ -18,6 +18,7 @@ import { SearchRelays } from 'Const';
import useEventPublisher from "Feed/EventPublisher";
import useModeration from "Hooks/useModeration";
import { IndexedUDB, useDb } from "State/Users/Db";
import { db } from "Db";
export default function Layout() {
@ -81,10 +82,24 @@ export default function Layout() {
useEffect(() => {
// check DB support then init
IndexedUDB.isAvailable()
.then(a => {
const db = a ? "indexdDb" : "redux";
console.debug(`Using db: ${db}`);
dispatch(init(db));
.then(async a => {
const dbType = a ? "indexdDb" : "redux";
// cleanup on load
if (dbType === "indexdDb") {
await db.feeds.clear();
const now = Math.floor(new Date().getTime() / 1000);
const cleanupEvents = await db.events
.where("created_at")
.above(now - (60 * 60))
.primaryKeys();
console.debug(`Cleanup ${cleanupEvents.length} events`);
await db.events.bulkDelete(cleanupEvents)
}
console.debug(`Using db: ${dbType}`);
dispatch(init(dbType));
})
}, []);