Skip to main content

RxObservable

Struct RxObservable 

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

A read-only stream of events.

Unlike RxVal, RxObservable does NOT have a current value. Subscribers are only called when new events are emitted, not immediately upon subscription.

This is useful for representing discrete events like button clicks, network messages, or user actions that don’t have a “current state”.

§Example

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

let mut tracker = DisposableTracker::new();
let rx_subject = RxSubject::new();
let rx_observable = rx_subject.observable();

rx_observable.subscribe(tracker.tracker(), |value| {
    println!("Event: {}", value);
}); // Nothing printed yet

rx_subject.next(42); // Prints "Event: 42"
rx_subject.next(100); // Prints "Event: 100"

Implementations§

Source§

impl<T: 'static> RxObservable<T>

Source

pub fn subscribe<F>(&self, tracker: &Tracker, f: F)
where F: FnMut(&T) + 'static,

Subscribes to events.

The subscriber function is called each time a new event is emitted. Unlike RxVal, it is NOT called immediately upon subscription.

The subscription is automatically cleaned up when the tracker is dropped.

§Arguments
  • tracker - Tracker that will manage this subscription’s lifetime
  • f - Function called with a reference to the event on each emission
Source

pub fn subscriber_count(&self) -> usize

Returns the number of active subscribers.

Source§

impl<T: 'static> RxObservable<T>

Source

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

Converts this RxObservable to an RxVal with an initial value.

The RxVal is updated whenever the observable 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.observable().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 RxObservable using a transformation function.

Returns a new RxObservable that emits transformed values. When the source observable 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.observable().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 RxObservable using a function that returns RxVal.

When the observable 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.observable().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);
// Emits twice: once for current value, once for subscription
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. Delegates to flat_map_val by converting the RxRef to RxVal.

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. Switches to the new observable each time the source emits.

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. Delegates to flat_map_observable by converting the RxSubject to RxObservable.

Source

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

Joins this RxObservable with another RxObservable.

The resulting observable emits whenever either source emits. Both observables must have the same type.

§Arguments
  • other - Another RxObservable to join with
§Example
use rx_rs::core::{RxSubject, DisposableTracker};
use std::cell::RefCell;
use std::rc::Rc;

let tracker = DisposableTracker::new();
let subject1 = RxSubject::new();
let subject2 = RxSubject::new();

let joined = subject1.observable().join_observable(subject2.observable());

let results = Rc::new(RefCell::new(Vec::new()));
let results_clone = results.clone();

joined.subscribe(tracker.tracker(), move |value| {
    results_clone.borrow_mut().push(*value);
});

subject1.next(1);
subject2.next(2);
subject1.next(3);

assert_eq!(*results.borrow(), vec![1, 2, 3]);
Source

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

Joins this RxObservable with an RxSubject. Delegates to join_observable by converting the RxSubject to RxObservable.

Trait Implementations§

Source§

impl<T> Clone for RxObservable<T>

Source§

fn clone(&self) -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for RxObservable<T>

§

impl<T> !RefUnwindSafe for RxObservable<T>

§

impl<T> !Send for RxObservable<T>

§

impl<T> !Sync for RxObservable<T>

§

impl<T> Unpin for RxObservable<T>

§

impl<T> UnsafeUnpin for RxObservable<T>

§

impl<T> !UnwindSafe for RxObservable<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.