ddd_toolkit_core/
async_dispatch.rs1use crate::event_restoration_guard::EventRestorationGuard;
2use crate::{AggregateRoot, AsyncDomainEventPublisher};
3use std::future::Future;
4use std::pin::Pin;
5
6pub fn dispatch_events_async<'a, A, P>(
18 aggregate: &'a mut A,
19 publisher: &'a mut P,
20) -> Pin<Box<dyn Future<Output = Result<(), P::Error>> + Send + 'a>>
21where
22 A: AggregateRoot + Send + 'a,
23 A::Event: Send + Sync + 'a,
24 P: AsyncDomainEventPublisher<Event = A::Event> + Send + 'a,
25 P::Error: 'a,
26{
27 Box::pin(async move {
28 let snapshot = aggregate.pull_events();
29 let mut guard = EventRestorationGuard::new(aggregate, snapshot);
30
31 for event in guard.events() {
32 publisher.publish(event).await?
33 }
34
35 guard.commit();
36 Ok(())
37 })
38}