Struct leptos::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 an async 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,

source

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> Clone for Signal<T>

source§

fn clone(&self) -> Signal<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for Signal<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<T> Default for Signal<T>
where T: Default,

source§

fn default() -> Signal<T>

Returns the “default value” for a type. Read more
source§

impl<F, T> From<F> for Signal<T>
where F: Fn() -> T + 'static,

source§

fn from(value: F) -> Signal<T>

Converts to this type from the input type.
source§

impl<T> From<Memo<T>> for Signal<T>

source§

fn from(value: Memo<T>) -> Signal<T>

Converts to this type from the input type.
source§

impl<T> From<ReadSignal<T>> for Signal<T>

source§

fn from(value: ReadSignal<T>) -> Signal<T>

Converts to this type from the input type.
source§

impl<T> From<RwSignal<T>> for Signal<T>

source§

fn from(value: RwSignal<T>) -> Signal<T>

Converts to this type from the input type.
source§

impl<T> From<Signal<Option<T>>> for MaybeProp<T>

source§

fn from(value: Signal<Option<T>>) -> MaybeProp<T>

Converts to this type from the input type.
source§

impl<T> From<Signal<T>> for MaybeProp<T>
where T: Clone,

source§

fn from(value: Signal<T>) -> MaybeProp<T>

Converts to this type from the input type.
source§

impl<T> From<Signal<T>> for MaybeSignal<T>

source§

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,

source§

fn into_attribute(self) -> Attribute

Converts the object into an Attribute.
source§

fn into_attribute_boxed(self: Box<Signal<T>>) -> Attribute

Helper function for dealing with Box<dyn IntoAttribute>.
source§

impl IntoClass for Signal<bool>

source§

fn into_class(self) -> Class

Converts the object into a Class.
source§

fn into_class_boxed(self: Box<Signal<bool>>) -> Class

Helper function for dealing with Box<dyn IntoClass>.
source§

impl<T> IntoProperty for Signal<T>
where T: Into<JsValue> + Clone,

source§

fn into_property(self) -> Property

Converts the object into a Property.
source§

fn into_property_boxed(self: Box<Signal<T>>) -> Property

Helper function for dealing with Box<dyn IntoProperty>.
source§

impl<T> IntoStyle for Signal<T>
where T: IntoStyle + Clone,

source§

fn into_style(self) -> Style

Converts the object into a Style.
source§

fn into_style_boxed(self: Box<Signal<T>>) -> Style

Helper function for dealing with Box<dyn IntoStyle>.
source§

impl<T> IntoView for Signal<T>
where T: IntoView + Clone,

source§

fn into_view(self) -> View

Converts the value into View.
source§

impl<T> PartialEq for Signal<T>

source§

fn eq(&self, other: &Signal<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> Serialize for Signal<T>
where T: Clone + Serialize,

source§

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>

source§

fn dispose(self)

Disposes of the signal. This: Read more
source§

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);
§

type Value = T

The value held by the signal.
source§

fn get(&self) -> T

Clones and returns the current value of the signal, and subscribes the running effect to this signal. Read more
source§

fn try_get(&self) -> Option<T>

Clones and returns the signal value, returning Some if the signal is still alive, and None otherwise.
source§

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.

§

type Value = T

The value held by the signal.
source§

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>

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,

source§

fn to_stream(&self) -> Pin<Box<dyn Stream<Item = T>>>

Generates a [Stream] that emits the new value of the signal whenever it changes. Read more
source§

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");
§

type Value = T

The value held by the signal.
source§

fn with<U>(&self, f: impl FnOnce(&T) -> U) -> U

Applies a function to the current value of the signal, and subscribes the running effect to this signal. Read more
source§

fn try_with<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>

Applies a function to the current value of the signal, and subscribes the running effect to this signal. Returns Some if the signal is valid and the function ran, otherwise returns None.
source§

fn track(&self)

Subscribes to this signal in the current reactive scope without doing anything with its value.
source§

impl<T> SignalWithUntracked for Signal<T>

§

type Value = T

The value held by the signal.
source§

fn with_untracked<O>(&self, f: impl FnOnce(&T) -> O) -> O

Runs the provided closure with a reference to the current value without creating a dependency on the current scope. Read more
source§

fn try_with_untracked<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>

Runs the provided closure with a reference to the current value without creating a dependency on the current scope. Returns [Some(O)] if the signal is still valid, None otherwise.
source§

impl<T> Copy for Signal<T>

source§

impl<T> Eq for Signal<T>

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<El> ElementDescriptorBounds for El
where El: Debug,