mirror of
https://github.com/t4t5/nostr-react.git
synced 2025-09-17 13:18:54 +02:00
Working unsubscribe function
This commit is contained in:
@@ -123,6 +123,9 @@ export function useNostr() {
|
||||
export function useNostrEvents({ filter }: { filter: Filter }) {
|
||||
const { isLoading, onConnect, debug, connectedRelays } = useNostr()
|
||||
const [events, setEvents] = useState<NostrEvent[]>([])
|
||||
const [unsubscribe, setUnsubscribe] = useState(() => {
|
||||
return
|
||||
})
|
||||
|
||||
let onEventCallback: null | OnEventFunc = null
|
||||
|
||||
@@ -130,19 +133,17 @@ export function useNostrEvents({ filter }: { filter: Filter }) {
|
||||
const filterBase64 =
|
||||
typeof window !== "undefined" ? window.btoa(JSON.stringify(filter)) : null
|
||||
|
||||
let unsubscribe = () => {
|
||||
return
|
||||
}
|
||||
|
||||
const subscribe = (relay: Relay) => {
|
||||
log(debug, "info", "⬆️ nostr: Subscribing to filter:", filter)
|
||||
const sub = relay.sub([filter])
|
||||
|
||||
unsubscribe = () => {
|
||||
log(debug, "info", "✌️ nostr: Unsubscribing from filter:", filter)
|
||||
const unsubscribeFunc = () => {
|
||||
log(debug, "info", "🚪 nostr: Unsubscribing from filter:", filter)
|
||||
return sub.unsub()
|
||||
}
|
||||
|
||||
setUnsubscribe(() => unsubscribeFunc)
|
||||
|
||||
sub.on("event", (event: NostrEvent) => {
|
||||
log(debug, "info", "⬇️ nostr: Received event:", event)
|
||||
onEventCallback?.(event)
|
||||
|
Reference in New Issue
Block a user