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:
parent
4c31ebaaa5
commit
e6607509c5
2 changed files with 90 additions and 18 deletions
|
|
@ -2,6 +2,7 @@ import { ref, computed, readonly } from 'vue'
|
|||
import { useNostrStore } from '@/stores/nostr'
|
||||
import { useMarketStore, type Market, type Stall, type Product } from '@/stores/market'
|
||||
import { config } from '@/lib/config'
|
||||
import { nip19 } from 'nostr-tools'
|
||||
|
||||
// Nostr event kinds for market functionality
|
||||
const MARKET_EVENT_KINDS = {
|
||||
|
|
@ -26,7 +27,7 @@ export function useMarket() {
|
|||
error.value = null
|
||||
|
||||
// Decode naddr
|
||||
const { type, data } = window.NostrTools.nip19.decode(naddr)
|
||||
const { type, data } = nip19.decode(naddr)
|
||||
if (type !== 'naddr' || data.kind !== MARKET_EVENT_KINDS.MARKET) {
|
||||
throw new Error('Invalid market naddr')
|
||||
}
|
||||
|
|
@ -70,13 +71,11 @@ export function useMarket() {
|
|||
const client = nostrStore.getClient()
|
||||
|
||||
// Fetch market configuration event
|
||||
const filters = [{
|
||||
const events = await client.fetchEvents({
|
||||
kinds: [MARKET_EVENT_KINDS.MARKET],
|
||||
authors: [marketData.pubkey],
|
||||
'#d': [marketData.d]
|
||||
}]
|
||||
|
||||
const events = await client.fetchNotes({ filters })
|
||||
})
|
||||
|
||||
if (events.length > 0) {
|
||||
const marketEvent = events[0]
|
||||
|
|
@ -103,12 +102,10 @@ export function useMarket() {
|
|||
const client = nostrStore.getClient()
|
||||
|
||||
// Fetch stall events for this market
|
||||
const filters = [{
|
||||
const events = await client.fetchEvents({
|
||||
kinds: [MARKET_EVENT_KINDS.STALL],
|
||||
authors: [marketPubkey]
|
||||
}]
|
||||
|
||||
const events = await client.fetchNotes({ filters })
|
||||
})
|
||||
|
||||
events.forEach(event => {
|
||||
try {
|
||||
|
|
@ -145,12 +142,10 @@ export function useMarket() {
|
|||
if (stallPubkeys.length === 0) return
|
||||
|
||||
// Fetch product events from all stalls
|
||||
const filters = [{
|
||||
const events = await client.fetchEvents({
|
||||
kinds: [MARKET_EVENT_KINDS.PRODUCT],
|
||||
authors: stallPubkeys
|
||||
}]
|
||||
|
||||
const events = await client.fetchNotes({ filters })
|
||||
})
|
||||
|
||||
events.forEach(event => {
|
||||
try {
|
||||
|
|
@ -198,12 +193,24 @@ export function useMarket() {
|
|||
}
|
||||
]
|
||||
|
||||
const unsubscribe = client.subscribeToNotes((event) => {
|
||||
handleMarketEvent(event)
|
||||
}, filters)
|
||||
// Subscribe to each relay individually
|
||||
const unsubscribes = config.market.supportedRelays.map(relay => {
|
||||
const sub = client.poolInstance.subscribeMany(
|
||||
[relay],
|
||||
filters,
|
||||
{
|
||||
onevent: (event: any) => {
|
||||
handleMarketEvent(event)
|
||||
}
|
||||
}
|
||||
)
|
||||
return () => sub.close()
|
||||
})
|
||||
|
||||
// Store unsubscribe function for cleanup
|
||||
return unsubscribe
|
||||
// Return a function that unsubscribes from all relays
|
||||
return () => {
|
||||
unsubscribes.forEach(unsub => unsub())
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.error('Error subscribing to market updates:', err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue