From 16e67c20615e6ab269efa7053ca0b6aba585f175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar=20Rubio?= Date: Mon, 5 Feb 2024 23:43:31 +0100 Subject: [PATCH] Use signals instead of memos with first unused argument when possible --- src/core/element_maybe_signal.rs | 3 +-- src/core/elements_maybe_signal.rs | 5 ++--- src/math/shared.rs | 3 +-- src/use_breakpoints.rs | 3 +-- src/use_cycle_list.rs | 4 ++-- src/use_infinite_scroll.rs | 2 +- src/use_sorted.rs | 9 +++------ 7 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/core/element_maybe_signal.rs b/src/core/element_maybe_signal.rs index 7a047e5..a57b594 100644 --- a/src/core/element_maybe_signal.rs +++ b/src/core/element_maybe_signal.rs @@ -208,8 +208,7 @@ macro_rules! impl_from_signal_string { Self::Dynamic(Signal::derive(|| None)) } else { Self::Dynamic( - create_memo(move |_| document().query_selector(&signal.get()).unwrap_or_default()) - .into(), + Signal::derive(move || document().query_selector(&signal.get()).unwrap_or_default()), ) }} } diff --git a/src/core/elements_maybe_signal.rs b/src/core/elements_maybe_signal.rs index 6a086af..e1f7f7b 100644 --- a/src/core/elements_maybe_signal.rs +++ b/src/core/elements_maybe_signal.rs @@ -216,7 +216,7 @@ macro_rules! impl_from_signal_string { fn from(signal: $ty) -> Self { cfg_if! { if #[cfg(feature = "ssr")] { Self::Dynamic( - create_memo(move |_| { + Signal::derive(move || { if let Ok(node_list) = document().query_selector_all(&signal.get()) { let mut list = Vec::with_capacity(node_list.length() as usize); for i in 0..node_list.length() { @@ -227,8 +227,7 @@ macro_rules! impl_from_signal_string { } else { vec![] } - }) - .into(), + }), ) } else { let _ = signal; diff --git a/src/math/shared.rs b/src/math/shared.rs index fded77a..6bbf6e7 100644 --- a/src/math/shared.rs +++ b/src/math/shared.rs @@ -13,7 +13,7 @@ macro_rules! use_partial_cmp { { let container = container.into(); - create_memo(move |_| { + Signal::derive(move || { container.with(|container| { if container.into_iter().count() == 0 { return None; @@ -34,7 +34,6 @@ macro_rules! use_partial_cmp { .cloned() }) }) - .into() } }; } diff --git a/src/use_breakpoints.rs b/src/use_breakpoints.rs index d05840f..548da84 100644 --- a/src/use_breakpoints.rs +++ b/src/use_breakpoints.rs @@ -264,14 +264,13 @@ impl UseBreakpointsReturn { let signals: Vec<_> = keys.iter().map(ge.clone()).collect(); - create_memo(move |_| { + Signal::derive(move || { keys.iter() .cloned() .zip(signals.iter().cloned()) .filter_map(|(key, signal)| signal.get().then_some(key)) .collect::>() }) - .into() } } diff --git a/src/use_cycle_list.rs b/src/use_cycle_list.rs index ecbb6b5..6fa7c82 100644 --- a/src/use_cycle_list.rs +++ b/src/use_cycle_list.rs @@ -86,7 +86,7 @@ where let index = { let list = list.clone(); - create_memo(move |_| { + Signal::derive(move || { list.with(|list| { let index = get_position(&state.get(), list); @@ -161,7 +161,7 @@ where UseCycleListReturn { state, set_state, - index: index.into(), + index, set_index: set, next, prev, diff --git a/src/use_infinite_scroll.rs b/src/use_infinite_scroll.rs index 2b23261..29056a3 100644 --- a/src/use_infinite_scroll.rs +++ b/src/use_infinite_scroll.rs @@ -108,7 +108,7 @@ where let (is_loading, set_loading) = create_signal(false); let el = el.into(); - let observed_element = create_memo(move |_| { + let observed_element = Signal::derive(move || { let el = el.get(); el.map(|el| { diff --git a/src/use_sorted.rs b/src/use_sorted.rs index 32ff806..b9591de 100644 --- a/src/use_sorted.rs +++ b/src/use_sorted.rs @@ -81,12 +81,11 @@ where { let iterable = iterable.into(); - create_memo(move |_| { + Signal::derive(move || { let mut iterable = iterable.get(); iterable.sort(); iterable }) - .into() } /// Version of [`use_sorted`] with a compare function. @@ -98,12 +97,11 @@ where { let iterable = iterable.into(); - create_memo(move |_| { + Signal::derive(move || { let mut iterable = iterable.get(); iterable.sort_by(cmp_fn.clone()); iterable }) - .into() } /// Version of [`use_sorted`] by key. @@ -116,10 +114,9 @@ where { let iterable = iterable.into(); - create_memo(move |_| { + Signal::derive(move || { let mut iterable = iterable.get(); iterable.sort_by_key(key_fn.clone()); iterable }) - .into() }