Преглед на файлове

fix: synchronize global 'config' var with the reducer's state

j8
paweldomas преди 7 години
родител
ревизия
b5b99301ca
променени са 2 файла, в които са добавени 53 реда и са изтрити 0 реда
  1. 1
    0
      react/features/base/config/index.js
  2. 52
    0
      react/features/base/config/middleware.js

+ 1
- 0
react/features/base/config/index.js Целия файл

@@ -2,4 +2,5 @@ export * from './actions';
2 2
 export * from './actionTypes';
3 3
 export * from './functions';
4 4
 
5
+import './middleware';
5 6
 import './reducer';

+ 52
- 0
react/features/base/config/middleware.js Целия файл

@@ -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
+}

Loading…
Отказ
Запис