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>
impl<T: 'static> RxSubject<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new RxSubject.
§Example
use rx_rs::core::RxSubject;
let messages = RxSubject::<String>::new();Sourcepub fn observable(&self) -> RxObservable<T>
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);Sourcepub fn subscriber_count(&self) -> usize
pub fn subscriber_count(&self) -> usize
Returns the number of active subscribers.
Sourcepub fn to_val(&self, initial: T, tracker: &Tracker) -> RxVal<T>
pub fn to_val(&self, initial: T, tracker: &Tracker) -> RxVal<T>
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 RxValtracker- 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);Sourcepub fn map<B, F>(&self, f: F) -> RxObservable<B>
pub fn map<B, F>(&self, f: F) -> RxObservable<B>
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));Sourcepub fn flat_map_val<B, F>(&self, f: F) -> RxObservable<B>
pub fn flat_map_val<B, F>(&self, f: F) -> RxObservable<B>
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));Sourcepub fn flat_map_ref<B, F>(&self, f: F) -> RxObservable<B>
pub fn flat_map_ref<B, F>(&self, f: F) -> RxObservable<B>
Flat-maps using a function that returns RxRef.
Sourcepub fn flat_map_observable<B, F>(&self, f: F) -> RxObservable<B>
pub fn flat_map_observable<B, F>(&self, f: F) -> RxObservable<B>
Flat-maps using a function that returns RxObservable.
Sourcepub fn flat_map_subject<B, F>(&self, f: F) -> RxObservable<B>
pub fn flat_map_subject<B, F>(&self, f: F) -> RxObservable<B>
Flat-maps using a function that returns RxSubject.
Sourcepub fn join_observable(&self, other: RxObservable<T>) -> RxObservable<T>where
T: Clone,
pub fn join_observable(&self, other: RxObservable<T>) -> RxObservable<T>where
T: Clone,
Joins this RxSubject with an RxObservable.
Sourcepub fn join_subject(&self, other: RxSubject<T>) -> RxObservable<T>where
T: Clone,
pub fn join_subject(&self, other: RxSubject<T>) -> RxObservable<T>where
T: Clone,
Joins this RxSubject with another RxSubject.