t4t5 / nostr-react

React Hooks for Nostr 🦤
MIT License
85 stars 14 forks source link

Unsubscribing from filters #16

Open cveering opened 1 year ago

cveering commented 1 year ago

I might be going about this wrong, but I'd like to unsubscribe from filters when the component dismounts, or after some other event, doing something like:

const {
        events,
        unsubscribe,
    } = useNostrEvents({
        filter: {
            authors: [pubkey],
            kinds: [0],
            since: 0
        },
    })

useEffect(() => {
        return () => {
            unsubscribe()
        }
}, [])

Doing this, I get an error stating unsubscribe is not a function even though the type of unsubscribe is () => void. What is the proper way to unsubscribe from a filter?

Really nice work btw... It's been fun playing around with it. :-)

wds4 commented 1 year ago

This is a good question. I don't explicitly unsubscribe from filters and things seem to work. I've been assuming the filters get closed automatically during the process of dismounting. But your question has me wondering whether subscribed filters accumulate as the user navigates around the app? maybe leading to performance degradation or other problems?

cveering commented 1 year ago

Yeah, I wonder the same thing. I'm not far enough into an app yet to notice if it's racking up subs or dropping them when the component dismounts. A common use case has been loading profiles where I'd like to let the relays respond, and then unsubscribe. Maybe I'm making too much of these open subs, but they can rack up really fast with a decently sized list of users. I'm finding that when dealing with replaceable events, I generally want a straight up round trip query/response. So this is really about working around the lack of that.

aussedatlo commented 1 year ago

unsubscribe is not in the dependency array, is this normal ?