Skip to content

Commit

Permalink
Add onEvent param to useNostrEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
t4t5 committed Dec 25, 2022
1 parent 9d7da11 commit 0a9bd4b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
})
Expand All @@ -124,5 +128,10 @@ export function useNostrEvents({ filter }: { filter: Filter }) {
events: sortedEvents,
onConnect,
connectedRelays,
onEvent: (_onEventCallback: OnEventFunc) => {
if (_onEventCallback) {
onEventCallback = _onEventCallback
}
},
}
}

0 comments on commit 0a9bd4b

Please sign in to comment.