jiti-meet/react/features/base/redux/MiddlewareRegistry.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-02-02 15:45:34 +00:00
/* @flow */
import { applyMiddleware } from 'redux';
2017-02-03 16:17:20 +00:00
import type { Middleware } from 'redux';
2017-02-02 15:45:34 +00:00
/**
* A registry for Redux middleware, allowing features to register their
* middleware without needing to create additional inter-feature dependencies.
*/
class MiddlewareRegistry {
2017-02-03 16:17:20 +00:00
_elements: Array<Middleware<*, *>>;
2017-02-02 15:45:34 +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[]}
*/
2017-02-02 15:45:34 +00:00
this._elements = [];
}
/**
* 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
* be included (such as middleware from third-party modules).
2017-02-02 15:45:34 +00:00
* @returns {Middleware}
*/
2017-02-03 16:17:20 +00:00
applyMiddleware(...additional: Array<Middleware<*, *>>) {
// XXX The explicit definition of the local variable middlewares is to
// satisfy flow.
const middlewares = [
...this._elements,
...additional
2017-02-03 16:17:20 +00:00
];
return applyMiddleware(...middlewares);
}
/**
* 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.
* @returns {void}
*/
2017-02-03 16:17:20 +00:00
register(middleware: Middleware<*, *>) {
2017-02-02 15:45:34 +00:00
this._elements.push(middleware);
}
}
/**
* The public singleton instance of the MiddlewareRegistry class.
*/
export default new MiddlewareRegistry();