2017-02-03 18:50:06 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
import { combineReducers } from 'redux';
|
2017-02-03 18:50:06 +00:00
|
|
|
import type { Reducer } from 'redux';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of the dictionary/map which associates a reducer (function) with the
|
|
|
|
* name of he Redux state property managed by the reducer.
|
|
|
|
*/
|
|
|
|
declare type NameReducerMap<S, A> = { [name: string]: Reducer<S, A> };
|
2016-10-05 14:36:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A registry for Redux reducers, allowing features to register themselves
|
|
|
|
* without needing to create additional inter-feature dependencies.
|
|
|
|
*/
|
|
|
|
class ReducerRegistry {
|
2017-02-03 18:50:06 +00:00
|
|
|
_elements: NameReducerMap<*, *>;
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Creates a ReducerRegistry instance.
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
/**
|
|
|
|
* The set of registered reducers, keyed based on the field each reducer
|
|
|
|
* will manage.
|
2017-02-03 18:50:06 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @type {NameReducerMap}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2017-01-28 23:28:13 +00:00
|
|
|
this._elements = {};
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Combines all registered reducers into a single reducing function.
|
|
|
|
*
|
|
|
|
* @param {Object} [additional={}] - Any additional reducers that need to be
|
|
|
|
* included (such as reducers from third-party modules).
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
2017-02-03 18:50:06 +00:00
|
|
|
combineReducers(additional: NameReducerMap<*, *> = {}) {
|
2019-03-19 15:42:25 +00:00
|
|
|
// $FlowExpectedError
|
2016-10-05 14:36:59 +00:00
|
|
|
return combineReducers({
|
2017-01-28 23:28:13 +00:00
|
|
|
...this._elements,
|
2016-10-05 14:36:59 +00:00
|
|
|
...additional
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a reducer to the registry.
|
|
|
|
*
|
|
|
|
* The method is to be invoked only before {@link #combineReducers()}.
|
|
|
|
*
|
|
|
|
* @param {string} name - The field in the state object that will be managed
|
|
|
|
* by the provided reducer.
|
2017-02-03 18:50:06 +00:00
|
|
|
* @param {Reducer} reducer - A Redux reducer.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-02-03 18:50:06 +00:00
|
|
|
register(name: string, reducer: Reducer<*, *>) {
|
2017-01-28 23:28:13 +00:00
|
|
|
this._elements[name] = reducer;
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The public singleton instance of the ReducerRegistry class.
|
|
|
|
*/
|
|
|
|
export default new ReducerRegistry();
|