From dcb0d1707acef42518f0e4483804390ca4e06550 Mon Sep 17 00:00:00 2001 From: Zak Stucke Date: Tue, 22 Oct 2024 19:53:56 +0300 Subject: [PATCH] From<> impls between ArcLocalResource and LocalResource --- leptos_server/src/local_resource.rs | 40 +++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/leptos_server/src/local_resource.rs b/leptos_server/src/local_resource.rs index c974d280e6..66be092a58 100644 --- a/leptos_server/src/local_resource.rs +++ b/leptos_server/src/local_resource.rs @@ -18,7 +18,7 @@ use std::{ }; pub struct ArcLocalResource { - data: ArcAsyncDerived, + data: ArcAsyncDerived>, #[cfg(debug_assertions)] defined_at: &'static Location<'static>, } @@ -59,8 +59,12 @@ impl ArcLocalResource { } } }; + let fetcher = SendWrapper::new(fetcher); Self { - data: ArcAsyncDerived::new_unsync(fetcher), + data: ArcAsyncDerived::new(move || { + let fut = fetcher(); + SendWrapper::new(async move { SendWrapper::new(fut.await) }) + }), #[cfg(debug_assertions)] defined_at: Location::caller(), } @@ -72,9 +76,14 @@ where T: Clone + 'static, { type Output = T; - type IntoFuture = AsyncDerivedFuture; + type IntoFuture = futures::future::Map< + AsyncDerivedFuture>, + fn(SendWrapper) -> T, + >; fn into_future(self) -> Self::IntoFuture { + use futures::FutureExt; + if let Some(mut notifier) = use_context::() { notifier.notify(); } else if cfg!(feature = "ssr") { @@ -84,7 +93,7 @@ where always pending on the server." ); } - self.data.into_future() + self.data.into_future().map(|value| (*value).clone()) } } @@ -105,7 +114,8 @@ impl ReadUntracked for ArcLocalResource where T: 'static, { - type Value = ReadGuard, AsyncPlain>>; + type Value = + ReadGuard>, AsyncPlain>>>; fn try_read_untracked(&self) -> Option { if let Some(mut notifier) = use_context::() { @@ -373,3 +383,23 @@ where self.data.clear_sources(subscriber); } } + +impl From> for LocalResource { + fn from(arc: ArcLocalResource) -> Self { + Self { + data: arc.data.into(), + #[cfg(debug_assertions)] + defined_at: arc.defined_at, + } + } +} + +impl From> for ArcLocalResource { + fn from(local: LocalResource) -> Self { + Self { + data: local.data.into(), + #[cfg(debug_assertions)] + defined_at: local.defined_at, + } + } +}