pub trait SignalWith<T> {
    // Required methods
    fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O;
    fn try_with<O>(&self, f: impl FnOnce(&T) -> O) -> Option<O>;

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

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

Required Methods§

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.

Panics

Panics if you try to access a signal that was created in a Scope that has been disposed.

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.

Provided Methods§

source

fn track(&self)

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

Implementors§

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§

impl<T> SignalWith<T> for Memo<T>

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§

impl<T> SignalWith<T> for ReadSignal<T>

Examples

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

// ❌ unnecessarily clones the string
let first_char = move || name().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("Bob".to_string());
assert_eq!(first_char(), 'B');
source§

impl<T> SignalWith<T> for RwSignal<T>

Examples

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

// ❌ unnecessarily clones the string
let first_char = move || name().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');