Middleware

关于 Middleware 的定义、签名和 ReduxJS 社区是一致的。

示例代码

  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. final T prevState = getState();
  12. if (monitor != null) {
  13. print('[$tag] prev-state: ${monitor(prevState)}');
  14. }
  15. next(action);
  16. final T nextState = getState();
  17. if (monitor != null) {
  18. print('[$tag] next-state: ${monitor(nextState)}');
  19. }
  20. if (prevState == nextState) {
  21. print('[$tag] warning: ${action.type} has not been used.');
  22. }
  23. print('========== [$tag] ================');
  24. }
  25. : next;
  26. };
  27. };
  28. }

更多的参考 src/utils/common_middleware