瀏覽代碼

Fix persistency to handle default values too (#3228)

master
Zoltan Bettenbuk 7 年之前
父節點
當前提交
63c017f8e6
共有 1 個檔案被更改,包括 41 行新增5 行删除
  1. 41
    5
      react/features/base/storage/PersistenceRegistry.js

+ 41
- 5
react/features/base/storage/PersistenceRegistry.js 查看文件

27
  */
27
  */
28
 class PersistenceRegistry {
28
 class PersistenceRegistry {
29
     _checksum: string;
29
     _checksum: string;
30
-
30
+    _defaultStates: { [name: string ]: ?Object} = {};
31
     _elements: PersistencyConfigMap = {};
31
     _elements: PersistencyConfigMap = {};
32
 
32
 
33
     /**
33
     /**
51
             const persistedSubtree
51
             const persistedSubtree
52
                 = this._getPersistedSubtree(
52
                 = this._getPersistedSubtree(
53
                     subtreeName,
53
                     subtreeName,
54
-                    this._elements[subtreeName]);
54
+                    this._elements[subtreeName],
55
+                    this._defaultStates[subtreeName]);
55
 
56
 
56
             if (persistedSubtree !== undefined) {
57
             if (persistedSubtree !== undefined) {
57
                 filteredPersistedState[subtreeName] = persistedSubtree;
58
                 filteredPersistedState[subtreeName] = persistedSubtree;
128
      * @param {string} name - The name of the subtree the config belongs to.
129
      * @param {string} name - The name of the subtree the config belongs to.
129
      * @param {ElementConfig} config - The config {@code Object}, or
130
      * @param {ElementConfig} config - The config {@code Object}, or
130
      * {@code boolean} if the entire subtree needs to be persisted.
131
      * {@code boolean} if the entire subtree needs to be persisted.
132
+     * @param {Object} defaultState - The default state of the component. If
133
+     * it's provided, the rehydrated state will be merged with it before it gets
134
+     * pushed into Redux.
131
      * @returns {void}
135
      * @returns {void}
132
      */
136
      */
133
-    register(name: string, config?: ElementConfig = true) {
137
+    register(
138
+            name: string,
139
+            config?: ElementConfig = true,
140
+            defaultState?: Object) {
134
         this._elements[name] = config;
141
         this._elements[name] = config;
142
+        this._defaultStates[name] = defaultState;
135
     }
143
     }
136
 
144
 
137
     /**
145
     /**
209
      * @param {string} subtreeName - The name of the subtree.
217
      * @param {string} subtreeName - The name of the subtree.
210
      * @param {Object} subtreeConfig - The config of the subtree from
218
      * @param {Object} subtreeConfig - The config of the subtree from
211
      * {@link #_elements}.
219
      * {@link #_elements}.
220
+     * @param {Object} subtreeDefaults - The defaults of the persisted subtree.
212
      * @private
221
      * @private
213
      * @returns {Object}
222
      * @returns {Object}
214
      */
223
      */
215
-    _getPersistedSubtree(subtreeName, subtreeConfig) {
224
+    _getPersistedSubtree(subtreeName, subtreeConfig, subtreeDefaults) {
216
         let persistedSubtree = window.localStorage.getItem(subtreeName);
225
         let persistedSubtree = window.localStorage.getItem(subtreeName);
217
 
226
 
218
         if (persistedSubtree) {
227
         if (persistedSubtree) {
223
                     = this._getFilteredSubtree(persistedSubtree, subtreeConfig);
232
                     = this._getFilteredSubtree(persistedSubtree, subtreeConfig);
224
 
233
 
225
                 if (filteredSubtree !== undefined) {
234
                 if (filteredSubtree !== undefined) {
226
-                    return filteredSubtree;
235
+                    return this._mergeDefaults(
236
+                        filteredSubtree, subtreeDefaults);
227
                 }
237
                 }
228
             } catch (error) {
238
             } catch (error) {
229
                 logger.error(
239
                 logger.error(
236
 
246
 
237
         return undefined;
247
         return undefined;
238
     }
248
     }
249
+
250
+    /**
251
+     * Merges the persisted subtree with its defaults before rehydrating the
252
+     * values.
253
+     *
254
+     * @private
255
+     * @param {Object} subtree - The Redux subtree.
256
+     * @param {?Object} defaults - The defaults, if any.
257
+     * @returns {Object}
258
+     */
259
+    _mergeDefaults(subtree: Object, defaults: ?Object) {
260
+        if (!defaults) {
261
+            return subtree;
262
+        }
263
+
264
+        // If the subtree is an array, we don't need to merge it with the
265
+        // defaults, because if it has a value, it will overwrite it, and if
266
+        // it's undefined, it won't be even returned, and Redux will natively
267
+        // use the default values instead.
268
+        if (!Array.isArray(subtree)) {
269
+            return {
270
+                ...defaults,
271
+                ...subtree
272
+            };
273
+        }
274
+    }
239
 }
275
 }
240
 
276
 
241
 export default new PersistenceRegistry();
277
 export default new PersistenceRegistry();

Loading…
取消
儲存