refactor: Update useMarket composable to utilize fetchEvents method and improve subscription handling

- Replace direct calls to fetchNotes with the new fetchEvents method in useMarket.ts for better event retrieval.
- Simplify event fetching logic by removing unnecessary filters and enhancing clarity.
- Implement individual relay subscriptions for market updates, allowing for more efficient event handling and cleanup.
This commit is contained in:
padreug 2025-08-02 17:11:23 +02:00
parent 4c31ebaaa5
commit e6607509c5
2 changed files with 90 additions and 18 deletions

View file

@ -29,6 +29,10 @@ export class NostrClient {
return this._isConnected
}
get poolInstance(): SimplePool {
return this.pool
}
async connect(): Promise<void> {
try {
// Try to connect to at least one relay
@ -205,6 +209,67 @@ export class NostrClient {
}
}
/**
* Fetch events by kind
*/
async fetchEvents(options: {
kinds: number[]
authors?: string[]
limit?: number
since?: number
until?: number
'#d'?: string[]
} = {}): Promise<Event[]> {
const {
kinds,
authors,
limit = 100,
since,
until,
'#d': dTags
} = options
const filters: Filter[] = [{
kinds,
limit,
...(authors && { authors }),
...(since && { since }),
...(until && { until }),
...(dTags && { '#d': dTags })
}]
try {
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
await new Promise(resolve => setTimeout(resolve, 2000))
// Close the subscription
subscription.close()
// Deduplicate events by ID
const uniqueEvents = Array.from(
new Map(allEvents.map(event => [event.id, event])).values()
)
return uniqueEvents
.sort((a, b) => b.created_at - a.created_at)
.slice(0, limit)
} catch (error) {
console.error('Failed to fetch events:', error)
throw error
}
}
/**
* Fetch user profiles
*/