Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repace the Memo struct with ReadSignal #632

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/todomvc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ pub fn List<G: Html>() -> View<G> {

ul(class="todo-list") {
Keyed(
iterable=*filtered_todos,
iterable=filtered_todos,
view=|todo| view! {
Item(todo=todo)
},
Expand Down
2 changes: 1 addition & 1 deletion packages/sycamore-core/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<G: GenericNode> View<G> {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new_dyn(f: impl FnMut() -> View<G> + 'static) -> Self {
Self {
inner: ViewType::Dyn(*create_memo(f).inner_signal()),
inner: ViewType::Dyn(create_memo(f)),
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/sycamore-reactive/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn map_keyed<T, K, U: 'static>(
list: impl Accessor<Vec<T>> + Clone + 'static,
map_fn: impl Fn(T) -> U + 'static,
key_fn: impl Fn(&T) -> K + 'static,
) -> Memo<Vec<U>>
) -> ReadSignal<Vec<U>>
where
T: PartialEq + Clone + 'static,
K: Eq + Hash,
Expand Down Expand Up @@ -187,7 +187,7 @@ where
pub fn map_indexed<T, U: 'static>(
list: impl Accessor<Vec<T>> + Clone + 'static,
map_fn: impl Fn(T) -> U + 'static,
) -> Memo<Vec<U>>
) -> ReadSignal<Vec<U>>
where
T: PartialEq + Clone + 'static,
U: Clone,
Expand Down
60 changes: 7 additions & 53 deletions packages/sycamore-reactive/src/memos.rs
Original file line number Diff line number Diff line change
@@ -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<T: 'static> {
signal: Signal<T>,
}

impl<T> Memo<T> {
/// 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<T> {
self.signal
}
}

impl<T> Deref for Memo<T> {
type Target = ReadSignal<T>;

fn deref(&self) -> &Self::Target {
&self.signal
}
}

impl<T> Clone for Memo<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Memo<T> {}

impl<T: fmt::Debug> fmt::Debug for Memo<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
self.with(|value| value.fmt(f))
}
}
impl<T: fmt::Display> fmt::Display for Memo<T> {
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
Expand All @@ -63,7 +17,7 @@ impl<T: fmt::Display> fmt::Display for Memo<T> {
pub fn create_selector_with<T>(
mut f: impl FnMut() -> T + 'static,
mut eq: impl FnMut(&T, &T) -> bool + 'static,
) -> Memo<T> {
) -> ReadSignal<T> {
let root = Root::global();
let signal = create_empty_signal();
let prev = root.current_node.replace(signal.id);
Expand All @@ -85,7 +39,7 @@ pub fn create_selector_with<T>(
}
}));

Memo { signal }
*signal
}

/// Creates a memoized computation from some signals.
Expand Down Expand Up @@ -131,7 +85,7 @@ pub fn create_selector_with<T>(
/// # });
/// ```
#[cfg_attr(debug_assertions, track_caller)]
pub fn create_memo<T>(f: impl FnMut() -> T + 'static) -> Memo<T> {
pub fn create_memo<T>(f: impl FnMut() -> T + 'static) -> ReadSignal<T> {
create_selector_with(f, |_, _| false)
}

Expand Down Expand Up @@ -159,7 +113,7 @@ pub fn create_memo<T>(f: impl FnMut() -> T + 'static) -> Memo<T> {
/// # });
/// ```
#[cfg_attr(debug_assertions, track_caller)]
pub fn create_selector<T>(f: impl FnMut() -> T + 'static) -> Memo<T>
pub fn create_selector<T>(f: impl FnMut() -> T + 'static) -> ReadSignal<T>
where
T: PartialEq,
{
Expand Down Expand Up @@ -203,11 +157,11 @@ where
pub fn create_reducer<T, Msg>(
initial: T,
reduce: impl FnMut(&T, Msg) -> T,
) -> (Memo<T>, impl Fn(Msg)) {
) -> (ReadSignal<T>, 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)]
Expand Down
4 changes: 2 additions & 2 deletions packages/sycamore-reactive/src/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -368,7 +368,7 @@ impl<T> Signal<T> {
}

#[cfg_attr(debug_assertions, track_caller)]
pub fn map<U>(self, mut f: impl FnMut(&T) -> U + 'static) -> Memo<U> {
pub fn map<U>(self, mut f: impl FnMut(&T) -> U + 'static) -> ReadSignal<U> {
create_memo(move || self.with(&mut f))
}

Expand Down
12 changes: 0 additions & 12 deletions packages/sycamore-reactive/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ impl<T: Clone> Accessor<T> for ReadSignal<T> {
}
}

impl<T: Clone> Accessor<T> for Memo<T> {
fn value(&self) -> T {
self.get_clone()
}
}

impl<T: Clone> Accessor<T> for T {
fn value(&self) -> T {
self.clone()
Expand Down Expand Up @@ -72,12 +66,6 @@ impl<T> Trackable for ReadSignal<T> {
}
}

impl<T> Trackable for Memo<T> {
fn _track(&self) {
self.track();
}
}

macro_rules! impl_trackable_deps_for_tuple {
($($T:tt),*) => {
paste::paste! {
Expand Down
2 changes: 1 addition & 1 deletion packages/sycamore-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a> tags.
if let Some(node) = view.as_node() {
node.event(ev::click, integration.click_handler());
Expand Down
2 changes: 1 addition & 1 deletion packages/tools/bench/benches/reactivity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Memo<usize>>::new();
let mut memos = Vec::<ReadSignal<usize>>::new();
for _ in 0..1000usize {
if let Some(prev) = memos.last().copied() {
memos.push(create_memo(move || prev.get() + 1));
Expand Down
Loading