Skip to main content

ConnectableController

Struct ConnectableController 

Source
pub struct ConnectableController<OE, S, State = Disconnected> { /* private fields */ }
Expand description

Multicasts a source Observable through a Subject, but waits until its connect method is called before subscribing to the source and emitting items to its observers. Subscribe to the multicast output through observable. See https://reactivex.io/documentation/operators/connect.html

§Examples

use rx_rust::{
    observable::ObservableExt,
    observer::Termination,
    operators::{
        connectable::connectable_controller::ConnectableController,
        creating::from_iter::FromIter,
    },
    subject::publish_subject::PublishSubject,
};

use std::{convert::Infallible, sync::{Arc, Mutex}};

let values_1 = Arc::new(Mutex::new(Vec::new()));
let values_2 = Arc::new(Mutex::new(Vec::new()));
let terminations = Arc::new(Mutex::new(Vec::new()));

let subject: PublishSubject<'_, i32, Infallible> = PublishSubject::default();
let controller = ConnectableController::new(FromIter::new(vec![1, 2]), subject);
let observable = controller.observable();
let values_1_observer = Arc::clone(&values_1);
let values_2_observer = Arc::clone(&values_2);
let terminations_observer = Arc::clone(&terminations);

let subscription_1 = observable.clone().subscribe_with_callback(
    move |value| values_1_observer.lock().unwrap().push(value),
    |_| {},
);
let subscription_2 = observable.subscribe_with_callback(
    move |value| values_2_observer.lock().unwrap().push(value),
    move |termination| terminations_observer
        .lock()
        .unwrap()
        .push(termination),
);

// Nothing is emitted until the source is connected.
let connected = controller.connect();
// Dropping the connected controller disconnects the source.
drop(connected);
drop(subscription_1);
drop(subscription_2);

assert_eq!(&*values_1.lock().unwrap(), &[1, 2]);
assert_eq!(&*values_2.lock().unwrap(), &[1, 2]);
assert_eq!(
    &*terminations.lock().unwrap(),
    &[Termination::Completed]
);

Implementations§

Source§

impl<OE, S> ConnectableController<OE, S, Disconnected>

Source

pub fn new(source: OE, subject: S) -> Self

Source

pub fn connect<'or, T, E>( self, ) -> ConnectableController<OE, S, Connected<OE::D>>
where OE: Observable<'or, T, E> + Clone, S: Observer<T, E> + Clone + MaybeSend + 'or,

Connects to the source. The returned controller owns the connection and disconnects it when dropped.

Ignoring the returned controller would disconnect at the end of the statement, so the compiler warns about it:

#![deny(unused_must_use)]
use rx_rust::{
    observable::ObservableExt,
    operators::creating::from_iter::FromIter,
};

FromIter::new([1_i32]).publish().connect();
Source

pub fn ref_count<'or, T, E>(self) -> RefCount<'or, T, E, OE, S>
where OE: Observable<'or, T, E>, S: Clone,

Source§

impl<OE, S, D> ConnectableController<OE, S, Connected<D>>
where D: Disposable,

Source

pub fn disconnect(self) -> ConnectableController<OE, S, Disconnected>

Disconnects the source and returns the controller in its disconnected state.

Source§

impl<OE, S, State> ConnectableController<OE, S, State>

Source

pub fn observable(&self) -> SubjectObservable<S>
where S: Clone,

Trait Implementations§

Source§

impl<OE, S, State> Debug for ConnectableController<OE, S, State>
where OE: Debug, S: Debug, State: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<OE, S, State> Freeze for ConnectableController<OE, S, State>
where OE: Freeze, S: Freeze, State: Freeze,

§

impl<OE, S, State> RefUnwindSafe for ConnectableController<OE, S, State>

§

impl<OE, S, State> Send for ConnectableController<OE, S, State>
where OE: Send, S: Send, State: Send,

§

impl<OE, S, State> Sync for ConnectableController<OE, S, State>
where OE: Sync, S: Sync, State: Sync,

§

impl<OE, S, State> Unpin for ConnectableController<OE, S, State>
where OE: Unpin, S: Unpin, State: Unpin,

§

impl<OE, S, State> UnsafeUnpin for ConnectableController<OE, S, State>
where OE: UnsafeUnpin, S: UnsafeUnpin, State: UnsafeUnpin,

§

impl<OE, S, State> UnwindSafe for ConnectableController<OE, S, State>
where OE: UnwindSafe, S: UnwindSafe, State: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSend for T
where T: Send,

Source§

impl<T> MaybeSync for T
where T: Sync,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.