Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ReducerRegistry.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // $FlowExpectedError
  37. return combineReducers({
  38. ...this._elements,
  39. ...additional
  40. });
  41. }
  42. /**
  43. * Adds a reducer to the registry.
  44. *
  45. * The method is to be invoked only before {@link #combineReducers()}.
  46. *
  47. * @param {string} name - The field in the state object that will be managed
  48. * by the provided reducer.
  49. * @param {Reducer} reducer - A Redux reducer.
  50. * @returns {void}
  51. */
  52. register(name: string, reducer: Reducer<*, *>) {
  53. this._elements[name] = reducer;
  54. }
  55. }
  56. /**
  57. * The public singleton instance of the ReducerRegistry class.
  58. */
  59. export default new ReducerRegistry();