Browse Source

[RN] Cache configurations in localStorage

This only helps iff there is a short transient network error which prevents the
configuration from being loaded. In such case, use the cached version in
localStorage, which may not match the shard, but it's (probably!) better than
nothing.

In case there is no Internet connectivity, an error will be produced as soon as
the XMPP connection is attempted anyway.
master
Saúl Ibarra Corretgé 8 years ago
parent
commit
034518a6a0
1 changed files with 39 additions and 2 deletions
  1. 39
    2
      react/features/app/actions.js

+ 39
- 2
react/features/app/actions.js View File

@@ -186,12 +186,49 @@ function _loadConfig({ contextRoot, host, protocol, room }) {
186 186
 
187 187
     // TDOO userinfo
188 188
 
189
-    let url = `${protocol}//${host}${contextRoot || '/'}config.js`;
189
+    const rootUrl = `${protocol}//${host}${contextRoot || '/'}`;
190
+    let url = `${rootUrl}config.js`;
190 191
 
191 192
     // XXX In order to support multiple shards, tell the room to the deployment.
192 193
     room && (url += `?room=${room.toLowerCase()}`);
193 194
 
194 195
     /* eslint-enable no-param-reassign */
195 196
 
196
-    return loadConfig(url);
197
+    const key = `config/${rootUrl}`;
198
+
199
+    return (
200
+        loadConfig(url)
201
+            .then(config => {
202
+                // Try to store the configuration in localStorage. If the
203
+                // deployment specified the 'getroom' option as a function, for
204
+                // example, we cannot store it, so don't.
205
+                try {
206
+                    window.localStorage.setItem(key, JSON.stringify(config));
207
+                } catch (e) {
208
+
209
+                    // Ignore the error, we won't cache this config.
210
+                }
211
+
212
+                return config;
213
+            })
214
+            .catch(error => {
215
+                // We failed to load the requested config, try to use the last
216
+                // one which was fetched for that deployment. It may not match
217
+                // the shard, but it's probably better than nothing.
218
+                const config = window.localStorage.getItem(key);
219
+
220
+                if (config) {
221
+                    try {
222
+                        return JSON.parse(config);
223
+                    } catch (e) {
224
+
225
+                        // Somehow incorrect data ended up in the storage. Clean
226
+                        // up.
227
+                        window.localStorage.removeItem(key);
228
+                    }
229
+                }
230
+
231
+                throw error;
232
+            })
233
+    );
197 234
 }

Loading…
Cancel
Save