What is Nostr?
Yuki Kishimoto
npub1drv…seet
2024-11-05 22:17:55
in reply to nevent1q…nsyd

Yuki Kishimoto on Nostr: Ahh ok. I'll think about that. But it's pretty easy to write a blocking wrapper, ...

Ahh ok. I'll think about that. But it's pretty easy to write a blocking wrapper, including only the stuff you need. This could be a solution for your above request:

```toml
[dependencies]
nostr-sdk = "0.36"
tokio = { version = "1.41", features = ["full"] }
```

```rust
use std::sync::LazyLock;

use nostr_sdk::prelude::*;
use tokio::runtime::Runtime;

static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| Runtime::new().unwrap());

pub struct ClientBlocking {
inner: Client,
}

impl ClientBlocking {
pub fn add_relay(&self, url: &str) -> Result<bool> {
RUNTIME.block_on(async {
Ok(self.inner.add_relay(url).await?)
})
}

pub fn connect(&self) {
RUNTIME.block_on(async {
self.inner.connect().await
})
}

pub fn send_event(&self, event: Event) -> Result<Output<EventId>> {
RUNTIME.block_on(async {
Ok(self.inner.send_event(event).await?)
})
}

pub fn subscribe(&self, filters: Vec<Filter>) -> Result<Output<SubscriptionId>> {
RUNTIME.block_on(async {
Ok(self.inner.subscribe(filters, None).await?)
})
}

pub fn handle_notifications<F>(&self, func: F) -> Result<()>
where
F: Fn(RelayPoolNotification) -> Result<bool>,
{
let mut notifications = self.inner.notifications();
while let Ok(notification) = RUNTIME.block_on(notifications.recv()) {
let shutdown: bool = RelayPoolNotification::Shutdown == notification;
let exit: bool = func(notification)?;
if exit || shutdown {
break;
}
}
Ok(())
}
}
```
Author Public Key
npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet