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:
parent
bab4758f33
commit
adcba13806
1 changed files with 19 additions and 13 deletions
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue