#[derive(Debug)]
pub struct SpecialNonReactiveZone;
#[derive(Debug)]
pub struct SpecialNonReactiveZoneGuard(bool);
use pin_project_lite::pin_project;
use std::{
cell::Cell,
future::Future,
pin::Pin,
task::{Context, Poll},
};
thread_local! {
static IS_SPECIAL_ZONE: Cell<bool> = const { Cell::new(false) };
}
impl SpecialNonReactiveZone {
pub fn enter() -> SpecialNonReactiveZoneGuard {
let prev = IS_SPECIAL_ZONE.replace(true);
SpecialNonReactiveZoneGuard(prev)
}
#[cfg(all(debug_assertions, feature = "effects"))]
#[inline(always)]
pub(crate) fn is_inside() -> bool {
if cfg!(debug_assertions) {
IS_SPECIAL_ZONE.get()
} else {
false
}
}
}
impl Drop for SpecialNonReactiveZoneGuard {
fn drop(&mut self) {
IS_SPECIAL_ZONE.set(self.0);
}
}
pin_project! {
#[doc(hidden)]
pub struct SpecialNonReactiveFuture<Fut> {
#[pin]
inner: Fut
}
}
impl<Fut> SpecialNonReactiveFuture<Fut> {
pub fn new(inner: Fut) -> Self {
Self { inner }
}
}
impl<Fut> Future for SpecialNonReactiveFuture<Fut>
where
Fut: Future,
{
type Output = Fut::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
#[cfg(debug_assertions)]
let _rw = SpecialNonReactiveZone::enter();
let this = self.project();
this.inner.poll(cx)
}
}
thread_local! {
static SUPPRESS_RESOURCE_LOAD: Cell<bool> = const { Cell::new(false) };
}
#[doc(hidden)]
pub fn suppress_resource_load(suppress: bool) {
SUPPRESS_RESOURCE_LOAD.with(|w| w.set(suppress));
}
#[doc(hidden)]
pub fn is_suppressing_resource_load() -> bool {
SUPPRESS_RESOURCE_LOAD.with(|w| w.get())
}