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(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,

source

pub fn derive(cx: Scope, 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(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);
source

pub fn default(cx: Scope) -> Signal<T>where T: Default,

Creates a signal that yields the default value of T when you call .get() or signal().

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>where T: Debug + 'static,

source§

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

Formats the value using the given formatter. Read more
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<T>> for MaybeSignal<T>

source§

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

Converts to this type from the input type.
source§

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

source§

fn into_view(self, cx: Scope) -> View

Converts the value into View.
source§

impl<T> PartialEq<Signal<T>> for Signal<T>where T: PartialEq<T> + 'static,

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> SignalGet<T> for Signal<T>where T: Clone,

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§

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<T> 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

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, cx: Scope ) -> Pin<Box<dyn Stream<Item = T> + 'static, Global>>

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

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§

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

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>where T: Eq + 'static,

source§

impl<T> StructuralEq for Signal<T>where T: 'static,

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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,

source§

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 Twhere 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 Twhere 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 Twhere 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 Twhere 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 Elwhere El: Debug,