Skip to main content

RxSubject

Struct RxSubject 

Source
pub struct RxSubject<T> { /* private fields */ }
Expand description

A read-write stream of events.

RxSubject provides both read and write access to an event stream. It exposes a read-only RxObservable via the .observable() method, and allows emitting events via the .next() method.

Unlike RxRef, RxSubject does NOT hold a current value. It only emits discrete events to subscribers.

§Example

use rx_rs::core::{RxSubject, DisposableTracker};

let mut tracker = DisposableTracker::new();
let button_clicks = RxSubject::new();

// Subscribe to button click events
button_clicks.observable().subscribe(tracker.tracker(), |click_count| {
    println!("Button clicked {} times", click_count);
}); // Nothing printed yet (no current value)

// Emit events
button_clicks.next(1); // Prints "Button clicked 1 times"
button_clicks.next(2); // Prints "Button clicked 2 times"

Implementations§

Source§

impl<T: 'static> RxSubject<T>

Source

pub fn new() -> Self

Creates a new RxSubject.

§Example
use rx_rs::core::RxSubject;

let messages = RxSubject::<String>::new();
Source

pub fn next(&self, value: T)

Emits a new event to all subscribers.

All subscribers to the RxObservable obtained via .observable() will be called with the event.

§Arguments
  • value - The event to emit
§Example
use rx_rs::core::RxSubject;

let events = RxSubject::new();
events.next("click");
events.next("hover");
Source

pub fn observable(&self) -> RxObservable<T>

Returns a read-only view of this event stream.

The returned RxObservable can be cloned and passed around, allowing multiple parts of the code to subscribe to events without having write access.

§Example
use rx_rs::core::{RxSubject, DisposableTracker};

let mut tracker = DisposableTracker::new();
let events = RxSubject::new();
let read_only = events.observable();

read_only.subscribe(tracker.tracker(), |event| {
    println!("Event: {}", event);
});

events.next(42);
Source

pub fn subscriber_count(&self) -> usize

Returns the number of active subscribers.

Source

pub fn to_val(&self, initial: T, tracker: &Tracker) -> RxVal<T>
where T: Clone + PartialEq,

Converts this RxSubject to an RxVal with an initial value.

The RxVal is updated whenever the subject emits a new value. A tracker must be provided to manage the subscription lifetime.

§Arguments
  • initial - The initial value for the RxVal
  • tracker - Tracker to manage the subscription lifetime
§Example
use rx_rs::core::{RxSubject, DisposableTracker};

let mut tracker = DisposableTracker::new();
let subject = RxSubject::new();
let val = subject.to_val(0, tracker.tracker());

assert_eq!(val.get(), 0);

subject.next(42);
assert_eq!(val.get(), 42);
Source

pub fn map<B, F>(&self, f: F) -> RxObservable<B>
where B: Clone + 'static, F: Fn(&T) -> B + 'static,

Maps the values of this RxSubject using a transformation function.

Returns a new RxObservable that emits transformed values. When the source subject emits, the transformation is applied and the resulting observable emits the transformed value.

§Arguments
  • f - Function to transform values from A to B
§Example
use rx_rs::core::{RxSubject, DisposableTracker};
use std::cell::RefCell;
use std::rc::Rc;

let tracker = DisposableTracker::new();
let subject = RxSubject::new();
let doubled = subject.map(|x| x * 2);

let result = Rc::new(RefCell::new(None));
let result_clone = result.clone();

doubled.subscribe(tracker.tracker(), move |value| {
    *result_clone.borrow_mut() = Some(*value);
});

subject.next(5);
assert_eq!(*result.borrow(), Some(10));
Source

pub fn flat_map_val<B, F>(&self, f: F) -> RxObservable<B>
where B: Clone + PartialEq + 'static, F: Fn(&T) -> RxVal<B> + 'static,

Flat-maps the values of this RxSubject using a function that returns RxVal.

When the subject emits, the function is called to get an RxVal, and the resulting observable emits the current value of that RxVal.

§Arguments
  • f - Function to transform values from A to RxVal
§Example
use rx_rs::core::{RxSubject, RxRef, DisposableTracker};
use std::cell::RefCell;
use std::rc::Rc;

let tracker = DisposableTracker::new();
let subject = RxSubject::new();
let inner = RxRef::new(100);

let inner_clone = inner.clone();
let flattened = subject.flat_map_val(move |_| inner_clone.val());

let result = Rc::new(RefCell::new(None));
let result_clone = result.clone();

flattened.subscribe(tracker.tracker(), move |value| {
    *result_clone.borrow_mut() = Some(*value);
});

subject.next(1);
assert_eq!(*result.borrow(), Some(100));
Source

pub fn flat_map_ref<B, F>(&self, f: F) -> RxObservable<B>
where B: Clone + PartialEq + 'static, F: Fn(&T) -> RxRef<B> + 'static,

Flat-maps using a function that returns RxRef.

Source

pub fn flat_map_observable<B, F>(&self, f: F) -> RxObservable<B>
where B: Clone + 'static, F: Fn(&T) -> RxObservable<B> + 'static,

Flat-maps using a function that returns RxObservable.

Source

pub fn flat_map_subject<B, F>(&self, f: F) -> RxObservable<B>
where B: Clone + 'static, F: Fn(&T) -> RxSubject<B> + 'static,

Flat-maps using a function that returns RxSubject.

Source

pub fn join_observable(&self, other: RxObservable<T>) -> RxObservable<T>
where T: Clone,

Joins this RxSubject with an RxObservable.

Source

pub fn join_subject(&self, other: RxSubject<T>) -> RxObservable<T>
where T: Clone,

Joins this RxSubject with another RxSubject.

Trait Implementations§

Source§

impl<T: Clone> Clone for RxSubject<T>

Source§

fn clone(&self) -> RxSubject<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: 'static> Default for RxSubject<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RxSubject<T>

§

impl<T> !RefUnwindSafe for RxSubject<T>

§

impl<T> !Send for RxSubject<T>

§

impl<T> !Sync for RxSubject<T>

§

impl<T> Unpin for RxSubject<T>

§

impl<T> UnsafeUnpin for RxSubject<T>

§

impl<T> !UnwindSafe for RxSubject<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.