Enum leptos_reactive::MaybeSignal
source · pub enum MaybeSignal<T>where
T: 'static,{
Static(T),
Dynamic(Signal<T>),
}
Expand description
A wrapper for a value that is either T
or Signal<T>
.
This allows you to create APIs that take either a reactive or a non-reactive value of the same type. This is especially useful for component properties.
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 = MaybeSignal::derive(cx, move || count() * 2);
let memoized_double_count = create_memo(cx, move |_| count() * 2);
let static_value = 5;
// this function takes either a reactive or non-reactive value
fn above_3(arg: &MaybeSignal<i32>) -> bool {
// ✅ calling the signal clones and returns the value
// it is a shorthand for arg.get()
arg() > 3
}
assert_eq!(above_3(&static_value.into()), true);
assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count), true);
assert_eq!(above_3(&memoized_double_count.into()), true);
Variants§
Static(T)
An unchanging value of type T
.
Dynamic(Signal<T>)
A reactive signal that contains a value of type T
.
Implementations§
source§impl<T> MaybeSignal<T>where
T: 'static,
impl<T> MaybeSignal<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: &MaybeSignal<i32>) -> bool {
arg.get() > 3
}
assert_eq!(above_3(&count.into()), false);
assert_eq!(above_3(&double_count.into()), true);
assert_eq!(above_3(&2.into()), false);
Trait Implementations§
source§impl<T: Clone> Clone for MaybeSignal<T>
impl<T: Clone> Clone for MaybeSignal<T>
source§impl<T> Debug for MaybeSignal<T>where
T: 'static + Debug,
impl<T> Debug for MaybeSignal<T>where T: 'static + Debug,
source§impl<T: Default> Default for MaybeSignal<T>
impl<T: Default> Default for MaybeSignal<T>
source§impl<T> From<Memo<T>> for MaybeSignal<T>
impl<T> From<Memo<T>> for MaybeSignal<T>
source§impl<T> From<ReadSignal<T>> for MaybeSignal<T>
impl<T> From<ReadSignal<T>> for MaybeSignal<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<RwSignal<T>> for MaybeSignal<T>
impl<T> From<RwSignal<T>> for MaybeSignal<T>
source§impl<T> From<Signal<T>> for MaybeSignal<T>
impl<T> From<Signal<T>> for MaybeSignal<T>
source§impl<T> From<T> for MaybeSignal<T>
impl<T> From<T> for MaybeSignal<T>
source§impl<T> PartialEq<MaybeSignal<T>> for MaybeSignal<T>where
T: 'static + PartialEq,
impl<T> PartialEq<MaybeSignal<T>> for MaybeSignal<T>where T: 'static + PartialEq,
source§fn eq(&self, other: &MaybeSignal<T>) -> bool
fn eq(&self, other: &MaybeSignal<T>) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl<T: Clone> SignalGet<T> for MaybeSignal<T>
impl<T: Clone> SignalGet<T> for MaybeSignal<T>
Examples
let (count, set_count) = create_signal(cx, 2);
let double_count = MaybeSignal::derive(cx, move || count() * 2);
let memoized_double_count = create_memo(cx, move |_| count() * 2);
let static_value: MaybeSignal<i32> = 5.into();
// this function takes any kind of wrapped signal
fn above_3(arg: &MaybeSignal<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);
assert_eq!(above_3(&static_value.into()), true);
source§impl<T: Clone> SignalGetUntracked<T> for MaybeSignal<T>
impl<T: Clone> SignalGetUntracked<T> for MaybeSignal<T>
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 MaybeSignal<T>
impl<T: Clone> SignalStream<T> for MaybeSignal<T>
source§impl<T> SignalWith<T> for MaybeSignal<T>
impl<T> SignalWith<T> for MaybeSignal<T>
Examples
let (name, set_name) = create_signal(cx, "Alice".to_string());
let name_upper =
MaybeSignal::derive(cx, move || name.with(|n| n.to_uppercase()));
let memoized_lower =
create_memo(cx, move |_| name.with(|n| n.to_lowercase()));
let static_value: MaybeSignal<String> = "Bob".to_string().into();
// this function takes any kind of wrapped signal
fn current_len_inefficient(arg: &MaybeSignal<String>) -> usize {
// ❌ unnecessarily clones the string
arg().len()
}
fn current_len(arg: &MaybeSignal<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!(current_len(&static_value), 3);
assert_eq!(name(), "Alice");
assert_eq!(name_upper(), "ALICE");
assert_eq!(memoized_lower(), "alice");
assert_eq!(static_value(), "Bob");
source§impl<T> SignalWithUntracked<T> for MaybeSignal<T>
impl<T> SignalWithUntracked<T> for MaybeSignal<T>
impl<T: Copy> Copy for MaybeSignal<T>
impl<T> Eq for MaybeSignal<T>where T: 'static + Eq,
impl<T> StructuralEq for MaybeSignal<T>where T: 'static,
impl<T> StructuralPartialEq for MaybeSignal<T>where T: 'static,
Auto Trait Implementations§
impl<T> !RefUnwindSafe for MaybeSignal<T>
impl<T> !Send for MaybeSignal<T>
impl<T> !Sync for MaybeSignal<T>
impl<T> Unpin for MaybeSignal<T>where T: Unpin,
impl<T> !UnwindSafe for MaybeSignal<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.