mirror of
https://github.com/t4t5/nostr-react.git
synced 2025-03-17 13:31:43 +01:00
Working unsubscribe function
This commit is contained in:
parent
54e611e362
commit
1f5db629b8
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user