[−][src]Trait epoxy_streams::ReactiveValue
Trait that applies to both readonly and writeable reactive values.
Required methods
fn get(&self) -> Arc<T>
Returns the current value of the ReactiveValue.
fn as_stream(&self) -> Stream<T>
Returns a Stream that represents the changing value over time. Use this function to subscribe to changes in the ReactiveValue.ReadonlyReactiveValue
Examples
use std::sync::{Arc, Mutex}; use epoxy_streams::ReactiveValue; let value = ReactiveValue::new(4); let last_value = Arc::new(Mutex::new(0_i32)); let last_value_write = last_value.clone(); let subscription = value.as_stream().subscribe(move |val| { *last_value_write.lock().unwrap() = *val; }); value.set(1); assert_eq!(*last_value.lock().unwrap(), 1); value.set(100); assert_eq!(*last_value.lock().unwrap(), 100);
Methods
impl<T: 'static> dyn ReactiveValue<T>
[src]
pub fn new(initial_value: T) -> WriteableReactiveValue<T>
[src]
Creates a new writeable reactive value.
Note: Use new_rc
if your default value is already an Arc, as this will
prevent another pointer from being created unnecessarily.
Examples
use epoxy_streams::ReactiveValue; let writeable_value = ReactiveValue::new(5); assert_eq!(*writeable_value.get(), 5); writeable_value.set(50); assert_eq!(*writeable_value.get(), 50);
pub fn new_rc(initial_value: Arc<T>) -> WriteableReactiveValue<T>
[src]
See docs for new
pub fn from_stream(stream: Stream<T>) -> ReadonlyReactiveValue<T> where
T: Default,
[src]
T: Default,
pub fn from_stream_with_default(
stream: Stream<T>,
default: T
) -> ReadonlyReactiveValue<T>
[src]
stream: Stream<T>,
default: T
) -> ReadonlyReactiveValue<T>
pub fn from_stream_with_default_rc(
stream: Stream<T>,
default: Arc<T>
) -> ReadonlyReactiveValue<T>
[src]
stream: Stream<T>,
default: Arc<T>
) -> ReadonlyReactiveValue<T>
impl<T: 'static> dyn ReactiveValue<T>
[src]
pub fn sanitize<F>(
value: &dyn ReactiveValue<T>,
filter_function: F,
default_value: T
) -> ReadonlyReactiveValue<T> where
F: Fn(&T) -> bool,
F: 'static,
[src]
value: &dyn ReactiveValue<T>,
filter_function: F,
default_value: T
) -> ReadonlyReactiveValue<T> where
F: Fn(&T) -> bool,
F: 'static,
Returns a ReactiveValue that only changes when the content of the original ReactiveValue
passes a test (specified by filter_function
). The output ReactiveValue will be equal
to default_value
until the original ReactiveValue passes the test.
Note: This function is roughly equivalent to the filter
operator on Streams, but
is renamed to clarify its behavior.
Examples
use epoxy_streams::ReactiveValue; let original = ReactiveValue::new(1_i32); let only_even = ReactiveValue::sanitize(&original, |val| val % 2 == 0, 0); assert_eq!(*only_even.get(), 0); original.set(2); assert_eq!(*only_even.get(), 2); original.set(3); assert_eq!(*only_even.get(), 2); original.set(4); assert_eq!(*only_even.get(), 4);
pub fn fallback<F>(
value: &dyn ReactiveValue<T>,
filter_function: F,
fallback_value: T
) -> ReadonlyReactiveValue<T> where
F: Fn(&T) -> bool,
F: 'static,
[src]
value: &dyn ReactiveValue<T>,
filter_function: F,
fallback_value: T
) -> ReadonlyReactiveValue<T> where
F: Fn(&T) -> bool,
F: 'static,
Returns a ReactiveValue that reverts to a given value when the filter test fails.
Examples
use epoxy_streams::ReactiveValue; let original = ReactiveValue::new(1_i32); let only_even = ReactiveValue::sanitize(&original, |val| val % 2 == 0, 0); assert_eq!(*only_even.get(), 0); original.set(2); assert_eq!(*only_even.get(), 2); original.set(3); assert_eq!(*only_even.get(), 2); original.set(4); assert_eq!(*only_even.get(), 4);
pub fn map<U, F>(
value: &dyn ReactiveValue<T>,
map_function: F
) -> ReadonlyReactiveValue<U> where
U: 'static,
F: Fn(&T) -> U,
F: 'static,
[src]
value: &dyn ReactiveValue<T>,
map_function: F
) -> ReadonlyReactiveValue<U> where
U: 'static,
F: Fn(&T) -> U,
F: 'static,
Returns a ReactiveValue whose content is the result of running the content of the original ReactiveValue through a mapping function.
Examples
use epoxy_streams::ReactiveValue; let original = ReactiveValue::new("Bread"); let thing_that_is_cool = ReactiveValue::map(&original, |str| { format!("{} is cool", str) }); assert_eq!(*thing_that_is_cool.get(), "Bread is cool"); original.set("Cheese"); assert_eq!(*thing_that_is_cool.get(), "Cheese is cool");