use alloc::boxed::Box;
use alloc::string::ToString;
use core::fmt;
use crate::state::{self, StateAtomic, StateGeneric, StateMutex, StateTrait};
pub trait Value {
fn bind_func(&self, func: Box<dyn FnMut(&str)>);
}
impl<T> Value for T
where
T: fmt::Display,
{
fn bind_func(&self, mut func: Box<dyn FnMut(&str)>) {
func(self.to_string().as_str());
}
}
impl<T> Value for StateAtomic<T>
where
T: Copy + fmt::Display,
{
fn bind_func(&self, mut func: Box<dyn FnMut(&str)>) {
func(&self.value().to_string());
wasm_bindgen_futures::spawn_local(state::state_change(func, self.rx()));
}
}
impl<T> Value for StateMutex<T>
where
T: Clone + fmt::Display,
{
fn bind_func(&self, mut func: Box<dyn FnMut(&str)>) {
func(&self.value().to_string());
wasm_bindgen_futures::spawn_local(state::state_change(func, self.rx()));
}
}
impl<T, D> Value for StateGeneric<T, D>
where
D: Clone + fmt::Display,
{
fn bind_func(&self, mut func: Box<dyn FnMut(&str)>) {
func(&self.value().to_string());
wasm_bindgen_futures::spawn_local(state::state_change(func, self.rx()));
}
}