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(2);
let double_count = MaybeSignal::derive(move || count.get() * 2);
let memoized_double_count = create_memo(move |_| count.get() * 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.get() > 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(derived_signal: impl Fn() -> T + 'static) -> Self
pub fn derive(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(2);
let double_count = Signal::derive(move || count.get() * 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<'de, T: Deserialize<'de>> Deserialize<'de> for MaybeSignal<T>
impl<'de, T: Deserialize<'de>> Deserialize<'de> for MaybeSignal<T>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<T> From<MaybeSignal<Option<T>>> for MaybeProp<T>
impl<T> From<MaybeSignal<Option<T>>> for MaybeProp<T>
Source§fn from(value: MaybeSignal<Option<T>>) -> Self
fn from(value: MaybeSignal<Option<T>>) -> Self
Converts to this type from the input type.
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 for MaybeSignal<T>where
T: 'static + PartialEq,
impl<T> PartialEq for MaybeSignal<T>where
T: 'static + PartialEq,
Source§impl<T: Serialize> Serialize for MaybeSignal<T>
impl<T: Serialize> Serialize for MaybeSignal<T>
Source§impl<T: Clone> SignalGet for MaybeSignal<T>
§Examples
let (count, set_count) = create_signal(2);
let double_count = MaybeSignal::derive(move || count.get() * 2);
let memoized_double_count = create_memo(move |_| count.get() * 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);
impl<T: Clone> SignalGet for MaybeSignal<T>
§Examples
let (count, set_count) = create_signal(2);
let double_count = MaybeSignal::derive(move || count.get() * 2);
let memoized_double_count = create_memo(move |_| count.get() * 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 for MaybeSignal<T>
impl<T: Clone> SignalGetUntracked 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 for MaybeSignal<T>
§Examples
let (name, set_name) = create_signal("Alice".to_string());
let name_upper =
MaybeSignal::derive(move || name.with(|n| n.to_uppercase()));
let memoized_lower = create_memo(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.get().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.get(), "Alice");
assert_eq!(name_upper.get(), "ALICE");
assert_eq!(memoized_lower.get(), "alice");
assert_eq!(static_value.get(), "Bob");
impl<T> SignalWith for MaybeSignal<T>
§Examples
let (name, set_name) = create_signal("Alice".to_string());
let name_upper =
MaybeSignal::derive(move || name.with(|n| n.to_uppercase()));
let memoized_lower = create_memo(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.get().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.get(), "Alice");
assert_eq!(name_upper.get(), "ALICE");
assert_eq!(memoized_lower.get(), "alice");
assert_eq!(static_value.get(), "Bob");
Source§impl<T> SignalWithUntracked for MaybeSignal<T>
impl<T> SignalWithUntracked for MaybeSignal<T>
impl<T: Copy> Copy for MaybeSignal<T>
impl<T> Eq for MaybeSignal<T>where
T: 'static + Eq,
impl<T> StructuralPartialEq for MaybeSignal<T>where
T: 'static,
Auto Trait Implementations§
impl<T> Freeze for MaybeSignal<T>where
T: Freeze,
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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.