From adcba138060bf63b5125440a9238f1a5a5b4dab8 Mon Sep 17 00:00:00 2001 From: padreug Date: Wed, 2 Jul 2025 19:21:43 +0200 Subject: [PATCH] feat: Refactor event fetching in NostrClient to use subscription method - Replace the previous get() method with a subscription approach to retrieve multiple events from relays. - Implement event collection with a timeout to ensure all stored events are fetched before closing the subscription. - Adjust the format of collected events to match expected structure. --- src/lib/nostr/client.ts | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/lib/nostr/client.ts b/src/lib/nostr/client.ts index dad7531..87d02bb 100644 --- a/src/lib/nostr/client.ts +++ b/src/lib/nostr/client.ts @@ -75,19 +75,25 @@ export class NostrClient { ] try { - // Get events from all relays using the working get() method - const noteEvents = await Promise.all( - this.relays.map(async (relay) => { - try { - const filter = filters[0] - const singleEvent = await this.pool.get([relay], filter) - return singleEvent ? [singleEvent] : [] - } catch (error) { - console.warn(`Failed to fetch notes from relay ${relay}:`, error) - return [] - } - }) - ) + // Use proper subscription method to get multiple events + const allEvents: Event[] = [] + + const subscription = this.pool.subscribeMany(this.relays, filters, { + onevent(event: Event) { + allEvents.push(event) + }, + oneose() { + // End of stored events - subscription is complete for initial fetch + } + }) + + // Wait for events to be collected (give it time to get all stored events) + await new Promise(resolve => setTimeout(resolve, 2000)) + + // Close the subscription since we just want the initial fetch + subscription.close() + + const noteEvents = [allEvents] // Wrap in array to match expected format // Flatten and deduplicate events by ID const uniqueNotes = Array.from(