Skip to content

Commit 1371314

Browse files
committed
sdk: add count_events_of method to Relay
1 parent b309592 commit 1371314

File tree

1 file changed

+39
-0
lines changed
  • crates/nostr-sdk/src/relay

1 file changed

+39
-0
lines changed

crates/nostr-sdk/src/relay/mod.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,45 @@ impl Relay {
15591559
});
15601560
}
15611561

1562+
/// Count events of filters
1563+
pub async fn count_events_of(
1564+
&self,
1565+
filters: Vec<Filter>,
1566+
timeout: Duration,
1567+
) -> Result<usize, Error> {
1568+
let id = SubscriptionId::generate();
1569+
self.send_msg(ClientMessage::new_count(id.clone(), filters), None)
1570+
.await?;
1571+
1572+
let mut count = 0;
1573+
1574+
let mut notifications = self.notification_sender.subscribe();
1575+
time::timeout(Some(timeout), async {
1576+
while let Ok(notification) = notifications.recv().await {
1577+
if let RelayPoolNotification::Message(
1578+
url,
1579+
RelayMessage::Count {
1580+
subscription_id,
1581+
count: c,
1582+
},
1583+
) = notification
1584+
{
1585+
if subscription_id == id && url == self.url {
1586+
count = c;
1587+
break;
1588+
}
1589+
}
1590+
}
1591+
})
1592+
.await
1593+
.ok_or(Error::Timeout)?;
1594+
1595+
// Unsubscribe
1596+
self.send_msg(ClientMessage::close(id), None).await?;
1597+
1598+
Ok(count)
1599+
}
1600+
15621601
/// Negentropy reconciliation
15631602
pub async fn reconcilie(
15641603
&self,

0 commit comments

Comments
 (0)