mirror of
https://github.com/t4t5/nostr-react.git
synced 2025-03-17 13:31:43 +01:00
Add onEvent param to useNostrEvents
This commit is contained in:
parent
9d7da11385
commit
0a9bd4bfd5
@ -12,6 +12,7 @@ import { uniqBy } from "./utils"
|
||||
export { dateToUnix } from "./utils"
|
||||
|
||||
type OnConnectFunc = (relay: Relay) => void
|
||||
type OnEventFunc = (event: NostrEvent) => void
|
||||
|
||||
interface NostrContextType {
|
||||
isLoading: boolean
|
||||
@ -105,11 +106,14 @@ export function useNostrEvents({ filter }: { filter: Filter }) {
|
||||
const { isLoading, onConnect, debug, connectedRelays } = useNostr()
|
||||
const [events, setEvents] = useState<NostrEvent[]>([])
|
||||
|
||||
let onEventCallback: null | OnEventFunc = null
|
||||
|
||||
onConnect((relay: Relay) => {
|
||||
const sub = relay.sub([filter], {})
|
||||
|
||||
sub.on("event", (event: NostrEvent) => {
|
||||
log(debug, "info", "⬇️ nostr: Received event:", event)
|
||||
onEventCallback?.(event)
|
||||
setEvents((_events) => {
|
||||
return [event, ..._events]
|
||||
})
|
||||
@ -124,5 +128,10 @@ export function useNostrEvents({ filter }: { filter: Filter }) {
|
||||
events: sortedEvents,
|
||||
onConnect,
|
||||
connectedRelays,
|
||||
onEvent: (_onEventCallback: OnEventFunc) => {
|
||||
if (_onEventCallback) {
|
||||
onEventCallback = _onEventCallback
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user