use async_trait::async_trait;
use coreon_core::{Exchange, Processor, Result};
use futures::future::join_all;
use std::sync::Arc;
pub struct Multicast {
branches: Vec<Arc<dyn Processor>>,
}
impl Multicast {
pub fn new(branches: Vec<Arc<dyn Processor>>) -> Arc<Self> {
Arc::new(Self { branches })
}
}
#[async_trait]
impl Processor for Multicast {
async fn process(&self, exchange: &mut Exchange) -> Result<()> {
let futures: Vec<_> = self
.branches
.iter()
.map(|b| {
let mut copy = exchange.clone();
let b = b.clone();
async move { b.process(&mut copy).await }
})
.collect();
let results = join_all(futures).await;
for r in results {
r?;
}
Ok(())
}
}