mirror of
https://github.com/t4t5/nostr-react.git
synced 2025-03-17 13:31:43 +01:00
add onReadyCallback to the useNostrEvents hook (#11)
* add onReadyCallback to the useNostrEvents hook allow users to be notified when all events for a specific filter are transmitted * change onReady to onDone * fix typo
This commit is contained in:
parent
55d9f5f449
commit
410490bacf
11
src/core.tsx
11
src/core.tsx
@ -16,6 +16,7 @@ import { uniqBy } from "./utils"
|
||||
type OnConnectFunc = (relay: Relay) => void
|
||||
type OnDisconnectFunc = (relay: Relay) => void
|
||||
type OnEventFunc = (event: NostrEvent) => void
|
||||
type OnDoneFunc = () => void
|
||||
type OnSubscribeFunc = (sub: Sub, relay: Relay) => void
|
||||
|
||||
interface NostrContextType {
|
||||
@ -140,6 +141,7 @@ export function useNostrEvents({
|
||||
|
||||
let onEventCallback: null | OnEventFunc = null
|
||||
let onSubscribeCallback: null | OnSubscribeFunc = null
|
||||
let onDoneCallback: null | OnDoneFunc = null
|
||||
|
||||
// Lets us detect changes in the nested filter object for the useEffect hook
|
||||
const filterBase64 =
|
||||
@ -177,6 +179,10 @@ export function useNostrEvents({
|
||||
return [event, ..._events]
|
||||
})
|
||||
})
|
||||
|
||||
sub.on("eose", () => {
|
||||
onDoneCallback?.()
|
||||
})
|
||||
|
||||
return sub
|
||||
}, [])
|
||||
@ -221,5 +227,10 @@ export function useNostrEvents({
|
||||
onEventCallback = _onEventCallback
|
||||
}
|
||||
},
|
||||
onDone: (_onDoneCallback: OnDoneFunc) => {
|
||||
if (_onDoneCallback) {
|
||||
onDoneCallback = _onDoneCallback
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user