use brk_error::Result;
use brk_mempool::Mempool;
use vecdb::ReadOnlyClone;
use tokio::task::spawn_blocking;
use crate::{Query, QueryPluginSet};
#[derive(Clone)]
pub struct AsyncQuery(Query);
impl AsyncQuery {
pub fn build<P>(plugins: &P, mempool: Option<Mempool>) -> Self
where
P: ReadOnlyClone,
P::ReadOnly: QueryPluginSet + 'static,
{
Self(Query::build(plugins, mempool))
}
pub async fn run<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(&Query) -> Result<T> + Send + 'static,
T: Send + 'static,
{
let query = self.0.clone();
spawn_blocking(move || f(&query)).await?
}
pub fn sync<F, T>(&self, f: F) -> T
where
F: FnOnce(&Query) -> T,
{
f(&self.0)
}
#[inline]
pub fn inner(&self) -> &Query {
&self.0
}
}