Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 18 additions & 38 deletions reactive_graph/src/computed/async_derived/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ pin_project! {
#[derive(Clone)]
#[allow(missing_docs)]
pub struct ScopedFuture<Fut> {
pub owner: Owner,
pub observer: Option<AnySubscriber>,
owner: Owner,
observer: Option<AnySubscriber>,
diagnostics: bool,
#[pin]
pub fut: Fut,
fut: Fut,
}
}

Expand All @@ -39,6 +40,7 @@ impl<Fut> ScopedFuture<Fut> {
Self {
owner,
observer,
diagnostics: true,
fut,
}
}
Expand All @@ -51,19 +53,19 @@ impl<Fut> ScopedFuture<Fut> {
Self {
owner,
observer: None,
diagnostics: false,
fut,
}
}

#[doc(hidden)]
#[track_caller]
pub fn new_untracked_with_diagnostics(
fut: Fut,
) -> ScopedFutureUntrackedWithDiagnostics<Fut> {
pub fn new_untracked_with_diagnostics(fut: Fut) -> Self {
let owner = Owner::current().unwrap_or_default();
ScopedFutureUntrackedWithDiagnostics {
Self {
owner,
observer: None,
diagnostics: true,
fut,
}
}
Expand All @@ -75,41 +77,19 @@ impl<Fut: Future> Future for ScopedFuture<Fut> {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
this.owner.with(|| {
#[cfg(debug_assertions)]
let _maybe_guard = if this.observer.is_none() {
Some(crate::diagnostics::SpecialNonReactiveZone::enter())
} else {
None
};
this.observer.with_observer(|| this.fut.poll(cx))
this.observer.with_observer(|| {
#[cfg(debug_assertions)]
let _maybe_guard = if *this.diagnostics {
None
} else {
Some(crate::diagnostics::SpecialNonReactiveZone::enter())
};
this.fut.poll(cx)
})
})
}
}

pin_project! {
/// A [`Future`] wrapper that sets the [`Owner`] and [`Observer`] before polling the inner
/// `Future`, output of [`ScopedFuture::new_untracked_with_diagnostics`].
///
/// In leptos 0.9 this will be replaced with `ScopedFuture` itself.
#[derive(Clone)]
pub struct ScopedFutureUntrackedWithDiagnostics<Fut> {
owner: Owner,
observer: Option<AnySubscriber>,
#[pin]
fut: Fut,
}
}

impl<Fut: Future> Future for ScopedFutureUntrackedWithDiagnostics<Fut> {
type Output = Fut::Output;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
this.owner
.with(|| this.observer.with_observer(|| this.fut.poll(cx)))
}
}

/// Utilities used to track whether asynchronous computeds are currently loading.
pub mod suspense {
use crate::{
Expand Down
Loading