From a82db40bbeb465af5219aab1d88bd54ac4940a74 Mon Sep 17 00:00:00 2001 From: Luke Chu <37006668+lukechu10@users.noreply.github.com> Date: Fri, 6 Oct 2023 17:36:15 +0100 Subject: [PATCH] Repace the Memo struct with ReadSignal `Memo` used to be a wrapper struct around `ReadSignal` which did not provide anything extra so this makes everything simpler. --- examples/todomvc/src/main.rs | 2 +- packages/sycamore-core/src/view.rs | 2 +- packages/sycamore-reactive/src/iter.rs | 4 +- packages/sycamore-reactive/src/memos.rs | 60 +++------------------- packages/sycamore-reactive/src/signals.rs | 4 +- packages/sycamore-reactive/src/utils.rs | 12 ----- packages/sycamore-router/src/router.rs | 2 +- packages/tools/bench/benches/reactivity.rs | 2 +- 8 files changed, 15 insertions(+), 73 deletions(-) diff --git a/examples/todomvc/src/main.rs b/examples/todomvc/src/main.rs index a733b33a..01e45139 100644 --- a/examples/todomvc/src/main.rs +++ b/examples/todomvc/src/main.rs @@ -340,7 +340,7 @@ pub fn List() -> View { ul(class="todo-list") { Keyed( - iterable=*filtered_todos, + iterable=filtered_todos, view=|todo| view! { Item(todo=todo) }, diff --git a/packages/sycamore-core/src/view.rs b/packages/sycamore-core/src/view.rs index 6775438d..1cd6a3ed 100644 --- a/packages/sycamore-core/src/view.rs +++ b/packages/sycamore-core/src/view.rs @@ -52,7 +52,7 @@ impl View { #[cfg_attr(debug_assertions, track_caller)] pub fn new_dyn(f: impl FnMut() -> View + 'static) -> Self { Self { - inner: ViewType::Dyn(*create_memo(f).inner_signal()), + inner: ViewType::Dyn(create_memo(f)), } } diff --git a/packages/sycamore-reactive/src/iter.rs b/packages/sycamore-reactive/src/iter.rs index 31f1b00f..2739d9fa 100644 --- a/packages/sycamore-reactive/src/iter.rs +++ b/packages/sycamore-reactive/src/iter.rs @@ -23,7 +23,7 @@ pub fn map_keyed( list: impl Accessor> + Clone + 'static, map_fn: impl Fn(T) -> U + 'static, key_fn: impl Fn(&T) -> K + 'static, -) -> Memo> +) -> ReadSignal> where T: PartialEq + Clone + 'static, K: Eq + Hash, @@ -187,7 +187,7 @@ where pub fn map_indexed( list: impl Accessor> + Clone + 'static, map_fn: impl Fn(T) -> U + 'static, -) -> Memo> +) -> ReadSignal> where T: PartialEq + Clone + 'static, U: Clone, diff --git a/packages/sycamore-reactive/src/memos.rs b/packages/sycamore-reactive/src/memos.rs index 8d7b3907..14eb2a94 100644 --- a/packages/sycamore-reactive/src/memos.rs +++ b/packages/sycamore-reactive/src/memos.rs @@ -1,54 +1,8 @@ //! Memos (aka. eager derived signals). use std::cell::RefCell; -use std::fmt::{self, Formatter}; -use std::ops::Deref; -use crate::{create_empty_signal, create_signal, ReadSignal, Root, Signal}; - -/// A memoized derived signal. -/// -/// Usually created using [`create_memo`], [`create_selector`], and [`create_selector_with`]. -pub struct Memo { - signal: Signal, -} - -impl Memo { - /// Get the inner [`Signal`] that is backing this memo. - /// - /// Be careful when using this! Normally, you should not be able to update a memo manually - /// because that is already being done automatically. However, you can use this to create a - /// "writable memo", one which can be both updated manually and automatically. - pub fn inner_signal(self) -> Signal { - self.signal - } -} - -impl Deref for Memo { - type Target = ReadSignal; - - fn deref(&self) -> &Self::Target { - &self.signal - } -} - -impl Clone for Memo { - fn clone(&self) -> Self { - *self - } -} -impl Copy for Memo {} - -impl fmt::Debug for Memo { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - self.with(|value| value.fmt(f)) - } -} -impl fmt::Display for Memo { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - self.with(|value| value.fmt(f)) - } -} +use crate::{create_empty_signal, create_signal, ReadSignal, Root}; /// Creates a memoized value from some signals. /// Unlike [`create_memo`], this function will not notify dependents of a @@ -63,7 +17,7 @@ impl fmt::Display for Memo { pub fn create_selector_with( mut f: impl FnMut() -> T + 'static, mut eq: impl FnMut(&T, &T) -> bool + 'static, -) -> Memo { +) -> ReadSignal { let root = Root::global(); let signal = create_empty_signal(); let prev = root.current_node.replace(signal.id); @@ -85,7 +39,7 @@ pub fn create_selector_with( } })); - Memo { signal } + *signal } /// Creates a memoized computation from some signals. @@ -131,7 +85,7 @@ pub fn create_selector_with( /// # }); /// ``` #[cfg_attr(debug_assertions, track_caller)] -pub fn create_memo(f: impl FnMut() -> T + 'static) -> Memo { +pub fn create_memo(f: impl FnMut() -> T + 'static) -> ReadSignal { create_selector_with(f, |_, _| false) } @@ -159,7 +113,7 @@ pub fn create_memo(f: impl FnMut() -> T + 'static) -> Memo { /// # }); /// ``` #[cfg_attr(debug_assertions, track_caller)] -pub fn create_selector(f: impl FnMut() -> T + 'static) -> Memo +pub fn create_selector(f: impl FnMut() -> T + 'static) -> ReadSignal where T: PartialEq, { @@ -203,11 +157,11 @@ where pub fn create_reducer( initial: T, reduce: impl FnMut(&T, Msg) -> T, -) -> (Memo, impl Fn(Msg)) { +) -> (ReadSignal, impl Fn(Msg)) { let reduce = RefCell::new(reduce); let signal = create_signal(initial); let dispatch = move |msg| signal.update(|value| *value = reduce.borrow_mut()(value, msg)); - (Memo { signal }, dispatch) + (*signal, dispatch) } #[cfg(test)] diff --git a/packages/sycamore-reactive/src/signals.rs b/packages/sycamore-reactive/src/signals.rs index 1251fe99..ded67dca 100644 --- a/packages/sycamore-reactive/src/signals.rs +++ b/packages/sycamore-reactive/src/signals.rs @@ -10,7 +10,7 @@ use std::ops::{AddAssign, Deref, DivAssign, MulAssign, RemAssign, SubAssign}; use slotmap::Key; use smallvec::SmallVec; -use crate::{create_memo, Mark, Memo, NodeHandle, NodeId, NodeState, ReactiveNode, Root}; +use crate::{create_memo, Mark, NodeHandle, NodeId, NodeState, ReactiveNode, Root}; /// A read-only reactive value. /// @@ -368,7 +368,7 @@ impl Signal { } #[cfg_attr(debug_assertions, track_caller)] - pub fn map(self, mut f: impl FnMut(&T) -> U + 'static) -> Memo { + pub fn map(self, mut f: impl FnMut(&T) -> U + 'static) -> ReadSignal { create_memo(move || self.with(&mut f)) } diff --git a/packages/sycamore-reactive/src/utils.rs b/packages/sycamore-reactive/src/utils.rs index 3547ceda..59c7d5c7 100644 --- a/packages/sycamore-reactive/src/utils.rs +++ b/packages/sycamore-reactive/src/utils.rs @@ -25,12 +25,6 @@ impl Accessor for ReadSignal { } } -impl Accessor for Memo { - fn value(&self) -> T { - self.get_clone() - } -} - impl Accessor for T { fn value(&self) -> T { self.clone() @@ -72,12 +66,6 @@ impl Trackable for ReadSignal { } } -impl Trackable for Memo { - fn _track(&self) { - self.track(); - } -} - macro_rules! impl_trackable_deps_for_tuple { ($($T:tt),*) => { paste::paste! { diff --git a/packages/sycamore-router/src/router.rs b/packages/sycamore-router/src/router.rs index 1fd66374..1b8dcc1e 100644 --- a/packages/sycamore-router/src/router.rs +++ b/packages/sycamore-router/src/router.rs @@ -268,7 +268,7 @@ where } })); let route_signal = create_memo(move || pathname.with(|pathname| route.match_path(pathname))); - let view = view(*route_signal); + let view = view(route_signal); // Delegate click events from child tags. if let Some(node) = view.as_node() { node.event(ev::click, integration.click_handler()); diff --git a/packages/tools/bench/benches/reactivity.rs b/packages/tools/bench/benches/reactivity.rs index 033508e1..3b813a16 100644 --- a/packages/tools/bench/benches/reactivity.rs +++ b/packages/tools/bench/benches/reactivity.rs @@ -109,7 +109,7 @@ pub fn bench(c: &mut Criterion) { b.iter(|| { let d = create_root(|| { let signal = create_signal(0); - let mut memos = Vec::>::new(); + let mut memos = Vec::>::new(); for _ in 0..1000usize { if let Some(prev) = memos.last().copied() { memos.push(create_memo(move || prev.get() + 1));