Skip to content

Commit

Permalink
From<> impls between ArcLocalResource and LocalResource
Browse files Browse the repository at this point in the history
  • Loading branch information
zakstucke committed Oct 22, 2024
1 parent 7b8cd90 commit dcb0d17
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions leptos_server/src/local_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
};

pub struct ArcLocalResource<T> {
data: ArcAsyncDerived<T>,
data: ArcAsyncDerived<SendWrapper<T>>,
#[cfg(debug_assertions)]
defined_at: &'static Location<'static>,
}
Expand Down Expand Up @@ -59,8 +59,12 @@ impl<T> ArcLocalResource<T> {
}
}
};
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(),
}
Expand All @@ -72,9 +76,14 @@ where
T: Clone + 'static,
{
type Output = T;
type IntoFuture = AsyncDerivedFuture<T>;
type IntoFuture = futures::future::Map<
AsyncDerivedFuture<SendWrapper<T>>,
fn(SendWrapper<T>) -> T,
>;

fn into_future(self) -> Self::IntoFuture {
use futures::FutureExt;

if let Some(mut notifier) = use_context::<LocalResourceNotifier>() {
notifier.notify();
} else if cfg!(feature = "ssr") {
Expand All @@ -84,7 +93,7 @@ where
always pending on the server."
);
}
self.data.into_future()
self.data.into_future().map(|value| (*value).clone())
}
}

Expand All @@ -105,7 +114,8 @@ impl<T> ReadUntracked for ArcLocalResource<T>
where
T: 'static,
{
type Value = ReadGuard<Option<T>, AsyncPlain<Option<T>>>;
type Value =
ReadGuard<Option<SendWrapper<T>>, AsyncPlain<Option<SendWrapper<T>>>>;

fn try_read_untracked(&self) -> Option<Self::Value> {
if let Some(mut notifier) = use_context::<LocalResourceNotifier>() {
Expand Down Expand Up @@ -373,3 +383,23 @@ where
self.data.clear_sources(subscriber);
}
}

impl<T: 'static> From<ArcLocalResource<T>> for LocalResource<T> {
fn from(arc: ArcLocalResource<T>) -> Self {
Self {
data: arc.data.into(),
#[cfg(debug_assertions)]
defined_at: arc.defined_at,
}
}
}

impl<T: 'static> From<LocalResource<T>> for ArcLocalResource<T> {
fn from(local: LocalResource<T>) -> Self {
Self {
data: local.data.into(),
#[cfg(debug_assertions)]
defined_at: local.defined_at,
}
}
}

0 comments on commit dcb0d17

Please sign in to comment.