Memos should actually memoize

This commit is contained in:
Greg Johnston 2022-09-01 10:38:10 -04:00
parent 44ec841522
commit 83bb874cd5
6 changed files with 38 additions and 8 deletions

View file

@ -8,13 +8,15 @@ where
pub fn create_memo<T>(cx: Scope, mut f: impl FnMut(Option<T>) -> T + 'static) -> Memo<T>
where
T: Clone + Debug + 'static,
T: PartialEq + Clone + Debug + 'static,
{
let (read, set) = create_signal(cx, None);
create_effect(cx, move |prev| {
let new = f(prev);
set(|n| *n = Some(new.clone()));
let new = f(prev.clone());
if prev.as_ref() != Some(&new) {
set(|n| *n = Some(new.clone()));
}
new
});

View file

@ -22,7 +22,7 @@ pub fn create_resource<S, T, Fu>(
fetcher: impl Fn(S) -> Fu + 'static,
) -> Resource<S, T>
where
S: Debug + Clone + 'static,
S: PartialEq + Debug + Clone + 'static,
T: Debug + Clone + 'static,
Fu: Future<Output = T> + 'static,
{
@ -36,7 +36,7 @@ pub fn create_resource_with_initial_value<S, T, Fu>(
initial_value: Option<T>,
) -> Resource<S, T>
where
S: Debug + Clone + 'static,
S: PartialEq + Debug + Clone + 'static,
T: Debug + Clone + 'static,
Fu: Future<Output = T> + 'static,
{
@ -231,7 +231,13 @@ where
let running_transition = self.scope.runtime.running_transition();
for suspense_context in suspense_contexts.borrow().iter() {
suspense_context.increment();
log::debug!(
"[Transition] resource: running transition? {}",
running_transition.is_some()
);
if let Some(transition) = &running_transition {
log::debug!("[Transition] adding resource");
transition
.resources
.borrow_mut()

View file

@ -40,7 +40,7 @@ where
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct RouteContext {
inner: Rc<RouteContextInner>,
}
@ -147,6 +147,16 @@ pub(crate) struct RouteContextInner {
pub(crate) outlet: Box<dyn Fn() -> Option<Child>>,
}
impl PartialEq for RouteContextInner {
fn eq(&self, other: &Self) -> bool {
self.cx == other.cx
&& self.base_path == other.base_path
&& self.path == other.path
&& self.original_path == other.original_path
&& self.params == other.params
}
}
impl std::fmt::Debug for RouteContextInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RouteContextInner")

View file

@ -13,3 +13,15 @@ pub enum RouterError {
#[error("failed to deserialize parameters")]
Params(Rc<dyn std::error::Error + Send + Sync>),
}
impl PartialEq for RouterError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::NoMatch(l0), Self::NoMatch(r0)) => l0 == r0,
(Self::NotFound(l0), Self::NotFound(r0)) => l0 == r0,
(Self::MissingParam(l0), Self::MissingParam(r0)) => l0 == r0,
(Self::Params(l0), Self::Params(r0)) => false,
_ => false,
}
}
}

View file

@ -1,6 +1,6 @@
use crate::ParamsMap;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Url {
pub origin: String,
pub pathname: String,

View file

@ -21,7 +21,7 @@ pub fn use_location(cx: Scope) -> Location {
pub fn use_params<T: Params>(cx: Scope) -> Memo<Result<T, RouterError>>
where
T: std::fmt::Debug + Clone,
T: PartialEq + std::fmt::Debug + Clone,
{
let route = use_route(cx);
create_memo(cx, move |_| T::from_map(&route.params()))