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.

ReducerRegistry.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { combineReducers } from 'redux';
  2. /**
  3. * A registry for Redux reducers, allowing features to register themselves
  4. * without needing to create additional inter-feature dependencies.
  5. */
  6. class ReducerRegistry {
  7. /**
  8. * Creates a ReducerRegistry instance.
  9. */
  10. constructor() {
  11. /**
  12. * The set of registered reducers, keyed based on the field each reducer
  13. * will manage.
  14. */
  15. this._elements = {};
  16. }
  17. /**
  18. * Combines all registered reducers into a single reducing function.
  19. *
  20. * @param {Object} [additional={}] - Any additional reducers that need to be
  21. * included (such as reducers from third-party modules).
  22. * @returns {Function}
  23. */
  24. combineReducers(additional = {}) {
  25. return combineReducers({
  26. ...this._elements,
  27. ...additional
  28. });
  29. }
  30. /**
  31. * Adds a reducer to the registry.
  32. *
  33. * The method is to be invoked only before {@link #combineReducers()}.
  34. *
  35. * @param {string} name - The field in the state object that will be managed
  36. * by the provided reducer.
  37. * @param {Function} reducer - A Redux reducer.
  38. * @returns {void}
  39. */
  40. register(name, reducer) {
  41. this._elements[name] = reducer;
  42. }
  43. }
  44. /**
  45. * The public singleton instance of the ReducerRegistry class.
  46. */
  47. export default new ReducerRegistry();