From 32bea69c288dced433bcc670facbdfda00955cf2 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Fri, 6 Sep 2024 14:49:37 -0400 Subject: [PATCH] fix: implement `dry_resolve` on Suspend so that resources created inside a Suspend are registered (closes #2917) (#2940) --- tachys/Cargo.toml | 1 - tachys/src/reactive_graph/suspense.rs | 55 ++++++++++----------------- 2 files changed, 20 insertions(+), 36 deletions(-) diff --git a/tachys/Cargo.toml b/tachys/Cargo.toml index 48ac3d4f2..6a6bc8104 100644 --- a/tachys/Cargo.toml +++ b/tachys/Cargo.toml @@ -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" diff --git a/tachys/src/reactive_graph/suspense.rs b/tachys/src/reactive_graph/suspense.rs index 9b8c95a71..c2d9333f6 100644 --- a/tachys/src/reactive_graph/suspense.rs +++ b/tachys/src/reactive_graph/suspense.rs @@ -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}, @@ -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 { - #[pin] - inner: ScopedFuture - } +/// A suspended `Future`, which can be used in the view. +#[derive(Clone)] +pub struct Suspend { + inner: Pin>>, } impl Suspend { /// Creates a new suspended view. pub fn new(fut: Fut) -> Self { Self { - inner: ScopedFuture::new(fut), + inner: Box::pin(ScopedFuture::new(fut)), } } } @@ -51,21 +47,19 @@ where { type Output = Fut::Output; - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.project(); - this.inner.poll(cx) + fn poll( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll { + self.inner.as_mut().poll(cx) } } impl From> for Suspend { fn from(inner: ScopedFuture) -> Self { - Self { inner } - } -} - -impl From> for ScopedFuture { - fn from(value: Suspend) -> Self { - value.inner + Self { + inner: Box::pin(inner), + } } } @@ -187,21 +181,10 @@ where Self::Output: RenderHtml, { 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::Output as AddAnyAttr>::Output<>::CloneableOwned>> + Send + 'static - >> - }) + Suspend::new(Box::pin(async move { + let this = self.inner.await; + this.add_any_attr(attr) + })) } } @@ -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(); + } }