mirror of
https://github.com/t4t5/nostr-react.git
synced 2025-05-04 19:10:15 +02:00
Add onSubscribe callback
This commit is contained in:
parent
50f67a0333
commit
f572ae9cdc
@ -16,6 +16,7 @@ export { dateToUnix } from "./utils"
|
|||||||
type OnConnectFunc = (relay: Relay) => void
|
type OnConnectFunc = (relay: Relay) => void
|
||||||
type OnDisconnectFunc = (relay: Relay) => void
|
type OnDisconnectFunc = (relay: Relay) => void
|
||||||
type OnEventFunc = (event: NostrEvent) => void
|
type OnEventFunc = (event: NostrEvent) => void
|
||||||
|
type OnSubscribeFunc = (sub: Sub, relay: Relay) => void
|
||||||
|
|
||||||
interface NostrContextType {
|
interface NostrContextType {
|
||||||
isLoading: boolean
|
isLoading: boolean
|
||||||
@ -138,6 +139,7 @@ export function useNostrEvents({
|
|||||||
})
|
})
|
||||||
|
|
||||||
let onEventCallback: null | OnEventFunc = null
|
let onEventCallback: null | OnEventFunc = null
|
||||||
|
let onSubscribeCallback: null | OnSubscribeFunc = null
|
||||||
|
|
||||||
// Lets us detect changes in the nested filter object for the useEffect hook
|
// Lets us detect changes in the nested filter object for the useEffect hook
|
||||||
const filterBase64 =
|
const filterBase64 =
|
||||||
@ -183,8 +185,12 @@ export function useNostrEvents({
|
|||||||
if (!enabled) return
|
if (!enabled) return
|
||||||
|
|
||||||
const relaySubs = connectedRelays.map((relay) => {
|
const relaySubs = connectedRelays.map((relay) => {
|
||||||
|
const sub = subscribe(relay, filter)
|
||||||
|
|
||||||
|
onSubscribeCallback?.(sub, relay)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sub: subscribe(relay, filter),
|
sub,
|
||||||
relay,
|
relay,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -205,6 +211,11 @@ export function useNostrEvents({
|
|||||||
onConnect,
|
onConnect,
|
||||||
connectedRelays,
|
connectedRelays,
|
||||||
unsubscribe,
|
unsubscribe,
|
||||||
|
onSubscribe: (_onSubscribeCallback: OnSubscribeFunc) => {
|
||||||
|
if (_onSubscribeCallback) {
|
||||||
|
onSubscribeCallback = _onSubscribeCallback
|
||||||
|
}
|
||||||
|
},
|
||||||
onEvent: (_onEventCallback: OnEventFunc) => {
|
onEvent: (_onEventCallback: OnEventFunc) => {
|
||||||
if (_onEventCallback) {
|
if (_onEventCallback) {
|
||||||
onEventCallback = _onEventCallback
|
onEventCallback = _onEventCallback
|
||||||
|
Loading…
x
Reference in New Issue
Block a user