2022-09-27 07:10:28 +00:00
|
|
|
import { Middleware, applyMiddleware } from 'redux';
|
2017-02-02 15:45:34 +00:00
|
|
|
|
2022-10-20 09:11:27 +00:00
|
|
|
import { IReduxState, IStore } from '../../app/types';
|
2022-09-14 11:32:58 +00:00
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* A registry for Redux middleware, allowing features to register their
|
|
|
|
* middleware without needing to create additional inter-feature dependencies.
|
|
|
|
*/
|
|
|
|
class MiddlewareRegistry {
|
2022-07-15 12:33:09 +00:00
|
|
|
_elements: Array<Middleware<any, any>>;
|
2017-02-02 15:45:34 +00:00
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Creates a MiddlewareRegistry instance.
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
/**
|
|
|
|
* The set of registered middleware.
|
2017-02-02 15:45:34 +00:00
|
|
|
*
|
|
|
|
* @private
|
2017-02-03 16:17:20 +00:00
|
|
|
* @type {Middleware[]}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2017-02-02 15:45:34 +00:00
|
|
|
this._elements = [];
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies all registered middleware into a store enhancer.
|
|
|
|
* (@link http://redux.js.org/docs/api/applyMiddleware.html).
|
|
|
|
*
|
2017-02-02 15:45:34 +00:00
|
|
|
* @param {Middleware[]} additional - Any additional middleware that need to
|
2016-10-05 14:36:59 +00:00
|
|
|
* be included (such as middleware from third-party modules).
|
2017-02-02 15:45:34 +00:00
|
|
|
* @returns {Middleware}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2022-07-15 12:33:09 +00:00
|
|
|
applyMiddleware(...additional: Array<Middleware<any, any>>) {
|
2019-03-19 15:42:25 +00:00
|
|
|
return applyMiddleware(...this._elements, ...additional);
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a middleware to the registry.
|
|
|
|
*
|
|
|
|
* The method is to be invoked only before {@link #applyMiddleware()}.
|
|
|
|
*
|
2017-02-02 15:45:34 +00:00
|
|
|
* @param {Middleware} middleware - A Redux middleware.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
register(middleware: Middleware<any, IReduxState, IStore['dispatch']>) {
|
2017-02-02 15:45:34 +00:00
|
|
|
this._elements.push(middleware);
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The public singleton instance of the MiddlewareRegistry class.
|
|
|
|
*/
|
|
|
|
export default new MiddlewareRegistry();
|