use crate::pubnub::PubNub;
use crate::runtime::Runtime;
use crate::subscription::subscribe_loop::ExitTx as SubscribeLoopExitTx;
use crate::subscription::subscribe_loop_supervisor::{
SubscribeLoopSupervisor, SubscribeLoopSupervisorParams,
};
use crate::transport::Transport;
use futures_util::lock::Mutex;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct Builder<TTransport = (), TRuntime = ()> {
transport: TTransport,
runtime: TRuntime,
subscribe_loop_exit_tx: Option<SubscribeLoopExitTx>,
}
impl<TTransport, TRuntime> Builder<TTransport, TRuntime>
where
TTransport: Transport,
TRuntime: Runtime,
{
#[must_use]
pub fn build(self) -> PubNub<TTransport, TRuntime> {
let Self {
transport,
runtime,
subscribe_loop_exit_tx,
} = self;
let subscribe_loop_supervisor_params = SubscribeLoopSupervisorParams {
exit_tx: subscribe_loop_exit_tx,
};
PubNub {
transport,
runtime,
subscribe_loop_supervisor: Arc::new(Mutex::new(SubscribeLoopSupervisor::new(
subscribe_loop_supervisor_params,
))),
}
}
}
impl<TTransport, TRuntime> Builder<TTransport, TRuntime> {
#[must_use]
pub fn with_components(transport: TTransport, runtime: TRuntime) -> Self {
Self {
subscribe_loop_exit_tx: None,
transport,
runtime,
}
}
#[must_use]
pub fn subscribe_loop_exit_tx(mut self, tx: SubscribeLoopExitTx) -> Self {
self.subscribe_loop_exit_tx = Some(tx);
self
}
#[must_use]
pub fn transport<U: Transport>(self, transport: U) -> Builder<U, TRuntime> {
Builder {
transport,
runtime: self.runtime,
subscribe_loop_exit_tx: self.subscribe_loop_exit_tx,
}
}
#[must_use]
pub fn runtime<U: Runtime>(self, runtime: U) -> Builder<TTransport, U> {
Builder {
runtime,
transport: self.transport,
subscribe_loop_exit_tx: self.subscribe_loop_exit_tx,
}
}
}
impl Builder<(), ()> {
#[must_use]
pub fn new() -> Self {
Self::with_components((), ())
}
}
impl<TTransport, TRuntime> Default for Builder<TTransport, TRuntime>
where
TTransport: Default,
TRuntime: Default,
{
#[must_use]
fn default() -> Self {
Self::with_components(TTransport::default(), TRuntime::default())
}
}