Skip to main content

Mediator

Struct Mediator 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Mediator

Source§

fn clone(&self) -> Mediator

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Mediator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more