Increase mention fetch timeout and retry on empty

Tag-based #p queries are slower on some relays. Increase timeout
from 8s to 12s for fetchMentions. Also retry once after 3s if the
initial notification fetch returns empty (helps on cold start when
relays need time to connect).
This commit is contained in:
Jure
2026-03-29 18:23:02 +02:00
parent 2bb1341eed
commit dd565ccb16
2 changed files with 12 additions and 3 deletions

View File

@@ -74,10 +74,11 @@ export async function fetchFollowSuggestions(myFollows: string[]): Promise<{ pub
export async function fetchMentions(pubkey: string, since: number, limit = 50): Promise<NDKEvent[]> {
const instance = getNDK();
// Use a longer timeout for #p queries — some relays are slow to index tag lookups
const events = await fetchWithTimeout(
instance,
{ kinds: [NDKKind.Text], "#p": [pubkey], since, limit },
FEED_TIMEOUT,
12000,
);
return Array.from(events).sort((a, b) => (b.created_at ?? 0) - (a.created_at ?? 0));
}

View File

@@ -100,10 +100,18 @@ export const useNotificationsStore = create<NotificationsState>((set, get) => ({
set({ loading: true });
try {
const since = Math.floor(Date.now() / 1000) - 7 * 86400;
const events = await fetchMentions(pubkey, since, MAX_NOTIFICATIONS);
const others = events.filter((e) => e.pubkey !== pubkey);
let events = await fetchMentions(pubkey, since, MAX_NOTIFICATIONS);
let others = events.filter((e) => e.pubkey !== pubkey);
debug.log("notif:fetch", events.length, "raw →", others.length, "others");
// Retry once if empty — relays may need more time for #p tag queries
if (others.length === 0) {
await new Promise((r) => setTimeout(r, 3000));
events = await fetchMentions(pubkey, since, MAX_NOTIFICATIONS);
others = events.filter((e) => e.pubkey !== pubkey);
debug.log("notif:fetch retry →", others.length, "others");
}
// Don't overwrite existing notifications with empty results (relay timeout/disconnect)
const { readIds, notifications: existing } = get();
if (others.length === 0 && existing.length > 0) {