pub struct Signal<T: 'static>(/* private fields */);
Expand description
A reactive value that can be read and written to.
This is the writable version of ReadSignal
.
See create_signal
for more information.
Implementations§
Source§impl<T> Signal<T>
impl<T> Signal<T>
Sourcepub fn set_silent(self, new: T)
pub fn set_silent(self, new: T)
Silently set a new value for the signal. This will not trigger any updates in dependent signals. As such, this is generally not recommended as it can easily lead to state inconsistencies.
§Example
let state = create_signal(0);
let doubled = create_memo(move || state.get() * 2);
assert_eq!(doubled.get(), 0);
state.set_silent(1);
assert_eq!(doubled.get(), 0); // We now have inconsistent state!
Sourcepub fn set(self, new: T)
pub fn set(self, new: T)
Set a new value for the signal and automatically update any dependents.
§Example
let state = create_signal(0);
let doubled = create_memo(move || state.get() * 2);
assert_eq!(doubled.get(), 0);
state.set(1);
assert_eq!(doubled.get(), 2);
Sourcepub fn replace_silent(self, new: T) -> T
pub fn replace_silent(self, new: T) -> T
Silently set a new value for the signal and return the previous value.
This is the silent version of Signal::replace
.
Sourcepub fn replace(self, new: T) -> T
pub fn replace(self, new: T) -> T
Set a new value for the signal and return the previous value.
§Example
let state = create_signal(123);
let prev = state.replace(456);
assert_eq!(state.get(), 456);
assert_eq!(prev, 123);
Sourcepub fn take_silent(self) -> Twhere
T: Default,
pub fn take_silent(self) -> Twhere
T: Default,
Silently gets the value of the signal and sets the new value to the default value.
This is the silent version of Signal::take
.
Sourcepub fn take(self) -> Twhere
T: Default,
pub fn take(self) -> Twhere
T: Default,
Gets the value of the signal and sets the new value to the default value.
§Example
let state = create_signal(Some(123));
let prev = state.take();
assert_eq!(state.get(), None);
assert_eq!(prev, Some(123));
Sourcepub fn update_silent<U>(self, f: impl FnOnce(&mut T) -> U) -> U
pub fn update_silent<U>(self, f: impl FnOnce(&mut T) -> U) -> U
Update the value of the signal silently. This will not trigger any updates in dependent signals. As such, this is generally not recommended as it can easily lead to state inconsistencies.
This is the silent version of Signal::update
.
Sourcepub fn update<U>(self, f: impl FnOnce(&mut T) -> U) -> U
pub fn update<U>(self, f: impl FnOnce(&mut T) -> U) -> U
Update the value of the signal and automatically update any dependents.
Using this has the advantage of not needing to clone the value when updating it, especially
with types that do not implement Copy
where cloning can be expensive, or for types that
do not implement Clone
at all.
§Example
let state = create_signal("Hello".to_string());
state.update(|val| val.push_str(" Sycamore!"));
assert_eq!(state.get_clone(), "Hello Sycamore!");
Sourcepub fn set_fn_silent(self, f: impl FnOnce(&T) -> T)
pub fn set_fn_silent(self, f: impl FnOnce(&T) -> T)
Use a function to produce a new value and sets the value silently.
This is the silent version of Signal::set_fn
.
Sourcepub fn set_fn(self, f: impl FnOnce(&T) -> T)
pub fn set_fn(self, f: impl FnOnce(&T) -> T)
Use a function to produce a new value and sets the value.
§Example
let state = create_signal(123);
state.set_fn(|val| *val + 1);
assert_eq!(state.get(), 124);
Sourcepub fn split(self) -> (ReadSignal<T>, impl Fn(T) -> T)
pub fn split(self) -> (ReadSignal<T>, impl Fn(T) -> T)
Split the signal into a reader/writter pair.
§Example
let (read_signal, mut write_signal) = create_signal(0).split();
assert_eq!(read_signal.get(), 0);
write_signal(1);
assert_eq!(read_signal.get(), 1);
Trait Implementations§
Source§impl<T: AddAssign<Rhs>, Rhs> AddAssign<Rhs> for Signal<T>
impl<T: AddAssign<Rhs>, Rhs> AddAssign<Rhs> for Signal<T>
Source§fn add_assign(&mut self, rhs: Rhs)
fn add_assign(&mut self, rhs: Rhs)
+=
operation. Read moreSource§impl<'de, T: Deserialize<'de>> Deserialize<'de> for Signal<T>
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Signal<T>
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl<T: DivAssign<Rhs>, Rhs> DivAssign<Rhs> for Signal<T>
impl<T: DivAssign<Rhs>, Rhs> DivAssign<Rhs> for Signal<T>
Source§fn div_assign(&mut self, rhs: Rhs)
fn div_assign(&mut self, rhs: Rhs)
/=
operation. Read moreSource§impl<T: MulAssign<Rhs>, Rhs> MulAssign<Rhs> for Signal<T>
impl<T: MulAssign<Rhs>, Rhs> MulAssign<Rhs> for Signal<T>
Source§fn mul_assign(&mut self, rhs: Rhs)
fn mul_assign(&mut self, rhs: Rhs)
*=
operation. Read moreSource§impl<T: Ord> Ord for Signal<T>
impl<T: Ord> Ord for Signal<T>
Source§impl<T: PartialOrd> PartialOrd for Signal<T>
impl<T: PartialOrd> PartialOrd for Signal<T>
Source§impl<T: RemAssign<Rhs>, Rhs> RemAssign<Rhs> for Signal<T>
impl<T: RemAssign<Rhs>, Rhs> RemAssign<Rhs> for Signal<T>
Source§fn rem_assign(&mut self, rhs: Rhs)
fn rem_assign(&mut self, rhs: Rhs)
%=
operation. Read moreSource§impl<T: SubAssign<Rhs>, Rhs> SubAssign<Rhs> for Signal<T>
impl<T: SubAssign<Rhs>, Rhs> SubAssign<Rhs> for Signal<T>
Source§fn sub_assign(&mut self, rhs: Rhs)
fn sub_assign(&mut self, rhs: Rhs)
-=
operation. Read more