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
falsefrombefore()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. A recursion depth limit prevents infinite loops.
Useful for cascading behavior: moving a card to “Done” triggers a notification, without the move reducer knowing about notifications.