Skip to main content

Middleware

Trait Middleware 

Source
pub trait Middleware:
    Send
    + Sync
    + 'static
    + Send {
    // Required method
    fn execute(
        &self,
        envelope: &MessageEnvelope,
        ctx: &HandlerContext,
        next: Next,
    ) -> impl Future<Output = Result<BoxOutput, HexeractError>> + Send;
}
Expand description

Intercepts a dispatch before reaching its handler.

Middlewares are stacked onion-style: the first registered middleware wraps all the others, observing both the entry and the exit of every dispatch.

§Example

use hexeract_core::{BoxOutput, HandlerContext, HexeractError, MessageEnvelope, Middleware, Next};

struct LoggingMiddleware;

impl Middleware for LoggingMiddleware {
    async fn execute(
        &self,
        envelope: &MessageEnvelope,
        ctx: &HandlerContext,
        next: Next,
    ) -> Result<BoxOutput, HexeractError> {
        tracing::info!(type_name = envelope.type_name(), "dispatching");
        let result = next.run(envelope, ctx).await;
        tracing::info!(type_name = envelope.type_name(), "dispatched");
        result
    }
}

Required Methods§

Source

fn execute( &self, envelope: &MessageEnvelope, ctx: &HandlerContext, next: Next, ) -> impl Future<Output = Result<BoxOutput, HexeractError>> + Send

Executes the middleware. The implementation must call next.run(...) to proceed to the next middleware or terminal, unless it intentionally short-circuits the chain.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§