#[macro_use]
mod macros;
pub mod dispatch;
pub mod json;
pub use dispatch::{
DispatchContext, EventDispatch, EventDispatcherContext, InvokeMode, ReceiveFn, SubscriberEntry,
SubscriberRegistry,
};
pub use json::{from_bytes, to_bytes};
pub use noema_macros::event;
use std::future::Future;
use std::sync::Arc;
pub type BoxDynError = Box<dyn std::error::Error + Send + Sync>;
pub type NoemaResult<T> = Result<T, BoxDynError>;
pub trait Event: Send + Sync + 'static {
const WIRE_NAME: &'static str;
fn name(&self) -> String {
Self::WIRE_NAME.to_string()
}
fn describe(&self) -> String {
format!("Event {}", Self::WIRE_NAME)
}
}
#[async_trait::async_trait]
pub trait EventPublishRaw: Send + Sync {
async fn publish_raw(&self, name: &str, payload: &[u8]) -> NoemaResult<()>;
}
#[async_trait::async_trait]
pub trait EventPublisher<E: Event + Send + Sync>: Send + Sync {
async fn publish(&self, event: E) -> NoemaResult<()>;
}
#[async_trait::async_trait]
pub trait EventListener<E: Event + Send + Sync + 'static>: Send + Sync {
fn name(&self) -> String {
std::any::type_name::<Self>().to_string()
}
async fn handle(&self, event: Arc<E>) -> NoemaResult<()>;
async fn on_error(
&self,
error_handler: Arc<dyn BackgroundErrorHandler + Send + Sync>,
error: BoxDynError,
_event: Arc<E>,
) {
error_handler.handle(error, E::WIRE_NAME, self.name()).await;
}
}
pub trait BackgroundSpawner: Send + Sync {
fn spawn(&self, fut: std::pin::Pin<Box<dyn Future<Output = ()> + Send + 'static>>);
}
#[async_trait::async_trait]
pub trait BackgroundErrorHandler: Send + Sync {
async fn handle(&self, error: BoxDynError, event_name: &str, handler_name: String);
}
#[cfg(test)]
mod tests;