feat: Enhance useMarket composable with improved error handling and sample data loading

- Add detailed logging for market loading, configuration, stalls, and products to aid in debugging.
- Implement graceful error handling by creating default markets and stalls when data loading fails.
- Introduce a method to add sample products for testing when no products are found, improving development experience.
- Update market data fetching logic to ensure consistent handling of identifiers and event processing.
This commit is contained in:
padreug 2025-08-02 17:35:41 +02:00
parent e6607509c5
commit 54044f165c
2 changed files with 184 additions and 26 deletions

View file

@ -229,33 +229,69 @@ export class NostrClient {
'#d': dTags
} = options
const filters: Filter[] = [{
// Build filter object, only including defined properties
const filter: Filter = {
kinds,
limit,
...(authors && { authors }),
...(since && { since }),
...(until && { until }),
...(dTags && { '#d': dTags })
}]
limit
}
if (authors && authors.length > 0) {
filter.authors = authors
}
if (since) {
filter.since = since
}
if (until) {
filter.until = until
}
if (dTags && dTags.length > 0) {
filter['#d'] = dTags
}
const filters: Filter[] = [filter]
try {
console.log('Fetching events with filters:', JSON.stringify(filters, null, 2))
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
// Try each relay individually to identify problematic relays
const relayResults = await Promise.allSettled(
this.relays.map(async (relay) => {
try {
const relayEvents: Event[] = []
const subscription = this.pool.subscribeMany([relay], filters, {
onevent(event: Event) {
relayEvents.push(event)
},
oneose() {
// End of stored events
}
})
// Wait for events to be collected
await new Promise(resolve => setTimeout(resolve, 1000))
subscription.close()
return relayEvents
} catch (error) {
console.warn(`Failed to fetch from relay ${relay}:`, error)
return []
}
})
)
// Collect all successful results
relayResults.forEach((result, index) => {
if (result.status === 'fulfilled') {
allEvents.push(...result.value)
} else {
console.warn(`Relay ${this.relays[index]} failed:`, result.reason)
}
})
// 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()