Struct reactivate::Reactive

source ·
pub struct Reactive<T> { /* private fields */ }
Expand description

Thread Safe Reactive Data Structure

§Examples

use reactivate::Reactive;

let r = Reactive::new("🦀");

Implementations§

source§

impl<T> Reactive<T>

source

pub fn new(value: T) -> Self

Constructs a new Reactive

§Examples
use reactivate::Reactive;

let r = Reactive::new("🦀");
source

pub fn value(&self) -> T
where T: Clone,

Returns a clone/copy of the value inside the reactive

§Examples
use reactivate::Reactive;

let r = Reactive::new(String::from("🦀"));
assert_eq!("🦀", r.value());
source

pub fn with_value(&self, f: impl FnOnce(&T))

Perform some action with the reference to the inner value.

§Examples
use reactivate::Reactive;

let r = Reactive::new(String::from("🦀"));
r.with_value(|s| println!("{}", s));
source

pub fn with(&self, f: impl FnOnce(&mut T, &mut [Box<dyn FnMut(&T) + Send>]))

All the Reactive methods acquire and release locks for each method call. It can be expensive if done repeatedly. So instead, this method will give mutable access to the internal value and observers to do as you please with them.

Generally not recommended unless you know what you are doing.

§Examples
use reactivate::Reactive;

let r = Reactive::new(10);
r.with(|val, obs| {
    *val += 11;
    for f in obs {
        f(val)
    }
});

assert_eq!(21, r.value());
source

pub fn derive<U>(&self, f: impl Fn(&T) -> U + Send + 'static) -> Reactive<U>
where T: Clone, U: Clone + PartialEq + Send + 'static,

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

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

pub fn update_unchecked(&self, f: impl FnOnce(&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

source

pub fn update_inplace_unchecked(&self, f: impl FnOnce(&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.

Prefer 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

pub fn set(&self, val: T)

Set the value inside the reactive to something new and notify all the observers by calling the added observer functions in the sequence they were added (even if the provided value is the same as the current one)

§Examples
use reactivate::Reactive;

let r = Reactive::new(10);
let d = r.derive(|val| val + 5);

r.set(20);

assert_eq!(25, d.value());
source

pub fn update(&self, f: impl FnOnce(&T) -> T)
where T: PartialEq,

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

pub fn update_inplace(&self, f: impl FnOnce(&mut T))
where T: Hash,

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.

Prefer 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());
source

pub fn notify(&self)

Notify all the observers of the current value by calling the added observer functions in the sequence they were added

§Examples
use reactivate::Reactive;
use std::sync::{Arc, Mutex};

let r: Reactive<String> = Reactive::new(String::from("🦀"));
let change_log: Arc<Mutex<Vec<String>>> = Default::default();

r.add_observer({
    let change_log = change_log.clone();
    move |val| change_log.lock().unwrap().push(val.clone())
});

r.notify();
r.notify();
r.notify();

assert_eq!(
vec![String::from("🦀"), String::from("🦀"), String::from("🦀"),],
    change_log.lock().unwrap().clone()
);

Trait Implementations§

source§

impl<T: Clone> Clone for Reactive<T>

source§

fn clone(&self) -> Reactive<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Reactive<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default> Default for Reactive<T>

source§

fn default() -> Reactive<T>

Returns the “default value” for a type. Read more
source§

impl<T> Merge for &Reactive<T>
where T: Clone + Default + Send + 'static,

§

type Output = T

source§

fn merge(self) -> Reactive<Self::Output>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Reactive<T>

§

impl<T> Send for Reactive<T>
where T: Send,

§

impl<T> Sync for Reactive<T>
where T: Send,

§

impl<T> Unpin for Reactive<T>

§

impl<T> UnwindSafe for Reactive<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.