Skip to content

Commit baef37e

Browse files
committed
nostr-sdk: add wait_for_subscription option
1 parent 0c765f8 commit baef37e

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,15 @@ impl Client {
458458
/// ```
459459
pub async fn subscribe(&self, filters: Vec<Filter>) {
460460
self.pool
461-
.subscribe(filters, self.opts.get_wait_for_send())
461+
.subscribe(filters, self.opts.get_wait_for_subscription())
462462
.await;
463463
}
464464

465465
/// Unsubscribe
466466
pub async fn unsubscribe(&self) {
467-
self.pool.unsubscribe(self.opts.get_wait_for_send()).await;
467+
self.pool
468+
.unsubscribe(self.opts.get_wait_for_subscription())
469+
.await;
468470
}
469471

470472
/// Get events of filters

crates/nostr-sdk/src/client/options.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub struct Options {
1414
wait_for_connection: Arc<AtomicBool>,
1515
/// Wait for the msg to be sent (default: true)
1616
wait_for_send: Arc<AtomicBool>,
17+
/// Wait for the subscription msg to be sent (default: false)
18+
wait_for_subscription: Arc<AtomicBool>,
1719
/// POW difficulty for all events (default: 0)
1820
difficulty: Arc<AtomicU8>,
1921
/// REQ filters chunk size (default: 10)
@@ -30,6 +32,7 @@ impl Default for Options {
3032
Self {
3133
wait_for_connection: Arc::new(AtomicBool::new(false)),
3234
wait_for_send: Arc::new(AtomicBool::new(true)),
35+
wait_for_subscription: Arc::new(AtomicBool::new(false)),
3336
difficulty: Arc::new(AtomicU8::new(0)),
3437
req_filters_chunk_size: Arc::new(AtomicU8::new(10)),
3538
timeout: None,
@@ -69,6 +72,18 @@ impl Options {
6972
self.wait_for_send.load(Ordering::SeqCst)
7073
}
7174

75+
/// If set to `true`, `Client` wait that a subscription msg is sent before continue (`subscribe` and `unsubscribe` methods)
76+
pub fn wait_for_subscription(self, wait: bool) -> Self {
77+
Self {
78+
wait_for_subscription: Arc::new(AtomicBool::new(wait)),
79+
..self
80+
}
81+
}
82+
83+
pub(crate) fn get_wait_for_subscription(&self) -> bool {
84+
self.wait_for_subscription.load(Ordering::SeqCst)
85+
}
86+
7287
/// Set default POW diffficulty for `Event`
7388
pub fn difficulty(self, difficulty: u8) -> Self {
7489
Self {

0 commit comments

Comments
 (0)