Rewrite to use async / await

This commit is contained in:
David Baker 2018-04-27 17:49:53 +01:00
parent 2cc50d35c6
commit fed74646b0

View file

@ -65,14 +65,14 @@ import sdk from './index';
* Resolves to `true` if we ended up starting a session, or `false` if we * Resolves to `true` if we ended up starting a session, or `false` if we
* failed. * failed.
*/ */
export function loadSession(opts) { export async function loadSession(opts) {
try {
let enableGuest = opts.enableGuest || false; let enableGuest = opts.enableGuest || false;
const guestHsUrl = opts.guestHsUrl; const guestHsUrl = opts.guestHsUrl;
const guestIsUrl = opts.guestIsUrl; const guestIsUrl = opts.guestIsUrl;
const fragmentQueryParams = opts.fragmentQueryParams || {}; const fragmentQueryParams = opts.fragmentQueryParams || {};
const defaultDeviceDisplayName = opts.defaultDeviceDisplayName; const defaultDeviceDisplayName = opts.defaultDeviceDisplayName;
return Promise.resolve().then(() => {
if (!guestHsUrl) { if (!guestHsUrl) {
console.warn("Cannot enable guest access: can't determine HS URL to use"); console.warn("Cannot enable guest access: can't determine HS URL to use");
enableGuest = false; enableGuest = false;
@ -91,8 +91,7 @@ export function loadSession(opts) {
guest: true, guest: true,
}, true).then(() => true); }, true).then(() => true);
} }
return _restoreFromLocalStorage(); const success = await _restoreFromLocalStorage();
}).then((success) => {
if (success) { if (success) {
return true; return true;
} }
@ -103,9 +102,9 @@ export function loadSession(opts) {
// fall back to login screen // fall back to login screen
return false; return false;
}).catch((e) => { } catch (e) {
return _handleLoadSessionFailure(e); return _handleLoadSessionFailure(e);
}); }
} }
/** /**
@ -199,10 +198,9 @@ function _registerAsGuest(hsUrl, isUrl, defaultDeviceDisplayName) {
// The plan is to gradually move the localStorage access done here into // The plan is to gradually move the localStorage access done here into
// SessionStore to avoid bugs where the view becomes out-of-sync with // SessionStore to avoid bugs where the view becomes out-of-sync with
// localStorage (e.g. teamToken, isGuest etc.) // localStorage (e.g. teamToken, isGuest etc.)
function _restoreFromLocalStorage() { async function _restoreFromLocalStorage() {
return Promise.resolve().then(() => {
if (!localStorage) { if (!localStorage) {
return Promise.resolve(false); return false;
} }
const hsUrl = localStorage.getItem("mx_hs_url"); const hsUrl = localStorage.getItem("mx_hs_url");
const isUrl = localStorage.getItem("mx_is_url") || 'https://matrix.org'; const isUrl = localStorage.getItem("mx_is_url") || 'https://matrix.org';
@ -220,19 +218,19 @@ function _restoreFromLocalStorage() {
if (accessToken && userId && hsUrl) { if (accessToken && userId && hsUrl) {
console.log(`Restoring session for ${userId}`); console.log(`Restoring session for ${userId}`);
return _doSetLoggedIn({ await _doSetLoggedIn({
userId: userId, userId: userId,
deviceId: deviceId, deviceId: deviceId,
accessToken: accessToken, accessToken: accessToken,
homeserverUrl: hsUrl, homeserverUrl: hsUrl,
identityServerUrl: isUrl, identityServerUrl: isUrl,
guest: isGuest, guest: isGuest,
}, false).then(() => true); }, false);
return true;
} else { } else {
console.log("No previous session found."); console.log("No previous session found.");
return Promise.resolve(false); return false;
} }
});
} }
function _handleLoadSessionFailure(e) { function _handleLoadSessionFailure(e) {