Skip to content

Commit 6b44a6c

Browse files
committed
ffi(sdk): convert RelayPool::handle_notifications method to async/future
No longer spawn a thread. Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent caf7a2e commit 6b44a6c

File tree

2 files changed

+57
-35
lines changed

2 files changed

+57
-35
lines changed

CHANGELOG.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@
55
<!-- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), -->
66
<!-- and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -->
77

8+
<!-- Template
9+
10+
## [Unreleased]
11+
12+
### Summary
13+
14+
### Changed
15+
16+
### Added
17+
18+
### Fixed
19+
20+
### Removed
21+
22+
-->
23+
24+
## [Unreleased]
25+
26+
### Summary
27+
28+
### Changed
29+
30+
* ffi(sdk): convert `RelayPool::handle_notifications` method to async/future
31+
32+
### Added
33+
34+
### Fixed
35+
36+
### Removed
37+
838
## [v0.32.0]
939

1040
### Summary

bindings/nostr-sdk-ffi/src/pool/mod.rs

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::ops::Deref;
77
use std::sync::Arc;
88
use std::time::Duration;
99

10-
use async_utility::thread;
1110
use nostr_ffi::{ClientMessage, Event, EventId, Filter};
1211
use nostr_sdk::database::DynNostrDatabase;
1312
use nostr_sdk::{RelayPoolOptions, SubscriptionId};
@@ -422,40 +421,33 @@ impl RelayPool {
422421
}
423422

424423
/// Handle relay pool notifications
425-
///
426-
/// <div class="warning">Python bindings needs to call `uniffi_set_event_loop(asyncio.get_running_loop())` before this method!</div>
427-
pub fn handle_notifications(
428-
self: Arc<Self>,
429-
handler: Arc<dyn HandleNotification>,
430-
) -> Result<()> {
431-
thread::spawn(async move {
432-
self.inner
433-
.handle_notifications(|notification| async {
434-
match notification {
435-
nostr_sdk::RelayPoolNotification::Message { relay_url, message } => {
436-
handler
437-
.handle_msg(relay_url.to_string(), Arc::new(message.into()))
438-
.await;
439-
}
440-
nostr_sdk::RelayPoolNotification::Event {
441-
relay_url,
442-
subscription_id,
443-
event,
444-
} => {
445-
handler
446-
.handle(
447-
relay_url.to_string(),
448-
subscription_id.to_string(),
449-
Arc::new((*event).into()),
450-
)
451-
.await;
452-
}
453-
_ => (),
424+
pub async fn handle_notifications(&self, handler: Arc<dyn HandleNotification>) -> Result<()> {
425+
Ok(self
426+
.inner
427+
.handle_notifications(|notification| async {
428+
match notification {
429+
nostr_sdk::RelayPoolNotification::Message { relay_url, message } => {
430+
handler
431+
.handle_msg(relay_url.to_string(), Arc::new(message.into()))
432+
.await;
433+
}
434+
nostr_sdk::RelayPoolNotification::Event {
435+
relay_url,
436+
subscription_id,
437+
event,
438+
} => {
439+
handler
440+
.handle(
441+
relay_url.to_string(),
442+
subscription_id.to_string(),
443+
Arc::new((*event).into()),
444+
)
445+
.await;
454446
}
455-
Ok(false)
456-
})
457-
.await
458-
})?;
459-
Ok(())
447+
_ => (),
448+
}
449+
Ok(false)
450+
})
451+
.await?)
460452
}
461453
}

0 commit comments

Comments
 (0)