use super::*;
impl<T> Copy for LocalState<T> where T: 'static {}
impl<T> Clone for LocalState<T>
where
T: 'static,
{
fn clone(&self) -> Self {
*self
}
}
impl<T> PartialEq for LocalState<T>
where
T: 'static,
{
fn eq(&self, other: &Self) -> bool {
let Self { signal: lhs } = *self;
let Self { signal: rhs } = *other;
lhs == rhs
}
}
impl<T> Eq for LocalState<T> where T: 'static {}
impl<T> LocalState<T>
where
T: 'static,
{
#[must_use]
pub fn new(initial: T) -> Self {
Self {
signal: Signal::new(initial),
}
}
#[must_use]
pub fn signal(&self) -> Signal<T> {
let Self { signal } = *self;
signal
}
#[must_use]
pub fn get(&self) -> T
where
T: Clone + Default,
{
self.signal().get()
}
#[must_use]
pub fn get_untracked(&self) -> T
where
T: Clone + Default,
{
self.signal().get_untracked()
}
pub fn set(&self, value: T) -> bool {
self.signal().set(value)
}
pub fn set_if_changed(&self, value: T) -> bool
where
T: PartialEq,
{
self.signal().set_if_changed(value)
}
pub fn update<F>(&self, updater: F) -> bool
where
T: Clone,
F: FnOnce(&mut T),
{
self.signal().update(updater)
}
#[must_use]
pub fn watch<F>(&self, mut handler: F) -> Effect
where
T: Clone,
F: FnMut(&T, &T) + 'static,
{
let sig: Signal<T> = self.signal();
let prev_cell: Rc<RefCell<Option<T>>> = Rc::new(RefCell::new(None));
Effect::new(move || {
if let Some(next) = sig.try_get() {
let prev_opt: Option<T> = match prev_cell.try_borrow_mut() {
Ok(mut guard) => guard.replace(next.clone()),
Err(_) => None,
};
if let Some(prev) = prev_opt {
handler(&prev, &next);
}
}
})
}
}
impl GlobalState {
pub fn provide<T>(initial: T) -> Signal<T>
where
T: 'static,
{
let (id, should_flush): (SignalId, bool) = with_runtime(
|rt: &mut ReactiveRuntime| rt.provide_global_state(initial),
(SignalId::default(), false),
);
if should_flush {
flush_effects();
}
Signal::from_raw_id(id)
}
#[must_use]
pub fn use_state<T>() -> Option<Signal<T>>
where
T: 'static,
{
with_runtime(
|rt: &mut ReactiveRuntime| rt.lookup_global_state::<T>(),
None,
)
.map(Signal::from_raw_id)
}
#[must_use]
pub fn use_or_init<T, F>(init: F) -> Signal<T>
where
T: 'static,
F: FnOnce() -> T,
{
let id: SignalId = with_runtime(
|rt: &mut ReactiveRuntime| rt.get_or_init_global_state(init),
SignalId::default(),
);
Signal::from_raw_id(id)
}
#[must_use]
pub fn get<T>() -> Option<T>
where
T: Clone + 'static,
{
Self::use_state::<T>().and_then(|sig: Signal<T>| sig.try_get())
}
#[must_use]
pub fn get_untracked<T>() -> Option<T>
where
T: Clone + 'static,
{
Self::use_state::<T>().and_then(|sig: Signal<T>| sig.try_get_untracked())
}
pub fn set<T>(value: T) -> bool
where
T: 'static,
{
match Self::use_state::<T>() {
Some(sig) => sig.set(value),
None => false,
}
}
pub fn set_if_changed<T>(value: T) -> bool
where
T: PartialEq + 'static,
{
match Self::use_state::<T>() {
Some(sig) => sig.set_if_changed(value),
None => false,
}
}
pub fn update<T, F>(updater: F) -> bool
where
T: Clone + 'static,
F: FnOnce(&mut T),
{
match Self::use_state::<T>() {
Some(sig) => sig.update(updater),
None => false,
}
}
#[must_use]
pub fn watch<T, F>(mut handler: F) -> Option<Effect>
where
T: Clone + 'static,
F: FnMut(&T, &T) + 'static,
{
let sig: Signal<T> = Self::use_state::<T>()?;
let prev_cell: Rc<RefCell<Option<T>>> = Rc::new(RefCell::new(None));
Some(Effect::new(move || {
if let Some(next) = sig.try_get() {
let prev_opt: Option<T> = match prev_cell.try_borrow_mut() {
Ok(mut guard) => guard.replace(next.clone()),
Err(_) => None,
};
if let Some(prev) = prev_opt {
handler(&prev, &next);
}
}
}))
}
pub fn provide_named<T>(name: &str, initial: T) -> Signal<T>
where
T: 'static,
{
let (id, should_flush): (SignalId, bool) = with_runtime(
|rt: &mut ReactiveRuntime| rt.provide_named_global_state(name, initial),
(SignalId::default(), false),
);
if should_flush {
flush_effects();
}
Signal::from_raw_id(id)
}
#[must_use]
pub fn use_named<T>(name: &str) -> Option<Signal<T>>
where
T: 'static,
{
with_runtime(
|rt: &mut ReactiveRuntime| rt.lookup_named_global_state(name),
None,
)
.map(Signal::from_raw_id)
}
#[must_use]
pub fn watch_named<T, F>(name: &str, mut handler: F) -> Option<Effect>
where
T: Clone + 'static,
F: FnMut(&T, &T) + 'static,
{
let sig: Signal<T> = Self::use_named::<T>(name)?;
let prev_cell: Rc<RefCell<Option<T>>> = Rc::new(RefCell::new(None));
Some(Effect::new(move || {
if let Some(next) = sig.try_get() {
let prev_opt: Option<T> = match prev_cell.try_borrow_mut() {
Ok(mut guard) => guard.replace(next.clone()),
Err(_) => None,
};
if let Some(prev) = prev_opt {
handler(&prev, &next);
}
}
}))
}
}