Skip to content

Commit

Permalink
fix: implement dry_resolve on Suspend so that resources created ins…
Browse files Browse the repository at this point in the history
…ide a Suspend are registered (closes #2917) (#2940)
  • Loading branch information
gbj authored Sep 6, 2024
1 parent f3c57f8 commit 32bea69
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
1 change: 0 additions & 1 deletion tachys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ slotmap = { version = "1.0", optional = true }
oco_ref = { workspace = true, optional = true }
once_cell = "1.19"
paste = "1.0"
pin-project-lite = "0.2.14"
wasm-bindgen = "0.2.93"
html-escape = "0.2.13"
js-sys = "0.3.69"
Expand Down
55 changes: 20 additions & 35 deletions tachys/src/reactive_graph/suspense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
};
use any_spawner::Executor;
use futures::{select, FutureExt};
use pin_project_lite::pin_project;
use reactive_graph::{
computed::{
suspense::{LocalResourceNotifier, SuspenseContext},
Expand All @@ -27,20 +26,17 @@ use std::{
task::{Context, Poll},
};

pin_project! {
/// A suspended `Future`, which can be used in the view.
#[derive(Clone)]
pub struct Suspend<Fut> {
#[pin]
inner: ScopedFuture<Fut>
}
/// A suspended `Future`, which can be used in the view.
#[derive(Clone)]
pub struct Suspend<Fut> {
inner: Pin<Box<ScopedFuture<Fut>>>,
}

impl<Fut> Suspend<Fut> {
/// Creates a new suspended view.
pub fn new(fut: Fut) -> Self {
Self {
inner: ScopedFuture::new(fut),
inner: Box::pin(ScopedFuture::new(fut)),
}
}
}
Expand All @@ -51,21 +47,19 @@ where
{
type Output = Fut::Output;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
this.inner.poll(cx)
fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Self::Output> {
self.inner.as_mut().poll(cx)
}
}

impl<Fut> From<ScopedFuture<Fut>> for Suspend<Fut> {
fn from(inner: ScopedFuture<Fut>) -> Self {
Self { inner }
}
}

impl<Fut> From<Suspend<Fut>> for ScopedFuture<Fut> {
fn from(value: Suspend<Fut>) -> Self {
value.inner
Self {
inner: Box::pin(inner),
}
}
}

Expand Down Expand Up @@ -187,21 +181,10 @@ where
Self::Output<NewAttr>: RenderHtml<Rndr>,
{
let attr = attr.into_cloneable_owned();
let ScopedFuture {
owner,
observer,
fut,
} = self.into();
Suspend::from(ScopedFuture {
owner,
observer,
fut: Box::pin(async move {
let this = fut.await;
this.add_any_attr(attr)
}) as Pin<Box<dyn Future<
Output = <<Fut as Future>::Output as AddAnyAttr<Rndr>>::Output<<NewAttr as Attribute<Rndr>>::CloneableOwned>> + Send + 'static
>>
})
Suspend::new(Box::pin(async move {
let this = self.inner.await;
this.add_any_attr(attr)
}))
}
}

Expand Down Expand Up @@ -338,5 +321,7 @@ where
Some(self.await)
}

fn dry_resolve(&mut self) {}
fn dry_resolve(&mut self) {
self.inner.as_mut().now_or_never();
}
}

0 comments on commit 32bea69

Please sign in to comment.