From ecc502a5c15a29a9928c8ec462883774bfc9f35a Mon Sep 17 00:00:00 2001 From: Michael Jurkoic Date: Mon, 25 Mar 2024 08:07:38 -0500 Subject: Use shared pointers for filters and events --- src/nostr_service.cpp | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'src/nostr_service.cpp') diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index c7e3158..73ce95e 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -20,6 +20,7 @@ using std::find_if; using std::function; using std::future; using std::lock_guard; +using std::make_shared; using std::make_tuple; using std::move; using std::mutex; @@ -172,16 +173,18 @@ tuple NostrService::publishEvent(shared_ptr event) return make_tuple(successfulRelays, failedRelays); }; -string NostrService::queryRelays(Filters filters) +string NostrService::queryRelays(shared_ptr filters) { - return this->queryRelays(filters, [this](string subscriptionId, Event event) { + return this->queryRelays(filters, [this](string subscriptionId, shared_ptr event) { lock_guard lock(this->_propertyMutex); - this->_lastRead[subscriptionId] = event.id; + this->_lastRead[subscriptionId] = event->id; this->onEvent(subscriptionId, event); }); }; -string NostrService::queryRelays(Filters filters, function responseHandler) +string NostrService::queryRelays( + shared_ptr filters, + function)> responseHandler) { RelayList successfulRelays; RelayList failedRelays; @@ -192,7 +195,7 @@ string NostrService::queryRelays(Filters filters, function lock(this->_propertyMutex); this->_subscriptions[relay].push_back(subscriptionId); - string request = filters.serialize(subscriptionId); + string request = filters->serialize(subscriptionId); future> requestFuture = async([this, &relay, &request]() { return this->_client->send(request, relay); @@ -224,20 +227,20 @@ string NostrService::queryRelays(Filters filters, function NostrService::getNewEvents() +vector> NostrService::getNewEvents() { - vector newEvents; + vector> newEvents; for (auto& [subscriptionId, events] : this->_events) { - vector subscriptionEvents = this->getNewEvents(subscriptionId); + vector> subscriptionEvents = this->getNewEvents(subscriptionId); newEvents.insert(newEvents.end(), subscriptionEvents.begin(), subscriptionEvents.end()); } return newEvents; }; -vector NostrService::getNewEvents(string subscriptionId) +vector> NostrService::getNewEvents(string subscriptionId) { if (this->_events.find(subscriptionId) == this->_events.end()) { @@ -252,13 +255,13 @@ vector NostrService::getNewEvents(string subscriptionId) } lock_guard lock(this->_propertyMutex); - vector newEvents; - vector receivedEvents = this->_events[subscriptionId]; - vector::iterator eventIt = find_if( + vector> newEvents; + vector> receivedEvents = this->_events[subscriptionId]; + vector>::iterator eventIt = find_if( receivedEvents.begin(), receivedEvents.end(), - [this,subscriptionId](Event event) { - return event.id == this->_lastRead[subscriptionId]; + [this,subscriptionId](shared_ptr event) { + return event->id == this->_lastRead[subscriptionId]; }) + 1; while (eventIt != receivedEvents.end()) @@ -480,7 +483,9 @@ bool NostrService::hasSubscription(string relay, string subscriptionId) return false; }; -void NostrService::onMessage(string message, function eventHandler) +void NostrService::onMessage( + string message, + function)> eventHandler) { json jarr = json::array(); jarr = json::parse(message); @@ -497,10 +502,10 @@ void NostrService::onMessage(string message, function event) { lock_guard lock(this->_propertyMutex); - this->_events[subscriptionId].push_back(event); + this->_events[subscriptionId].push_back(move(event)); PLOG_INFO << "Received event for subscription: " << subscriptionId; // To protect memory, only keep a limited number of events per subscription. @@ -510,8 +515,8 @@ void NostrService::onEvent(string subscriptionId, Event event) auto eventIt = find_if( events.begin(), events.end(), - [this, subscriptionId](Event event) { - return event.id == this->_lastRead[subscriptionId]; + [this, subscriptionId](shared_ptr event) { + return event->id == this->_lastRead[subscriptionId]; }); if (eventIt == events.begin()) @@ -519,7 +524,7 @@ void NostrService::onEvent(string subscriptionId, Event event) eventIt++; } - this->_lastRead[subscriptionId] = eventIt->id; + this->_lastRead[subscriptionId] = (*eventIt)->id; _events[subscriptionId].erase(_events[subscriptionId].begin()); } }; -- cgit