pub trait SignalWith {
    type Value;

    // Required methods
    fn with<O>(&self, f: impl FnOnce(&Self::Value) -> O) -> O;
    fn try_with<O>(&self, f: impl FnOnce(&Self::Value) -> O) -> Option<O>;

    // Provided method
    fn track(&self) { ... }
}
Expand description

This trait allows obtaining an immutable reference to the signal’s inner type.

Required Associated Types§

source

type Value

The value held by the signal.

Required Methods§

source

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

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

§Panics

Panics if you try to access a signal that is owned by a reactive node that has been disposed.

source

fn try_with<O>(&self, f: impl FnOnce(&Self::Value) -> 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.

Provided Methods§

source

fn track(&self)

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

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<S, T> SignalWith for Resource<S, T>
where S: Clone,

§

type Value = Option<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");
§

type Value = T

source§

impl<T> SignalWith for Memo<T>

§

type Value = T

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

source§

impl<T> SignalWith for ReadSignal<T>

§Examples

let (name, set_name) = create_signal("Alice".to_string());

// ❌ unnecessarily clones the string
let first_char = move || name.get().chars().next().unwrap();
assert_eq!(first_char(), 'A');

// ✅ gets the first char without cloning the `String`
let first_char = move || name.with(|n| n.chars().next().unwrap());
assert_eq!(first_char(), 'A');
set_name.set("Bob".to_string());
assert_eq!(first_char(), 'B');
§

type Value = T

source§

impl<T> SignalWith for RwSignal<T>

§Examples

let name = create_rw_signal("Alice".to_string());

// ❌ unnecessarily clones the string
let first_char = move || name.get().chars().next().unwrap();
assert_eq!(first_char(), 'A');

// ✅ gets the first char without cloning the `String`
let first_char = move || name.with(|n| n.chars().next().unwrap());
assert_eq!(first_char(), 'A');
name.set("Bob".to_string());
assert_eq!(first_char(), 'B');
§

type Value = T