[][src]Trait epoxy::ReactiveValue

pub trait ReactiveValue<T> {
    fn get(&self) -> Arc<T>;
fn as_stream(&self) -> Stream<T>; }

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);
Loading content...

Methods

impl<T> dyn ReactiveValue<T> + 'static where
    T: 'static, 
[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]

pub fn from_stream_with_default(
    stream: Stream<T>,
    default: T
) -> ReadonlyReactiveValue<T>
[src]

pub fn from_stream_with_default_rc(
    stream: Stream<T>,
    default: Arc<T>
) -> ReadonlyReactiveValue<T>
[src]

impl<T> dyn ReactiveValue<T> + 'static where
    T: 'static, 
[src]

pub fn sanitize<F>(
    value: &dyn ReactiveValue<T>,
    filter_function: F,
    default_value: T
) -> ReadonlyReactiveValue<T> where
    F: Fn(&T) -> bool + 'static, 
[src]

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 + 'static, 
[src]

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
    F: Fn(&T) -> U + 'static,
    U: 'static, 
[src]

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");

Implementors

impl<T> ReactiveValue<T> for ReadonlyReactiveValue<T>[src]

impl<T> ReactiveValue<T> for WriteableReactiveValue<T>[src]

Loading content...