From aaba3db6976f9bb8e92ae7ff1075f9719f8936c1 Mon Sep 17 00:00:00 2001 From: Michael Jurkoic Date: Mon, 18 Mar 2024 20:00:17 -0500 Subject: Provide option to store events for async retrieval --- include/nostr.hpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/nostr.hpp b/include/nostr.hpp index 448ad64..1a4e33c 100644 --- a/include/nostr.hpp +++ b/include/nostr.hpp @@ -147,6 +147,8 @@ public: * @brief Queries all open relay connections for events matching the given set of filters. * @param filters The filters to use for the query. * @returns The ID of the subscription created for the query. + * @remarks The service will store a limited number of events returned from the relay for the + * given filters. These events may be retrieved via `getNewEvents`. */ std::string queryRelays(Filters filters); @@ -156,6 +158,9 @@ public: * @param responseHandler A callable object that will be invoked each time the client receives * an event matching the filters. * @returns The ID of the subscription created for the query. + * @remark By providing a response handler, the caller assumes responsibility for handling all + * events returned from the relay for the given filters. The service will not store the + * events, and they will not be accessible via `getNewEvents`. */ std::string queryRelays(Filters filters, std::function responseHandler); @@ -164,14 +169,14 @@ public: * subscriptions. * @returns A pointer to a vector of new events. */ - std::unique_ptr> getNewEvents(); + std::vector getNewEvents(); /** * @brief Get any new events received since the last call to this method, for the given * subscription. * @returns A pointer to a vector of new events. */ - std::unique_ptr> getNewEvents(std::string subscriptionId); + std::vector getNewEvents(std::string subscriptionId); /** * @brief Closes the subscription with the given ID on all open relay connections. @@ -198,12 +203,22 @@ public: std::tuple closeSubscriptions(RelayList relays); private: + ///< The maximum number of events the service will store for each subscription. + const int MAX_EVENTS_PER_SUBSCRIPTION = 128; + + ///< The WebSocket client used to communicate with relays. client::IWebSocketClient* _client; + ///< A mutex to protect the instance properties. std::mutex _propertyMutex; + ///< The default set of Nostr relays to which the service will attempt to connect. RelayList _defaultRelays; - RelayList _activeRelays; + ///< The set of Nostr relays to which the service is currently connected. + RelayList _activeRelays; + ///< A map from relay URIs to the subscription IDs open on each relay. std::unordered_map> _subscriptions; + ///< A map from subscription IDs to the events returned by the relays for each subscription. std::unordered_map> _events; + ///< A map from the subscription IDs to the latest read event for each subscription. std::unordered_map::iterator> _eventIterators; /** -- cgit