2022-12-06 15:45:09 +00:00
|
|
|
use std::{future::Future, time::Duration};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum Executor {
|
|
|
|
Production,
|
|
|
|
#[cfg(test)]
|
|
|
|
Deterministic(std::sync::Arc<gpui::executor::Background>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Executor {
|
|
|
|
pub fn spawn_detached<F>(&self, future: F)
|
|
|
|
where
|
|
|
|
F: 'static + Send + Future<Output = ()>,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
Executor::Production => {
|
|
|
|
tokio::spawn(future);
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
Executor::Deterministic(background) => {
|
|
|
|
background.spawn(future).detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sleep(&self, duration: Duration) -> impl Future<Output = ()> {
|
|
|
|
let this = self.clone();
|
|
|
|
async move {
|
|
|
|
match this {
|
|
|
|
Executor::Production => tokio::time::sleep(duration).await,
|
|
|
|
#[cfg(test)]
|
|
|
|
Executor::Deterministic(background) => background.timer(duration).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-24 01:34:13 +00:00
|
|
|
|
|
|
|
pub fn record_backtrace(&self) {
|
|
|
|
match self {
|
|
|
|
Executor::Production => {}
|
|
|
|
#[cfg(test)]
|
|
|
|
Executor::Deterministic(background) => background.record_backtrace(),
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 15:45:09 +00:00
|
|
|
}
|