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 {
|
try {
|
||||||
// Get events from all relays using the working get() method
|
// Use proper subscription method to get multiple events
|
||||||
const noteEvents = await Promise.all(
|
const allEvents: Event[] = []
|
||||||
this.relays.map(async (relay) => {
|
|
||||||
try {
|
const subscription = this.pool.subscribeMany(this.relays, filters, {
|
||||||
const filter = filters[0]
|
onevent(event: Event) {
|
||||||
const singleEvent = await this.pool.get([relay], filter)
|
allEvents.push(event)
|
||||||
return singleEvent ? [singleEvent] : []
|
},
|
||||||
} catch (error) {
|
oneose() {
|
||||||
console.warn(`Failed to fetch notes from relay ${relay}:`, error)
|
// End of stored events - subscription is complete for initial fetch
|
||||||
return []
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
|
||||||
|
// 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
|
// Flatten and deduplicate events by ID
|
||||||
const uniqueNotes = Array.from(
|
const uniqueNotes = Array.from(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue