pub struct Signal<T: 'static> { /* private fields */ }
Expand description
State that can be set.
§Example
use maple_core::prelude::*;
let state = Signal::new(0);
assert_eq!(*state.get(), 0);
state.set(1);
assert_eq!(*state.get(), 1);
Implementations§
Source§impl<T: 'static> Signal<T>
impl<T: 'static> Signal<T>
Sourcepub fn set(&self, new_value: T)
pub fn set(&self, new_value: T)
Set the current value of the state.
This will notify and update any effects and memos that depend on this value.
§Example
let state = Signal::new(0);
assert_eq!(*state.get(), 0);
state.set(1);
assert_eq!(*state.get(), 1);
Sourcepub fn handle(&self) -> StateHandle<T>
pub fn handle(&self) -> StateHandle<T>
Get the StateHandle
associated with this signal.
This is a shortcut for (*signal).clone()
.
Sourcepub fn into_handle(self) -> StateHandle<T>
pub fn into_handle(self) -> StateHandle<T>
Consumes this signal and returns its underlying StateHandle
.
Sourcepub fn trigger_subscribers(&self)
pub fn trigger_subscribers(&self)
Calls all the subscribers without modifying the state.
This can be useful when using patterns such as inner mutability where the state updated will not be automatically triggered.
In the general case, however, it is preferable to use Signal::set
instead.
Methods from Deref<Target = StateHandle<T>>§
Sourcepub fn get_untracked(&self) -> Rc<T>
pub fn get_untracked(&self) -> Rc<T>
Get the current value of the state, without tracking this as a dependency if inside a reactive context.
§Example
use maple_core::prelude::*;
let state = Signal::new(1);
let double = create_memo({
let state = state.clone();
move || *state.get_untracked() * 2
});
assert_eq!(*double.get(), 2);
state.set(2);
// double value should still be old value because state was untracked
assert_eq!(*double.get(), 2);
Trait Implementations§
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>
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