Skip to main content

Middleware

Trait Middleware 

Source
pub trait Middleware<S, 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.

Implementors§

Source§

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

Source§

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