|
@@ -0,0 +1,52 @@
|
|
1
|
+// @flow
|
|
2
|
+
|
|
3
|
+import { MiddlewareRegistry } from '../redux';
|
|
4
|
+
|
|
5
|
+import { SET_CONFIG } from './actionTypes';
|
|
6
|
+
|
|
7
|
+/**
|
|
8
|
+ * The middleware of the feature {@code base/config}.
|
|
9
|
+ *
|
|
10
|
+ * @param {Store} store - The redux store.
|
|
11
|
+ * @private
|
|
12
|
+ * @returns {Function}
|
|
13
|
+ */
|
|
14
|
+MiddlewareRegistry.register(store => next => action => {
|
|
15
|
+ switch (action.type) {
|
|
16
|
+ case SET_CONFIG:
|
|
17
|
+ return _setConfig(store, next, action);
|
|
18
|
+ }
|
|
19
|
+
|
|
20
|
+ return next(action);
|
|
21
|
+});
|
|
22
|
+
|
|
23
|
+/**
|
|
24
|
+ * Notifies the feature {@code base/config} that the {@link SET_CONFIG} redux
|
|
25
|
+ * action is being {@code dispatch}ed in a specific redux store.
|
|
26
|
+ *
|
|
27
|
+ * @param {Store} store - The redux store in which the specified {@code action}
|
|
28
|
+ * is being dispatched.
|
|
29
|
+ * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
|
|
30
|
+ * specified {@code action} in the specified {@code store}.
|
|
31
|
+ * @param {Action} action - The redux action which is being {@code dispatch}ed
|
|
32
|
+ * in the specified {@code store}.
|
|
33
|
+ * @private
|
|
34
|
+ * @returns {*} The return value of {@code next(action)}.
|
|
35
|
+ */
|
|
36
|
+function _setConfig({ getState }, next, action) {
|
|
37
|
+ // The reducer is doing some alterations to the config passed in the action,
|
|
38
|
+ // so make sure it's the final state by waiting for the action to be
|
|
39
|
+ // reduced.
|
|
40
|
+ const result = next(action);
|
|
41
|
+
|
|
42
|
+ // FIXME On Web we rely on the global 'config' variable which gets altered
|
|
43
|
+ // multiple times, before it makes it to the reducer. At some point it may
|
|
44
|
+ // not be the global variable which is being modified anymore due to
|
|
45
|
+ // different merge methods being used along the way. The global variable
|
|
46
|
+ // must be synchronized with the final state resolved by the reducer.
|
|
47
|
+ if (typeof window.config !== 'undefined') {
|
|
48
|
+ window.config = getState()['features/base/config'];
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ return result;
|
|
52
|
+}
|