You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MiddlewareRegistry.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { applyMiddleware } from 'redux';
  2. /**
  3. * A registry for Redux middleware, allowing features to register their
  4. * middleware without needing to create additional inter-feature dependencies.
  5. */
  6. class MiddlewareRegistry {
  7. /**
  8. * Creates a MiddlewareRegistry instance.
  9. */
  10. constructor() {
  11. /**
  12. * The set of registered middleware.
  13. */
  14. this._elements = new Set();
  15. }
  16. /**
  17. * Applies all registered middleware into a store enhancer.
  18. * (@link http://redux.js.org/docs/api/applyMiddleware.html).
  19. *
  20. * @param {Function[]} additional - Any additional middleware that need to
  21. * be included (such as middleware from third-party modules).
  22. * @returns {Function}
  23. */
  24. applyMiddleware(...additional) {
  25. return applyMiddleware(
  26. ...this._elements,
  27. ...additional
  28. );
  29. }
  30. /**
  31. * Adds a middleware to the registry.
  32. *
  33. * The method is to be invoked only before {@link #applyMiddleware()}.
  34. *
  35. * @param {Function} middleware - A Redux middleware.
  36. * @returns {void}
  37. */
  38. register(middleware) {
  39. this._elements.add(middleware);
  40. }
  41. }
  42. /**
  43. * The public singleton instance of the MiddlewareRegistry class.
  44. */
  45. export default new MiddlewareRegistry();