use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
use std::sync::Arc;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct Options {
wait_for_connection: Arc<AtomicBool>,
wait_for_send: Arc<AtomicBool>,
wait_for_subscription: Arc<AtomicBool>,
difficulty: Arc<AtomicU8>,
req_filters_chunk_size: Arc<AtomicU8>,
timeout: Option<Duration>,
#[cfg(feature = "nip46")]
nip46_timeout: Option<Duration>,
}
impl Default for Options {
fn default() -> Self {
Self {
wait_for_connection: Arc::new(AtomicBool::new(false)),
wait_for_send: Arc::new(AtomicBool::new(true)),
wait_for_subscription: Arc::new(AtomicBool::new(false)),
difficulty: Arc::new(AtomicU8::new(0)),
req_filters_chunk_size: Arc::new(AtomicU8::new(10)),
timeout: None,
#[cfg(feature = "nip46")]
nip46_timeout: Some(Duration::from_secs(180)),
}
}
}
impl Options {
pub fn new() -> Self {
Self::default()
}
pub fn wait_for_connection(self, wait: bool) -> Self {
Self {
wait_for_connection: Arc::new(AtomicBool::new(wait)),
..self
}
}
pub(crate) fn get_wait_for_connection(&self) -> bool {
self.wait_for_connection.load(Ordering::SeqCst)
}
pub fn wait_for_send(self, wait: bool) -> Self {
Self {
wait_for_send: Arc::new(AtomicBool::new(wait)),
..self
}
}
pub(crate) fn get_wait_for_send(&self) -> bool {
self.wait_for_send.load(Ordering::SeqCst)
}
pub fn wait_for_subscription(self, wait: bool) -> Self {
Self {
wait_for_subscription: Arc::new(AtomicBool::new(wait)),
..self
}
}
pub(crate) fn get_wait_for_subscription(&self) -> bool {
self.wait_for_subscription.load(Ordering::SeqCst)
}
pub fn difficulty(self, difficulty: u8) -> Self {
Self {
difficulty: Arc::new(AtomicU8::new(difficulty)),
..self
}
}
pub(crate) fn get_difficulty(&self) -> u8 {
self.difficulty.load(Ordering::SeqCst)
}
pub(crate) fn update_difficulty(&self, difficulty: u8) {
let _ = self
.difficulty
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |_| Some(difficulty));
}
pub fn req_filters_chunk_size(self, size: u8) -> Self {
Self {
req_filters_chunk_size: Arc::new(AtomicU8::new(size)),
..self
}
}
pub(crate) fn get_req_filters_chunk_size(&self) -> usize {
self.req_filters_chunk_size.load(Ordering::SeqCst) as usize
}
pub fn timeout(self, timeout: Option<Duration>) -> Self {
Self { timeout, ..self }
}
pub(crate) fn get_timeout(&self) -> Option<Duration> {
self.timeout
}
#[cfg(feature = "nip46")]
pub fn nip46_timeout(self, timeout: Option<Duration>) -> Self {
Self {
nip46_timeout: timeout,
..self
}
}
#[cfg(feature = "nip46")]
pub(crate) fn get_nip46_timeout(&self) -> Option<Duration> {
self.nip46_timeout
}
}