use std::future::Future;
use std::pin::Pin;
use crate::bus::MessagePublisher;
use crate::outbox::{OutboxMessage, OutboxPublishHook};
use crate::repository::RepositoryError;
use super::outbox_dispatch::publish_and_settle;
use super::OutboxStore;
pub struct BusOutboxPublishHook<S, P> {
store: S,
publisher: P,
max_attempts: u32,
}
impl<S, P> BusOutboxPublishHook<S, P> {
pub fn new(store: S, publisher: P, max_attempts: u32) -> Self {
Self {
store,
publisher,
max_attempts,
}
}
}
impl<S, P> OutboxPublishHook for BusOutboxPublishHook<S, P>
where
S: OutboxStore,
P: MessagePublisher,
{
fn publish_claimed<'a>(
&'a self,
claimed: Vec<OutboxMessage>,
) -> Pin<Box<dyn Future<Output = Result<(), RepositoryError>> + Send + 'a>> {
Box::pin(async move {
publish_and_settle(
&self.store,
&self.publisher,
claimed,
self.max_attempts,
std::num::NonZeroUsize::MIN,
)
.await
.map(|_outcome| ())
})
}
}