pub struct Mediator { /* private fields */ }Expand description
In-process dispatcher for commands, queries and notifications.
Construct one with MediatorBuilder, clone it freely (the registry is
shared behind an Arc), and call Mediator::send, Mediator::query
or Mediator::publish from anywhere in your async runtime.
Implementations§
Source§impl Mediator
impl Mediator
Sourcepub async fn send<C: Command>(
&self,
command: C,
) -> Result<C::Output, HexeractError>
pub async fn send<C: Command>( &self, command: C, ) -> Result<C::Output, HexeractError>
Dispatches a Command to its registered handler and returns the
handler’s output.
A fresh CorrelationId is minted for this dispatch. To continue an
existing causal chain (e.g. from inside another handler), use
Mediator::send_with_correlation_id instead.
§Errors
Returns HexeractError::HandlerNotFound if no handler is
registered for C, or the handler’s own error converted into
HexeractError when the handler itself fails.
Sourcepub async fn send_with_correlation_id<C: Command>(
&self,
command: C,
correlation_id: CorrelationId,
) -> Result<C::Output, HexeractError>
pub async fn send_with_correlation_id<C: Command>( &self, command: C, correlation_id: CorrelationId, ) -> Result<C::Output, HexeractError>
Dispatches a Command to its registered handler using the supplied
CorrelationId, preserving the causal chain across in-process
dispatches.
Use this variant when the caller already holds a CorrelationId
(for example ctx.correlation_id inside a handler) and wants all
follow-up messages to share the same identifier. The plain
Mediator::send method is equivalent to calling this with
CorrelationId::new().
§Errors
Returns HexeractError::HandlerNotFound if no handler is
registered for C, or the handler’s own error converted into
HexeractError when the handler itself fails.
Sourcepub async fn query<Q: Query>(
&self,
query: Q,
) -> Result<Q::Output, HexeractError>
pub async fn query<Q: Query>( &self, query: Q, ) -> Result<Q::Output, HexeractError>
Dispatches a Query to its registered handler and returns the
handler’s output.
A fresh CorrelationId is minted for this dispatch. To continue an
existing causal chain, use Mediator::query_with_correlation_id
instead.
§Errors
Returns HexeractError::HandlerNotFound if no handler is
registered for Q, or the handler’s own error converted into
HexeractError when the handler itself fails.
Sourcepub async fn query_with_correlation_id<Q: Query>(
&self,
query: Q,
correlation_id: CorrelationId,
) -> Result<Q::Output, HexeractError>
pub async fn query_with_correlation_id<Q: Query>( &self, query: Q, correlation_id: CorrelationId, ) -> Result<Q::Output, HexeractError>
Dispatches a Query to its registered handler using the supplied
CorrelationId, preserving the causal chain across in-process
dispatches.
Use this variant when the caller already holds a CorrelationId
(for example ctx.correlation_id inside a handler) and wants all
follow-up messages to share the same identifier. The plain
Mediator::query method is equivalent to calling this with
CorrelationId::new().
§Errors
Returns HexeractError::HandlerNotFound if no handler is
registered for Q, or the handler’s own error converted into
HexeractError when the handler itself fails.
Sourcepub async fn publish<N: Notification>(
&self,
notification: N,
) -> Result<(), HexeractError>
pub async fn publish<N: Notification>( &self, notification: N, ) -> Result<(), HexeractError>
Publishes a Notification to every handler registered for N.
A notification with zero handlers is a no-op.
A fresh CorrelationId is minted for this dispatch. To continue an
existing causal chain, use Mediator::publish_with_correlation_id
instead.
Handlers are dispatched concurrently: every handler future is built
up front and driven together with join_all, so a slow handler no
longer blocks the handlers registered after it. The fan-out is
cooperative and runtime-agnostic: it runs on the caller’s task without
spawning, so a CPU-bound handler that never .awaits still blocks its
siblings until it yields. Every handler shares the same
CorrelationId so traces can link the fan-out to its source publish
call, but each handler receives a dedicated MessageId.
§Ordering
Execution is interleaved, so the order in which handlers observe the
notification is unspecified. Only the collection order is
deterministic: the NotificationFailures aggregated on failure
appear in handler registration order.
§Errors
Every handler runs regardless of its siblings. If one or more fail,
their typed errors are aggregated into HexeractError::PublishFailed,
each NotificationFailure retaining the failing handler’s name and
the error source chain so the caller can recover an individual
failure instead of parsing a flattened string.
Sourcepub async fn publish_with_correlation_id<N: Notification>(
&self,
notification: N,
correlation_id: CorrelationId,
) -> Result<(), HexeractError>
pub async fn publish_with_correlation_id<N: Notification>( &self, notification: N, correlation_id: CorrelationId, ) -> Result<(), HexeractError>
Publishes a Notification to every handler registered for N using
the supplied CorrelationId, preserving the causal chain across
in-process dispatches.
Use this variant when the caller already holds a CorrelationId
(for example ctx.correlation_id inside a handler) and wants all
follow-up messages to share the same identifier. Every handler in the
fan-out receives the same supplied id and a dedicated MessageId.
The plain Mediator::publish method is equivalent to calling this
with CorrelationId::new().
§Ordering
See Mediator::publish for ordering and concurrency guarantees.
§Errors
See Mediator::publish for error aggregation semantics.