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

source

pub fn derive( cx: Scope, derived_signal: impl Fn() -> T + 'static ) -> MaybeSignal<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: &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 for MaybeSignal<T>where T: Clone,

source§

fn clone(&self) -> MaybeSignal<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 MaybeSignal<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> Default for MaybeSignal<T>where T: Default,

source§

fn default() -> MaybeSignal<T>

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

impl From<&str> for MaybeSignal<String>

source§

fn from(value: &str) -> MaybeSignal<String>

Converts to this type from the input type.
source§

impl<T> From<Memo<T>> for MaybeSignal<T>

source§

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

Converts to this type from the input type.
source§

impl<T> From<ReadSignal<T>> for MaybeSignal<T>

source§

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

Converts to this type from the input type.
source§

impl<T> From<RwSignal<T>> for MaybeSignal<T>

source§

fn from(value: RwSignal<T>) -> MaybeSignal<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> From<T> for MaybeSignal<T>

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts the value into View.
source§

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

source§

fn eq(&self, other: &MaybeSignal<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 MaybeSignal<T>where T: Clone,

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§

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 MaybeSignal<T>where T: Clone,

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 MaybeSignal<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 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§

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

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 MaybeSignal<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 MaybeSignal<T>where T: Copy,

source§

impl<T> Eq for MaybeSignal<T>where T: Eq + 'static,

source§

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

source§

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

source§

fn from(t: !) -> T

Converts to this type from the input type.
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,