2016-10-05 14:36:59 +00:00
|
|
|
import { applyMiddleware } from 'redux';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A registry for Redux middleware, allowing features to register their
|
|
|
|
* middleware without needing to create additional inter-feature dependencies.
|
|
|
|
*/
|
|
|
|
class MiddlewareRegistry {
|
|
|
|
/**
|
|
|
|
* Creates a MiddlewareRegistry instance.
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
/**
|
|
|
|
* The set of registered middleware.
|
|
|
|
*/
|
2017-01-28 23:28:13 +00:00
|
|
|
this._elements = new Set();
|
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).
|
|
|
|
*
|
|
|
|
* @param {Function[]} additional - Any additional middleware that need to
|
|
|
|
* be included (such as middleware from third-party modules).
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
applyMiddleware(...additional) {
|
|
|
|
return applyMiddleware(
|
2017-01-28 23:28:13 +00:00
|
|
|
...this._elements,
|
2016-10-05 14:36:59 +00:00
|
|
|
...additional
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a middleware to the registry.
|
|
|
|
*
|
|
|
|
* The method is to be invoked only before {@link #applyMiddleware()}.
|
|
|
|
*
|
|
|
|
* @param {Function} middleware - A Redux middleware.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
register(middleware) {
|
2017-01-28 23:28:13 +00:00
|
|
|
this._elements.add(middleware);
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The public singleton instance of the MiddlewareRegistry class.
|
|
|
|
*/
|
|
|
|
export default new MiddlewareRegistry();
|