53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
|
// @flow
|
||
|
|
||
|
import { MiddlewareRegistry } from '../redux';
|
||
|
|
||
|
import { SET_CONFIG } from './actionTypes';
|
||
|
|
||
|
/**
|
||
|
* The middleware of the feature {@code base/config}.
|
||
|
*
|
||
|
* @param {Store} store - The redux store.
|
||
|
* @private
|
||
|
* @returns {Function}
|
||
|
*/
|
||
|
MiddlewareRegistry.register(store => next => action => {
|
||
|
switch (action.type) {
|
||
|
case SET_CONFIG:
|
||
|
return _setConfig(store, next, action);
|
||
|
}
|
||
|
|
||
|
return next(action);
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* Notifies the feature {@code base/config} that the {@link SET_CONFIG} redux
|
||
|
* action is being {@code dispatch}ed in a specific redux store.
|
||
|
*
|
||
|
* @param {Store} store - The redux store in which the specified {@code action}
|
||
|
* is being dispatched.
|
||
|
* @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
|
||
|
* specified {@code action} in the specified {@code store}.
|
||
|
* @param {Action} action - The redux action which is being {@code dispatch}ed
|
||
|
* in the specified {@code store}.
|
||
|
* @private
|
||
|
* @returns {*} The return value of {@code next(action)}.
|
||
|
*/
|
||
|
function _setConfig({ getState }, next, action) {
|
||
|
// The reducer is doing some alterations to the config passed in the action,
|
||
|
// so make sure it's the final state by waiting for the action to be
|
||
|
// reduced.
|
||
|
const result = next(action);
|
||
|
|
||
|
// FIXME On Web we rely on the global 'config' variable which gets altered
|
||
|
// multiple times, before it makes it to the reducer. At some point it may
|
||
|
// not be the global variable which is being modified anymore due to
|
||
|
// different merge methods being used along the way. The global variable
|
||
|
// must be synchronized with the final state resolved by the reducer.
|
||
|
if (typeof window.config !== 'undefined') {
|
||
|
window.config = getState()['features/base/config'];
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|