use std::future::Future;
use std::sync::Arc;
use navree_config::__private::register_driver;
use navree_config::{DriverContext, DriverSlot};
use navree_kernel::{BoxError, ModuleBuilder};
use crate::QueueStore;
pub(crate) const SLOT: DriverSlot = DriverSlot::new(
"queue.driver",
&["database", "memory", "redis"],
"m.queue_store",
"queue store",
);
pub(crate) struct CheckedStores;
pub trait ModuleBuilderQueueExt {
fn queue_store<S, F, Fut>(&mut self, name: &'static str, factory: F) -> &mut Self
where
S: QueueStore,
F: Fn(DriverContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<S, BoxError>> + Send + 'static;
}
impl ModuleBuilderQueueExt for ModuleBuilder {
fn queue_store<S, F, Fut>(&mut self, name: &'static str, factory: F) -> &mut Self
where
S: QueueStore,
F: Fn(DriverContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<S, BoxError>> + Send + 'static,
{
register_driver::<dyn QueueStore, _, _>(self, &SLOT, Some("queue"), name, move |ctx| {
let built = factory(ctx);
async move {
built
.await
.map(|store| Arc::new(store) as Arc<dyn QueueStore>)
}
})
}
}