chore: remove feed cache
This commit is contained in:
parent
3f406ec19e
commit
a49121c05a
@ -1,10 +1,9 @@
|
|||||||
import Dexie, { Table } from "dexie";
|
import Dexie, { Table } from "dexie";
|
||||||
import { TaggedRawEvent, u256 } from "@snort/nostr";
|
import { u256 } from "@snort/nostr";
|
||||||
import { MetadataCache } from "State/Users";
|
import { MetadataCache } from "State/Users";
|
||||||
import { hexToBech32 } from "Util";
|
|
||||||
|
|
||||||
export const NAME = "snortDB";
|
export const NAME = "snortDB";
|
||||||
export const VERSION = 3;
|
export const VERSION = 4;
|
||||||
|
|
||||||
export interface SubCache {
|
export interface SubCache {
|
||||||
id: string;
|
id: string;
|
||||||
@ -15,27 +14,14 @@ export interface SubCache {
|
|||||||
|
|
||||||
const STORES = {
|
const STORES = {
|
||||||
users: "++pubkey, name, display_name, picture, nip05, npub",
|
users: "++pubkey, name, display_name, picture, nip05, npub",
|
||||||
events: "++id, pubkey, created_at",
|
|
||||||
feeds: "++id",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export class SnortDB extends Dexie {
|
export class SnortDB extends Dexie {
|
||||||
users!: Table<MetadataCache>;
|
users!: Table<MetadataCache>;
|
||||||
events!: Table<TaggedRawEvent>;
|
|
||||||
feeds!: Table<SubCache>;
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super(NAME);
|
super(NAME);
|
||||||
this.version(VERSION)
|
this.version(VERSION).stores(STORES);
|
||||||
.stores(STORES)
|
|
||||||
.upgrade(async tx => {
|
|
||||||
await tx
|
|
||||||
.table("users")
|
|
||||||
.toCollection()
|
|
||||||
.modify(user => {
|
|
||||||
user.npub = hexToBech32("npub", user.pubkey);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,7 @@ import { useEffect, useMemo, useReducer, useState } from "react";
|
|||||||
import { TaggedRawEvent } from "@snort/nostr";
|
import { TaggedRawEvent } from "@snort/nostr";
|
||||||
import { Subscriptions } from "@snort/nostr";
|
import { Subscriptions } from "@snort/nostr";
|
||||||
import { System } from "System";
|
import { System } from "System";
|
||||||
import { debounce, unwrap } from "Util";
|
import { debounce } from "Util";
|
||||||
import { db } from "Db";
|
|
||||||
|
|
||||||
export type NoteStore = {
|
export type NoteStore = {
|
||||||
notes: Array<TaggedRawEvent>;
|
notes: Array<TaggedRawEvent>;
|
||||||
@ -80,7 +79,6 @@ export default function useSubscription(
|
|||||||
const [state, dispatch] = useReducer(notesReducer, initStore);
|
const [state, dispatch] = useReducer(notesReducer, initStore);
|
||||||
const [debounceOutput, setDebounceOutput] = useState<number>(0);
|
const [debounceOutput, setDebounceOutput] = useState<number>(0);
|
||||||
const [subDebounce, setSubDebounced] = useState<Subscriptions>();
|
const [subDebounce, setSubDebounced] = useState<Subscriptions>();
|
||||||
const useCache = useMemo(() => options?.cache === true, [options]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (sub) {
|
if (sub) {
|
||||||
@ -97,25 +95,11 @@ export default function useSubscription(
|
|||||||
end: false,
|
end: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (useCache) {
|
|
||||||
// preload notes from db
|
|
||||||
PreloadNotes(subDebounce.Id)
|
|
||||||
.then(ev => {
|
|
||||||
dispatch({
|
|
||||||
type: "EVENT",
|
|
||||||
ev: ev,
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(console.warn);
|
|
||||||
}
|
|
||||||
subDebounce.OnEvent = e => {
|
subDebounce.OnEvent = e => {
|
||||||
dispatch({
|
dispatch({
|
||||||
type: "EVENT",
|
type: "EVENT",
|
||||||
ev: e,
|
ev: e,
|
||||||
});
|
});
|
||||||
if (useCache) {
|
|
||||||
db.events.put(e);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
subDebounce.OnEnd = c => {
|
subDebounce.OnEnd = c => {
|
||||||
@ -144,15 +128,7 @@ export default function useSubscription(
|
|||||||
System.RemoveSubscription(subDebounce.Id);
|
System.RemoveSubscription(subDebounce.Id);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, [subDebounce, useCache]);
|
}, [subDebounce]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (subDebounce && useCache) {
|
|
||||||
return debounce(500, () => {
|
|
||||||
TrackNotesInFeed(subDebounce.Id, state.notes).catch(console.warn);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, [state, useCache]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return debounce(DebounceMs, () => {
|
return debounce(DebounceMs, () => {
|
||||||
@ -174,23 +150,3 @@ export default function useSubscription(
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Lookup cached copy of feed
|
|
||||||
*/
|
|
||||||
const PreloadNotes = async (id: string): Promise<TaggedRawEvent[]> => {
|
|
||||||
const feed = await db.feeds.get(id);
|
|
||||||
if (feed) {
|
|
||||||
const events = await db.events.bulkGet(feed.ids);
|
|
||||||
return events.filter(a => a !== undefined).map(a => unwrap(a));
|
|
||||||
}
|
|
||||||
return [];
|
|
||||||
};
|
|
||||||
|
|
||||||
const TrackNotesInFeed = async (id: string, notes: TaggedRawEvent[]) => {
|
|
||||||
const existing = await db.feeds.get(id);
|
|
||||||
const ids = Array.from(new Set([...(existing?.ids || []), ...notes.map(a => a.id)]));
|
|
||||||
const since = notes.reduce((acc, v) => (acc > v.created_at ? v.created_at : acc), +Infinity);
|
|
||||||
const until = notes.reduce((acc, v) => (acc < v.created_at ? v.created_at : acc), -Infinity);
|
|
||||||
await db.feeds.put({ id, ids, since, until });
|
|
||||||
};
|
|
||||||
|
@ -17,7 +17,6 @@ import { SearchRelays, SnortPubKey } from "Const";
|
|||||||
import useEventPublisher from "Feed/EventPublisher";
|
import useEventPublisher from "Feed/EventPublisher";
|
||||||
import useModeration from "Hooks/useModeration";
|
import useModeration from "Hooks/useModeration";
|
||||||
import { IndexedUDB } from "State/Users/Db";
|
import { IndexedUDB } from "State/Users/Db";
|
||||||
import { db } from "Db";
|
|
||||||
import { bech32ToHex } from "Util";
|
import { bech32ToHex } from "Util";
|
||||||
import { NoteCreator } from "Element/NoteCreator";
|
import { NoteCreator } from "Element/NoteCreator";
|
||||||
import Plus from "Icons/Plus";
|
import Plus from "Icons/Plus";
|
||||||
@ -120,15 +119,6 @@ export default function Layout() {
|
|||||||
// cleanup on load
|
// cleanup on load
|
||||||
if (dbType === "indexdDb") {
|
if (dbType === "indexdDb") {
|
||||||
IndexedUDB.ready = true;
|
IndexedUDB.ready = true;
|
||||||
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}`);
|
console.debug(`Using db: ${dbType}`);
|
||||||
|
Loading…
Reference in New Issue
Block a user