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.8KB

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