Interface Middleware



  • public interface Middleware
    Middleware definition.
    A middleware can be used to implement
    • Logging that is called before and after the dispatcher
    • Asynchronous operations by consuming an Action
    • And probably a million more use-cases I can't think about right now
    • Method Detail

      • onAction

        void onAction(Action<?> action,
                      GetState state,
                      Dispatcher dispatcher,
                      Continuation continuation)
        Called before an Action gets passed to the Reducers.

        Examples: Log state changes:

         void onAction(...) {
            State oldState = state.getState();
            continuation.next(action);
            State newState = state.getState();
        
            System.out.println(stateDiff(oldState, newState));
         }
         
        Consume an Action and dispatch it on a background thread:
         void onAction(...) {
            if(action.getData() instanceof AsyncAction) {
                // consume action
                backgroundTask.start(action.getData, dispatcher);
            } else {
                continuation.next(action);
            }
         }
         
        Parameters:
        action - a dispatched action
        state - access to the current state
        dispatcher - access to the dispatcher for dispatching a new action
        continuation - callback for passing the action to the next middleware