make Suspend a transparent wrapper

This commit is contained in:
Greg Johnston 2024-04-27 14:02:17 -04:00
parent 420dccda60
commit 9bab4da172
3 changed files with 10 additions and 12 deletions

View file

@ -2,7 +2,7 @@ use lazy_static::lazy_static;
use leptos::{
component, prelude::*, reactive_graph::computed::AsyncDerived, server,
server::Resource, server_fn::ServerFnError, suspend, view, ErrorBoundary,
IntoView, Params, Suspense,
IntoView, Params, Suspense, Suspend
};
use leptos_meta::*;
use leptos_router::{
@ -52,7 +52,7 @@ pub fn App() -> impl IntoView {
fn HomePage() -> impl IntoView {
// load the posts
let posts = Resource::new_serde(|| (), |_| list_post_metadata());
let posts_view = suspend!(
let posts_view = Suspend(async move {
posts.await.map(|posts| {
posts.into_iter()
.map(|post| view! {
@ -64,7 +64,7 @@ fn HomePage() -> impl IntoView {
})
.collect::<Vec<_>>()
})
);
});
view! {
<h1>"My Great Blog"</h1>

View file

@ -177,7 +177,7 @@ pub use reactive_graph::{
};
pub use server_fn;
pub use show::*;
pub use suspense_component::Suspense;
pub use suspense_component::{Suspend, Suspense};
pub use throw_error as error;
pub use transition::*;
#[doc(hidden)]

View file

@ -234,7 +234,7 @@ pub trait FutureViewExt: Sized {
where
Self: Future,
{
Suspend { fut: self }
Suspend(self)
}
}
@ -247,9 +247,7 @@ macro_rules! suspend {
};
}
pub struct Suspend<Fut> {
pub fut: Fut,
}
pub struct Suspend<Fut>(pub Fut);
impl<Fut> Debug for Suspend<Fut> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -300,7 +298,7 @@ where
// poll the future once immediately
// if it's already available, start in the ready state
// otherwise, start with the fallback
let mut fut = Box::pin(ScopedFuture::new(self.fut));
let mut fut = Box::pin(ScopedFuture::new(self.0));
let initial = fut.as_mut().now_or_never();
let initially_pending = initial.is_none();
let inner = Rc::new(RefCell::new(initial.build()));
@ -327,7 +325,7 @@ where
fn rebuild(self, state: &mut Self::State) {
// get a unique ID if there's a SuspenseContext
let fut = ScopedFuture::new(self.fut);
let fut = ScopedFuture::new(self.0);
let id = use_context::<SuspenseContext>().map(|sc| sc.task_id());
// spawn the future, and rebuild the state when it resolves
@ -375,7 +373,7 @@ where
// poll the future once immediately
// if it's already available, start in the ready state
// otherwise, start with the fallback
let mut fut = Box::pin(ScopedFuture::new(self.fut));
let mut fut = Box::pin(ScopedFuture::new(self.0));
let initial = fut.as_mut().now_or_never();
let initially_pending = initial.is_none();
let inner = Rc::new(RefCell::new(
@ -403,6 +401,6 @@ where
}
async fn resolve(self) -> Self::AsyncOutput {
self.fut.await
self.0.await
}
}