Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ReducerRegistry.ts 1.8KB

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