pub struct RxVal<T> { /* private fields */ }Expand description
A read-only reactive value that holds a current state.
When subscribed, the subscriber is called immediately with the current value, and then called again whenever the value changes.
This is useful for representing state that always has a current value, like connection status, client ID, or configuration values.
§Example
use rx_rs::core::{RxRef, DisposableTracker};
let mut tracker = DisposableTracker::new();
let rx_ref = RxRef::new(42);
let rx_val = rx_ref.val();
rx_val.subscribe(tracker.tracker(), |value| {
println!("Value: {}", value);
}); // Prints "Value: 42" immediately
rx_ref.set(100); // Prints "Value: 100"Implementations§
Source§impl<T: 'static> RxVal<T>
impl<T: 'static> RxVal<T>
Sourcepub fn subscribe<F>(&self, tracker: &Tracker, f: F)
pub fn subscribe<F>(&self, tracker: &Tracker, f: F)
Subscribes to value changes.
The subscriber function is called immediately with the current value, and then called again whenever the value changes.
The subscription is automatically cleaned up when the tracker is dropped.
§Arguments
tracker- Tracker that will manage this subscription’s lifetimef- Function called with a reference to the value on each update
Sourcepub fn subscriber_count(&self) -> usize
pub fn subscriber_count(&self) -> usize
Returns the number of active subscribers.
Sourcepub fn debug_ptr(&self) -> usize
pub fn debug_ptr(&self) -> usize
Returns a pointer address for debugging identity. Use this to check if two RxVal instances share the same underlying data.
Sourcepub fn stream(&self) -> RxObservable<T>where
T: Clone,
pub fn stream(&self) -> RxObservable<T>where
T: Clone,
Converts this RxVal into a stream (RxObservable).
The returned observable does NOT emit the current value immediately on subscription. It only emits when the RxVal changes to a new value.
Note: If you subscribe directly to the RxVal, it WILL emit immediately. But when converted to a stream, it behaves like a pure observable.
§Example
use rx_rs::core::{RxRef, DisposableTracker};
let mut tracker = DisposableTracker::new();
let rx_ref = RxRef::new(42);
let stream = rx_ref.val().stream();
stream.subscribe(tracker.tracker(), |value| {
println!("Value: {}", value);
}); // Does NOT print immediately
rx_ref.set(100); // Prints "Value: 100"Sourcepub fn map<B, F>(&self, f: F) -> RxVal<B>
pub fn map<B, F>(&self, f: F) -> RxVal<B>
Maps the values of this RxVal using a transformation function.
Returns a new RxVal that always contains the transformed value. When the source RxVal changes, the transformation is applied and the resulting RxVal is updated.
§Arguments
f- Function to transform values from A to B
§Example
use rx_rs::core::RxRef;
let number = RxRef::new(5);
let doubled = number.val().map(|x| x * 2);
assert_eq!(doubled.get(), 10);
number.set(10);
assert_eq!(doubled.get(), 20);Sourcepub fn flat_map<B, F>(&self, f: F) -> RxVal<B>
pub fn flat_map<B, F>(&self, f: F) -> RxVal<B>
Flat-maps the values of this RxVal using a function that returns RxVal.
When the source RxVal changes, the function is called to produce a new RxVal, and the result RxVal is updated to reflect the current value of that inner RxVal. The result also updates when the inner RxVal changes.
§Arguments
f- Function to transform values from A to RxVal
§Example
use rx_rs::core::RxRef;
let outer = RxRef::new(1);
let inner1 = RxRef::new(10);
let inner2 = RxRef::new(20);
let inner1_clone = inner1.clone();
let inner2_clone = inner2.clone();
let flattened = outer.val().flat_map(move |&x| {
if x == 1 { inner1_clone.val() } else { inner2_clone.val() }
});
assert_eq!(flattened.get(), 10);
outer.set(2);
assert_eq!(flattened.get(), 20);Sourcepub fn flat_map_ref<B, F>(&self, f: F) -> RxVal<B>
pub fn flat_map_ref<B, F>(&self, f: F) -> RxVal<B>
Flat-maps using a function that returns RxRef. Delegates to flat_map by converting the RxRef to RxVal.
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. Returns an RxObservable that switches to the new observable on each change.
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. Delegates to flat_map_observable by converting the RxSubject to RxObservable.
Sourcepub fn zip_val<U>(&self, other: RxVal<U>) -> RxVal<(T, U)>
pub fn zip_val<U>(&self, other: RxVal<U>) -> RxVal<(T, U)>
Combines this RxVal with another RxVal, producing a new RxVal containing a tuple.
The resulting RxVal updates whenever either source changes.
§Arguments
other- Another RxVal to combine with
§Example
use rx_rs::core::RxRef;
let name = RxRef::new("Alice");
let age = RxRef::new(30);
let combined = name.val().zip_val(age.val());
assert_eq!(combined.get(), ("Alice", 30));
name.set("Bob");
assert_eq!(combined.get(), ("Bob", 30));