#![forbid(unsafe_code)]
use crate::{create_rw_signal, RwSignal, Scope, UntrackedGettableSignal, UntrackedSettableSignal};
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct StoredValue<T>(RwSignal<T>)
where
T: 'static;
impl<T> Clone for StoredValue<T> {
fn clone(&self) -> Self {
Self(self.0)
}
}
impl<T> Copy for StoredValue<T> {}
impl<T> StoredValue<T>
where
T: 'static,
{
pub fn get(&self) -> T
where
T: Clone,
{
self.with(T::clone)
}
pub fn with<U>(&self, f: impl FnOnce(&T) -> U) -> U {
self.0.with_untracked(f)
}
pub fn update(&self, f: impl FnOnce(&mut T)) {
self.0.update_untracked(f);
}
pub fn set(&self, value: T) {
self.0.set_untracked(value);
}
}
pub fn store_value<T>(cx: Scope, value: T) -> StoredValue<T>
where
T: 'static,
{
StoredValue(create_rw_signal(cx, value))
}
#[cfg(not(feature = "stable"))]
impl<T> FnOnce<()> for StoredValue<T>
where
T: Clone,
{
type Output = T;
extern "rust-call" fn call_once(self, _args: ()) -> Self::Output {
self.get()
}
}
#[cfg(not(feature = "stable"))]
impl<T> FnMut<()> for StoredValue<T>
where
T: Clone,
{
extern "rust-call" fn call_mut(&mut self, _args: ()) -> Self::Output {
self.get()
}
}
#[cfg(not(feature = "stable"))]
impl<T> Fn<()> for StoredValue<T>
where
T: Clone,
{
extern "rust-call" fn call(&self, _args: ()) -> Self::Output {
self.get()
}
}