Middleware

  • The definition and signature of Middleware is consistent with the ReduxJS community.
  • Sample Code
  1. Middleware<T> logMiddleware<T>({
  2. String tag = 'redux',
  3. String Function(T) monitor,
  4. }) {
  5. return ({Dispatch dispatch, Get<T> getState}) {
  6. return (Dispatch next) {
  7. return isDebug()
  8. ? (Action action) {
  9. print('---------- [$tag] ----------');
  10. print('[$tag] ${action.type} ${action.payload}');
  11.  
  12. final T prevState = getState();
  13. if (monitor != null) {
  14. print('[$tag] prev-state: ${monitor(prevState)}');
  15. }
  16.  
  17. next(action);
  18.  
  19. final T nextState = getState();
  20. if (monitor != null) {
  21. print('[$tag] next-state: ${monitor(nextState)}');
  22. }
  23.  
  24. if (prevState == nextState) {
  25. print('[$tag] warning: ${action.type} has not been used.');
  26. }
  27.  
  28. print('========== [$tag] ================');
  29. }
  30. : next;
  31. };
  32. };
  33. }

更多的参考 src/utils/common_middleware