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