Skip to main content

Middleware

Trait Middleware 

Source
pub trait Middleware<S, A>
where A: Action,
{ // Required methods fn before(&mut self, action: &A, state: &S) -> bool; fn after(&mut self, action: &A, state_changed: bool, state: &S) -> Vec<A>; }
Expand description

Middleware trait for intercepting actions

Implement this trait to add logging, persistence, throttling, or other cross-cutting concerns to your store. Middleware can:

  • Observe: inspect actions and state (logging, analytics, persistence)
  • Cancel: return false from before() to prevent the action from reaching the reducer
  • Inject: return follow-up actions from after() that are dispatched through the full pipeline

§Cancel

Return false from before() to cancel the action — the reducer is never called and after() is not invoked. Useful for throttling, validation, and auth guards.

§Inject

Return actions from after() to trigger follow-up dispatches. Injected actions go through the full middleware + reducer pipeline. Dispatch limits prevent runaway loops.

Useful for cascading behavior: moving a card to “Done” triggers a notification, without the move reducer knowing about notifications.

Required Methods§

Source

fn before(&mut self, action: &A, state: &S) -> bool

Called before the action is dispatched to the reducer.

Return true to proceed with dispatch, false to cancel.

Source

fn after(&mut self, action: &A, state_changed: bool, state: &S) -> Vec<A>

Called after the action is processed by the reducer.

Return any follow-up actions to dispatch through the full pipeline.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl<S, A> Middleware<S, A> for DebugActionRecorder<A>
where A: Action,

Source§

fn before(&mut self, action: &A, _state: &S) -> bool

Source§

fn after(&mut self, _action: &A, _state_changed: bool, _state: &S) -> Vec<A>

Implementors§

Source§

impl<S, A> Middleware<S, A> for ActionLoggerMiddleware
where A: ActionParams,

Source§

impl<S, A> Middleware<S, A> for ComposedMiddleware<S, A>
where A: Action,

Source§

impl<S, A> Middleware<S, A> for NoopMiddleware
where A: Action,