Skip to content

Commit

Permalink
sdk: add ClientBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Oct 17, 2023
1 parent 489a90d commit 272c0d6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
60 changes: 60 additions & 0 deletions crates/nostr-sdk/src/client/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Distributed under the MIT software license

//! Client builder
use std::sync::Arc;

use nostr::Keys;
use nostr_sdk_db::memory::MemoryDatabase;
use nostr_sdk_db::DynNostrDatabase;

#[cfg(feature = "nip46")]
use super::RemoteSigner;
use crate::{Client, Options};

/// Client builder
pub struct ClientBuilder {
pub(super) keys: Keys,
pub(super) database: Arc<DynNostrDatabase>,
pub(super) opts: Options,
#[cfg(feature = "nip46")]
pub(super) remote_signer: Option<RemoteSigner>,
}

impl ClientBuilder {
/// New client builder
pub fn new(keys: &Keys) -> Self {
Self {
keys: keys.clone(),
database: Arc::new(MemoryDatabase::new()),
opts: Options::default(),
#[cfg(feature = "nip46")]
remote_signer: None,
}
}

/// Set database
pub fn database(mut self, database: Arc<DynNostrDatabase>) -> Self {
self.database = database;
self
}

/// Set opts
pub fn opts(mut self, opts: Options) -> Self {
self.opts = opts;
self
}

/// Set remote signer
#[cfg(feature = "nip46")]
pub fn remote_signer(mut self, remote_signer: RemoteSigner) -> Self {
self.remote_signer = Some(remote_signer);
self
}

/// Build [`Client`]
pub fn build(self) -> Client {
Client::from_builder(self)
}
}
28 changes: 16 additions & 12 deletions crates/nostr-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ use tokio::sync::{broadcast, RwLock};

#[cfg(feature = "blocking")]
pub mod blocking;
pub mod builder;
pub mod options;
#[cfg(feature = "nip46")]
pub mod signer;

pub use self::builder::ClientBuilder;
pub use self::options::Options;
#[cfg(feature = "nip46")]
pub use self::signer::remote::RemoteSigner;
Expand Down Expand Up @@ -165,14 +167,7 @@ impl Client {
/// let client = Client::with_opts(&my_keys, opts);
/// ```
pub fn with_opts(keys: &Keys, opts: Options) -> Self {
Self {
pool: RelayPool::new(opts.pool),
keys: Arc::new(RwLock::new(keys.clone())),
opts,
dropped: Arc::new(AtomicBool::new(false)),
#[cfg(feature = "nip46")]
remote_signer: None,
}
ClientBuilder::new(keys).opts(opts).build()
}

/// Create a new NIP46 Client
Expand All @@ -188,12 +183,21 @@ impl Client {
remote_signer: RemoteSigner,
opts: Options,
) -> Self {
ClientBuilder::new(app_keys)
.remote_signer(remote_signer)
.opts(opts)
.build()
}

/// Compose [`Client`] from [`ClientBuilder`]
pub fn from_builder(builder: ClientBuilder) -> Self {
Self {
pool: RelayPool::new(opts.pool),
keys: Arc::new(RwLock::new(app_keys.clone())),
opts,
pool: RelayPool::with_database(builder.opts.pool, builder.database),
keys: Arc::new(RwLock::new(builder.keys)),
opts: builder.opts,
dropped: Arc::new(AtomicBool::new(false)),
remote_signer: Some(remote_signer),
#[cfg(feature = "nip46")]
remote_signer: builder.remote_signer,
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod util;

#[cfg(feature = "blocking")]
pub use self::client::blocking;
pub use self::client::{Client, Options};
pub use self::client::{Client, ClientBuilder, Options};
pub use self::relay::{
ActiveSubscription, FilterOptions, InternalSubscriptionId, Relay, RelayConnectionStats,
RelayOptions, RelayPoolNotification, RelayPoolOptions, RelaySendOptions, RelayStatus,
Expand Down

0 comments on commit 272c0d6

Please sign in to comment.