use crate::core::{Effect, EnvRef, Exit};
use tokio::runtime::Runtime as TokioRuntime;
use tracing::Instrument;
pub struct Runtime {
pub(crate) rt: tokio::runtime::Runtime,
}
impl Default for Runtime {
fn default() -> Self {
Self::new()
}
}
impl Runtime {
pub fn new() -> Self {
Self {
rt: TokioRuntime::new().unwrap(),
}
}
pub fn block_on<R, E, A>(&self, effect: Effect<R, E, A>, env: R) -> Exit<E, A>
where
R: Send + Sync + 'static,
E: Send + Sync + 'static,
A: Send + Sync + 'static,
{
self.rt.block_on(async move {
let ctx = crate::core::Ctx::new();
let result = (effect.inner)(EnvRef { value: env }, ctx.clone())
.instrument(tracing::info_span!("runtime_execution"))
.await;
ctx.scope.close(crate::core::ScopeExit::from(&result)).await;
result
})
}
}