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(2);
let double_count = Signal::derive(move || count.get() * 2);
let memoized_double_count = create_memo(move |_| count.get() * 2);
// this function takes any kind of wrapped signal
fn above_3(arg: &Signal<i32>) -> bool {
// ✅ calling the signal clones and returns the value
// can be `arg() > 3` on nightly
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);
Implementations§
source§impl<T> Signal<T>where
T: 'static,
impl<T> Signal<T>where
T: 'static,
sourcepub fn derive(derived_signal: impl Fn() -> T + 'static) -> Signal<T>
pub fn derive(derived_signal: impl Fn() -> T + 'static) -> Signal<T>
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: &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>) -> Signal<T>
fn from(value: ReadSignal<T>) -> Signal<T>
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§fn from(value: Signal<T>) -> MaybeSignal<T>
fn from(value: Signal<T>) -> MaybeSignal<T>
Converts to this type from the input type.
source§impl<T> IntoAttribute for Signal<T>where
T: IntoAttribute + Clone,
impl<T> IntoAttribute for Signal<T>where
T: IntoAttribute + Clone,
source§impl<T> IntoProperty for Signal<T>
impl<T> IntoProperty for Signal<T>
source§impl<T> Serialize for Signal<T>
impl<T> Serialize for Signal<T>
source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Serialize this value into the given Serde serializer. Read more
source§impl<T> SignalDispose for Signal<T>
impl<T> SignalDispose for Signal<T>
source§impl<T> SignalGet for Signal<T>where
T: Clone,
impl<T> SignalGet for Signal<T>where
T: Clone,
§Examples
let (count, set_count) = create_signal(2);
let double_count = Signal::derive(move || count.get() * 2);
let memoized_double_count = create_memo(move |_| count.get() * 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> SignalGetUntracked for Signal<T>where
T: Clone,
impl<T> SignalGetUntracked for Signal<T>where
T: Clone,
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> SignalStream<T> for Signal<T>where
T: Clone,
impl<T> SignalStream<T> for Signal<T>where
T: Clone,
source§impl<T> SignalWith for Signal<T>
impl<T> SignalWith for Signal<T>
§Examples
let (name, set_name) = create_signal("Alice".to_string());
let name_upper = Signal::derive(move || name.with(|n| n.to_uppercase()));
let memoized_lower = create_memo(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.get().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.get(), "Alice");
assert_eq!(name_upper.get(), "ALICE");
assert_eq!(memoized_lower.get(), "alice");
source§impl<T> SignalWithUntracked for Signal<T>
impl<T> SignalWithUntracked for Signal<T>
impl<T> Copy for Signal<T>
impl<T> Eq for Signal<T>
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>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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)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.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more