Struct leptos_reactive::Signal
source · pub struct Signal<T>where
T: 'static,{ /* private fields */ }
Expand description
A wrapper for any kind of readable reactive signal: a ReadSignal, Memo, RwSignal, or derived signal closure.
This allows you to create APIs that take any kind of Signal<T>
as an argument,
rather than adding a generic F: Fn() -> T
. Values can be access with the same
function call, with()
, and get()
APIs as other signals.
Core Trait Implementations
.get()
(or calling the signal as a function) clones the current value of the signal. If you call it within an effect, it will cause that effect to subscribe to the signal, and to re-run whenever the value of the signal changes..get_untracked()
clones the value of the signal without reactively tracking it.
.with()
allows you to reactively access the signal’s value without cloning by applying a callback function..with_untracked()
allows you to access the signal’s value without reactively tracking it.
.to_stream()
converts the signal to anasync
stream of values.
Examples
let (count, set_count) = create_signal(cx, 2);
let double_count = Signal::derive(cx, move || count() * 2);
let memoized_double_count = create_memo(cx, move |_| count() * 2);
// this function takes any kind of wrapped signal
fn above_3(arg: &Signal<i32>) -> bool {
// ✅ calling the signal clones and returns the value
// it is a shorthand for arg.get()
arg() > 3
}
assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
assert_eq!(above_3(&memoized_double_count.into()), true);
Implementations§
source§impl<T> Signal<T>where
T: 'static,
impl<T> Signal<T>where T: 'static,
sourcepub fn derive(cx: Scope, derived_signal: impl Fn() -> T + 'static) -> Self
pub fn derive(cx: Scope, derived_signal: impl Fn() -> T + 'static) -> Self
Wraps a derived signal, i.e., any computation that accesses one or more reactive signals.
let (count, set_count) = create_signal(cx, 2);
let double_count = Signal::derive(cx, move || count() * 2);
// this function takes any kind of wrapped signal
fn above_3(arg: &Signal<i32>) -> bool {
arg.get() > 3
}
assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
Trait Implementations§
source§impl<T> From<ReadSignal<T>> for Signal<T>
impl<T> From<ReadSignal<T>> for Signal<T>
source§fn from(value: ReadSignal<T>) -> Self
fn from(value: ReadSignal<T>) -> Self
Converts to this type from the input type.
source§impl<T> From<Signal<T>> for MaybeSignal<T>
impl<T> From<Signal<T>> for MaybeSignal<T>
source§impl<T> PartialEq<Signal<T>> for Signal<T>where
T: 'static + PartialEq,
impl<T> PartialEq<Signal<T>> for Signal<T>where T: 'static + PartialEq,
source§impl<T: Clone> SignalGet<T> for Signal<T>
impl<T: Clone> SignalGet<T> for Signal<T>
Examples
let (count, set_count) = create_signal(cx, 2);
let double_count = Signal::derive(cx, move || count() * 2);
let memoized_double_count = create_memo(cx, move |_| count() * 2);
// this function takes any kind of wrapped signal
fn above_3(arg: &Signal<i32>) -> bool {
arg.get() > 3
}
assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
assert_eq!(above_3(&memoized_double_count.into()), true);
source§impl<T: Clone> SignalGetUntracked<T> for Signal<T>
impl<T: Clone> SignalGetUntracked<T> for Signal<T>
Please note that using Signal::with_untracked
still clones the inner value,
so there’s no benefit to using it as opposed to calling
Signal::get_untracked
.
source§fn get_untracked(&self) -> T
fn get_untracked(&self) -> T
Gets the signal’s value without creating a dependency on the
current scope. Read more
source§fn try_get_untracked(&self) -> Option<T>
fn try_get_untracked(&self) -> Option<T>
Gets the signal’s value without creating a dependency on the
current scope. Returns [
Some(T)
] if the signal is still
valid, None
otherwise.source§impl<T: Clone> SignalStream<T> for Signal<T>
impl<T: Clone> SignalStream<T> for Signal<T>
source§impl<T> SignalWith<T> for Signal<T>
impl<T> SignalWith<T> for Signal<T>
Examples
let (name, set_name) = create_signal(cx, "Alice".to_string());
let name_upper =
Signal::derive(cx, move || name.with(|n| n.to_uppercase()));
let memoized_lower =
create_memo(cx, move |_| name.with(|n| n.to_lowercase()));
// this function takes any kind of wrapped signal
fn current_len_inefficient(arg: Signal<String>) -> usize {
// ❌ unnecessarily clones the string
arg().len()
}
fn current_len(arg: &Signal<String>) -> usize {
// ✅ gets the length without cloning the `String`
arg.with(|value| value.len())
}
assert_eq!(current_len(&name.into()), 5);
assert_eq!(current_len(&name_upper), 5);
assert_eq!(current_len(&memoized_lower.into()), 5);
assert_eq!(name(), "Alice");
assert_eq!(name_upper(), "ALICE");
assert_eq!(memoized_lower(), "alice");
source§impl<T> SignalWithUntracked<T> for Signal<T>
impl<T> SignalWithUntracked<T> for Signal<T>
impl<T> Copy for Signal<T>
impl<T> Eq for Signal<T>where T: 'static + Eq,
impl<T> StructuralEq for Signal<T>where T: 'static,
impl<T> StructuralPartialEq for Signal<T>where T: 'static,
Auto Trait Implementations§
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
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.