#![cfg(feature = "async")]
use std::sync::Arc;
use tokio::sync::Mutex;
use crate::{Anomalyzer, AnomalyzerConf};
pub struct AsyncAnomalyzer {
inner: Arc<Mutex<Anomalyzer>>,
}
impl AsyncAnomalyzer {
pub async fn new(
conf: AnomalyzerConf,
initial_data: Option<Vec<f64>>,
) -> Result<Self, String> {
let inner = tokio::task::spawn_blocking(move || {
Anomalyzer::new(conf, initial_data)
})
.await
.map_err(|e| format!("task join error: {e}"))??;
Ok(Self {
inner: Arc::new(Mutex::new(inner)),
})
}
pub async fn push(&self, value: f64) -> f64 {
let inner = Arc::clone(&self.inner);
tokio::task::spawn_blocking(move || {
let mut guard = inner.blocking_lock();
guard.push(value)
})
.await
.unwrap_or(0.0)
}
pub async fn eval(&self) -> f64 {
let inner = Arc::clone(&self.inner);
tokio::task::spawn_blocking(move || {
let guard = inner.blocking_lock();
guard.eval()
})
.await
.unwrap_or(0.0)
}
}