ReactiveValue

Trait ReactiveValue 

Source
pub trait ReactiveValue<T> {
    // Required methods
    fn get(&self) -> Arc<T>;
    fn as_stream(&self) -> Stream<T>;
}
Expand description

Trait that applies to both readonly and writeable reactive values.

Required Methods§

Source

fn get(&self) -> Arc<T>

Returns the current value of the ReactiveValue.

Source

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

Implementations§

Source§

impl<T> dyn ReactiveValue<T>
where T: 'static,

Source

pub fn new(initial_value: T) -> WriteableReactiveValue<T>

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);
Examples found in repository?
examples/reactive_scores.rs (line 7)
6fn main() {
7    let player_1_points = epoxy::ReactiveValue::new(4);
8    let player_1_multiplier = epoxy::ReactiveValue::new(1.0_f32);
9
10    let player_2_points = epoxy::ReactiveValue::new(5);
11    let player_2_multiplier = epoxy::ReactiveValue::new(1.0_f32);
12
13    let player_1_score = computed!(*player_1_points as f32 * player_1_multiplier);
14    let player_2_score = computed!(*player_2_points as f32 * player_2_multiplier);
15
16    let winner = computed! {
17        if (player_1_score == player_2_score) {
18            "Tie"
19        } else if (player_1_score > player_2_score) {
20            "Player 1"
21        } else {
22            "Player 2"
23        }
24    };
25
26    assert_eq!(*player_1_score.get(), 4_f32);
27    assert_eq!(*player_2_score.get(), 5_f32);
28    assert_eq!(*winner.get(), "Player 2");
29
30    player_1_multiplier.set(2.5_f32);
31
32    assert_eq!(*player_1_score.get(), 10_f32);
33    assert_eq!(*player_2_score.get(), 5_f32);
34    assert_eq!(*winner.get(), "Player 1");
35
36    player_2_points.set(9);
37
38    assert_eq!(*player_1_score.get(), 10_f32);
39    assert_eq!(*player_2_score.get(), 9_f32);
40    assert_eq!(*winner.get(), "Player 1");
41
42    player_2_points.set(10);
43
44    assert_eq!(*player_1_score.get(), 10_f32);
45    assert_eq!(*player_2_score.get(), 10_f32);
46    assert_eq!(*winner.get(), "Tie");
47}
Source

pub fn new_rc(initial_value: Arc<T>) -> WriteableReactiveValue<T>

See docs for new

Source

pub fn from_stream(stream: Stream<T>) -> ReadonlyReactiveValue<T>
where T: Default,

Source

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

Source

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

Source§

impl<T> dyn ReactiveValue<T>
where T: 'static,

Source

pub fn sanitize<F>( value: &dyn ReactiveValue<T>, filter_function: F, default_value: T, ) -> ReadonlyReactiveValue<T>
where F: Fn(&T) -> bool + '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);
Source

pub fn fallback<F>( value: &dyn ReactiveValue<T>, filter_function: F, fallback_value: T, ) -> ReadonlyReactiveValue<T>
where F: Fn(&T) -> bool + '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);
Source

pub fn map<U, F>( value: &dyn ReactiveValue<T>, map_function: F, ) -> ReadonlyReactiveValue<U>
where U: 'static, F: Fn(&T) -> U + '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");

Implementors§