From 410490bacfdee8d0bc153df5ddc4006747977f5e Mon Sep 17 00:00:00 2001 From: Sebastiaan Wouters <24827662+SebastiaanWouters@users.noreply.github.com> Date: Tue, 17 Jan 2023 10:47:28 +0100 Subject: [PATCH] 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 --- src/core.tsx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/core.tsx b/src/core.tsx index c5e98f6..730dfc1 100644 --- a/src/core.tsx +++ b/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 + } + }, } }