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:
Sebastiaan Wouters 2023-01-17 10:47:28 +01:00 committed by GitHub
parent 55d9f5f449
commit 410490bacf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}
},
}
}