use std::cell::RefCell;
use std::future::Future;
use super::drain::drain;
use super::push::take_records;
use super::state::{is_active, job_enabled, request_enabled, EMIT_BUFFER};
use super::types::BufferedEmit;
pub async fn request_scope<F, T>(fut: F) -> (T, Vec<BufferedEmit>)
where
F: Future<Output = T>,
{
request_scope_gated(request_enabled(), fut).await
}
pub async fn worker_scope<F, T>(fut: F) -> T
where
F: Future<Output = T>,
{
worker_scope_gated(job_enabled(), fut).await
}
pub(crate) async fn request_scope_gated<F, T>(enabled: bool, fut: F) -> (T, Vec<BufferedEmit>)
where
F: Future<Output = T>,
{
if !enabled || is_active() {
return (fut.await, Vec::new());
}
EMIT_BUFFER
.scope(RefCell::new(Vec::new()), async move {
let out = fut.await;
(out, take_records())
})
.await
}
pub(crate) async fn worker_scope_gated<F, T>(enabled: bool, fut: F) -> T
where
F: Future<Output = T>,
{
if !enabled || is_active() {
return fut.await;
}
EMIT_BUFFER
.scope(RefCell::new(Vec::new()), async move {
let out = fut.await;
drain(take_records());
out
})
.await
}