pub struct Signal<T> { /* private fields */ }
Expand description
A reactive signal that holds a value, tracks dependencies, and triggers effects.
Signal<T>
behaves similarly to a traditional “Property” (getter/setter),
but on top of that, it automatically tracks which reactive computations
or effects access it. When its value changes, all dependent effects
are automatically re-run.
In short:
- Like a Property: provides
get()
andset()
for accessing and updating the value. - Adds tracking: automatically records dependencies when read inside reactive contexts,
and automatically triggers dependent
Effect
s when updated.
§Type Parameters
T
: The type of the value stored in the signal. Must implementEq + Default
.
Implementations§
Source§impl<T> Signal<T>
impl<T> Signal<T>
Sourcepub fn new(value: Option<T>) -> Selfwhere
T: Default,
pub fn new(value: Option<T>) -> Selfwhere
T: Default,
Creates a new Signal
with the given initial value.
If None
is provided, T::default()
is used.
§Examples
use reactive_cache::Signal;
let signal = Signal::new(Some(10));
assert_eq!(*signal.get(), 10);
let default_signal: Signal<i32> = Signal::new(None);
assert_eq!(*default_signal.get(), 0);
Sourcepub fn get(&self) -> Ref<'_, T>
pub fn get(&self) -> Ref<'_, T>
Gets a reference to the current value, tracking dependencies and effects if inside a reactive context.
§Examples
use reactive_cache::Signal;
let signal = Signal::new(Some(42));
assert_eq!(*signal.get(), 42);
Sourcepub fn set(&self, value: T) -> boolwhere
T: Eq,
pub fn set(&self, value: T) -> boolwhere
T: Eq,
Sets the value of the signal.
Returns true
if the value changed and dependent effects were triggered.
§Examples
use reactive_cache::Signal;
let signal = Signal::new(Some(5));
assert_eq!(signal.set(10), true);
assert_eq!(*signal.get(), 10);
// Setting to the same value returns false
assert_eq!(signal.set(10), false);
Auto Trait Implementations§
impl<T> !Freeze for Signal<T>
impl<T> !RefUnwindSafe for Signal<T>
impl<T> !Send for Signal<T>
impl<T> !Sync for Signal<T>
impl<T> Unpin for Signal<T>where
T: Unpin,
impl<T> !UnwindSafe for Signal<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more