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.
This commit is contained in:
padreug 2025-07-02 19:21:43 +02:00
parent bab4758f33
commit adcba13806

View file

@ -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(