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>

source

pub fn new(value: T) -> Self

Constructs a new Reactive

Examples
use reactivate::Reactive;

let r = Reactive::new("🦀");
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 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

source

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>

source

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

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>

source

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>

source

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

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

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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 Twhere 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.