Coding style
This commit is contained in:
parent
034518a6a0
commit
86e4876df2
|
@ -186,49 +186,53 @@ function _loadConfig({ contextRoot, host, protocol, room }) {
|
||||||
|
|
||||||
// TDOO userinfo
|
// TDOO userinfo
|
||||||
|
|
||||||
const rootUrl = `${protocol}//${host}${contextRoot || '/'}`;
|
const baseURL = `${protocol}//${host}${contextRoot || '/'}`;
|
||||||
let url = `${rootUrl}config.js`;
|
let url = `${baseURL}config.js`;
|
||||||
|
|
||||||
// XXX In order to support multiple shards, tell the room to the deployment.
|
// XXX In order to support multiple shards, tell the room to the deployment.
|
||||||
room && (url += `?room=${room.toLowerCase()}`);
|
room && (url += `?room=${room.toLowerCase()}`);
|
||||||
|
|
||||||
/* eslint-enable no-param-reassign */
|
/* eslint-enable no-param-reassign */
|
||||||
|
|
||||||
const key = `config/${rootUrl}`;
|
const key = `config.js/${baseURL}`;
|
||||||
|
|
||||||
return (
|
return loadConfig(url).then(
|
||||||
loadConfig(url)
|
/* onFulfilled */ config => {
|
||||||
.then(config => {
|
// Try to store the configuration in localStorage. If the deployment
|
||||||
// Try to store the configuration in localStorage. If the
|
// specified 'getroom' as a function, for example, it does not make
|
||||||
// deployment specified the 'getroom' option as a function, for
|
// sense to and it will not be stored.
|
||||||
// example, we cannot store it, so don't.
|
try {
|
||||||
try {
|
if (typeof window.config === 'undefined'
|
||||||
|
|| window.config !== config) {
|
||||||
window.localStorage.setItem(key, JSON.stringify(config));
|
window.localStorage.setItem(key, JSON.stringify(config));
|
||||||
} catch (e) {
|
|
||||||
|
|
||||||
// Ignore the error, we won't cache this config.
|
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore the error because the caching is optional.
|
||||||
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
})
|
},
|
||||||
.catch(error => {
|
/* onRejected */ error => {
|
||||||
// We failed to load the requested config, try to use the last
|
// XXX The (down)loading of config failed. Try to use the last
|
||||||
// one which was fetched for that deployment. It may not match
|
// successfully fetched for that deployment. It may not match the
|
||||||
// the shard, but it's probably better than nothing.
|
// shard.
|
||||||
const config = window.localStorage.getItem(key);
|
let storage;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// XXX Even reading the property localStorage of window may
|
||||||
|
// throw an error (which is user agent-specific behavior).
|
||||||
|
storage = window.localStorage;
|
||||||
|
|
||||||
|
const config = storage.getItem(key);
|
||||||
|
|
||||||
if (config) {
|
if (config) {
|
||||||
try {
|
return JSON.parse(config);
|
||||||
return JSON.parse(config);
|
|
||||||
} catch (e) {
|
|
||||||
|
|
||||||
// Somehow incorrect data ended up in the storage. Clean
|
|
||||||
// up.
|
|
||||||
window.localStorage.removeItem(key);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Somehow incorrect data ended up in the storage. Clean it up.
|
||||||
|
storage && storage.removeItem(key);
|
||||||
|
}
|
||||||
|
|
||||||
throw error;
|
throw error;
|
||||||
})
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue