Struct reactivate::Reactive
source · pub struct Reactive<T> { /* private fields */ }Expand description
Thread Safe Reactive Data Structure using the observer pattern
Implementations§
source§impl<T> Reactive<T>
impl<T> Reactive<T>
sourcepub fn add_observer(&self, f: impl FnMut(&T) + Send + 'static)
pub fn add_observer(&self, f: impl FnMut(&T) + Send + 'static)
Adds a new observer to the reactive. the observer functions are called whenever the value inside the Reactive is updated
Examples
use reactivate::Reactive;
use std::sync::{Arc, Mutex};
let r: Reactive<String> = Reactive::default();
// Arc<Mutex<T>> is used to make the vector thread safe
// because Reactive as a whole must be thread safe
let change_log: Arc<Mutex<Vec<String>>> = Default::default();
// add an observer function to keep a log of all the updates done to the reactive.
r.add_observer({
let change_log = change_log.clone();
move |val| change_log.lock().unwrap().push(val.clone())
});
r.update(|_| String::from("🦀"));
r.update(|_| String::from("🦞"));
assert_eq!(
vec![String::from("🦀"), String::from("🦞")],
change_log.lock().unwrap().clone()
);sourcepub fn update_unchecked(&self, f: impl Fn(&T) -> T)
pub fn update_unchecked(&self, f: impl Fn(&T) -> T)
Update the value inside the reactive and notify all the observers by calling the added observer functions in the sequence they were added without checking if the value is changed after applying the provided function
Examples
use reactivate::Reactive;
let r = Reactive::new(10);
let d = r.derive(|val| val + 5);
// notifies the observers as usual because value changed from 10 to 20
r.update_unchecked(|_| 20);
assert_eq!(25, d.value());
// would still notify the observers even if the value didn't change
r.update_unchecked(|_| 20);
assert_eq!(25, d.value());Reasons to use
update_unchecked doesn’t require PartialEq trait bounds on T
because the old value and the new value (after applying f) aren’t compared.
It is also faster than update for that reason
sourcepub fn update_inplace_unchecked(&self, f: impl Fn(&mut T))
pub fn update_inplace_unchecked(&self, f: impl Fn(&mut T))
Updates the value inside inplace without creating a new clone/copy and notify all the observers by calling the added observer functions in the sequence they were added without checking if the value is changed after applying the provided function.
Perfer this when the datatype inside is expensive to clone, like a vector.
Examples
use reactivate::Reactive;
let r = Reactive::new(vec![1, 2, 3]);
let d = r.derive(|nums| nums.iter().sum::<i32>());
// notifies the observers as usual because value changed from [1, 2, 3] to [1, 2, 3, 4, 5, 6]
r.update_inplace_unchecked(|nums| {
nums.push(4);
nums.push(5);
nums.push(6);
});
assert_eq!(21, d.value());
// would still notify the observers even if the value didn't change
r.update_inplace_unchecked(|nums| {
nums.push(100);
nums.pop();
});
assert_eq!(21, d.value());Reasons to use
update_inplace_unchecked doesn’t require Hash trait bounds on T
because the hashes of old value and the new value (after applying f)
aren’t calculated and compared.
It is also faster than update_inplace for that reason
source§impl<T: Clone> Reactive<T>
impl<T: Clone> Reactive<T>
sourcepub fn value(&self) -> T
pub fn value(&self) -> T
Returns a clone/copy of the value inside the reactive
Examples
use reactivate::Reactive;
let r = Reactive::new(String::from("🦀"));
assert_eq!("🦀", r.value());sourcepub fn derive<U: Default + Clone + PartialEq + Send + 'static>(
&self,
f: impl Fn(&T) -> U + Send + 'static
) -> Reactive<U>
pub fn derive<U: Default + Clone + PartialEq + Send + 'static>( &self, f: impl Fn(&T) -> U + Send + 'static ) -> Reactive<U>
derive a new child reactive that changes whenever the parent reactive changes. (achieved by adding an observer function to the parent reactive behind the scenes)
Examples
use reactivate::Reactive;
let r = Reactive::new(10);
let d = r.derive(|val| val + 5);
assert_eq!(15, d.value());source§impl<T: PartialEq> Reactive<T>
impl<T: PartialEq> Reactive<T>
sourcepub fn update(&self, f: impl Fn(&T) -> T)
pub fn update(&self, f: impl Fn(&T) -> T)
Update the value inside the reactive and notify all the observers by calling the added observer functions in the sequence they were added ONLY if the value changes after applying the provided function
Examples
use reactivate::Reactive;
let r = Reactive::new(10);
let d = r.derive(|val| val + 5);
r.update(|_| 20);
assert_eq!(25, d.value());source§impl<T: Hash> Reactive<T>
impl<T: Hash> Reactive<T>
sourcepub fn update_inplace(&self, f: impl Fn(&mut T))
pub fn update_inplace(&self, f: impl Fn(&mut T))
Updates the value inside inplace without creating a new clone/copy and notify all the observers by calling the added observer functions in the sequence they were added ONLY if the value changes after applying the provided function.
Perfer this when the datatype inside is expensive to clone, like a vector.
Examples
use reactivate::Reactive;
let r = Reactive::new(vec![1, 2, 3]);
let d = r.derive(|nums| nums.iter().sum::<i32>());
r.update_inplace(|nums| {
nums.push(4);
nums.push(5);
nums.push(6);
});
assert_eq!(21, d.value());