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) {
let enableGuest = opts.enableGuest || false; try {
const guestHsUrl = opts.guestHsUrl; let enableGuest = opts.enableGuest || false;
const guestIsUrl = opts.guestIsUrl; const guestHsUrl = opts.guestHsUrl;
const fragmentQueryParams = opts.fragmentQueryParams || {}; const guestIsUrl = opts.guestIsUrl;
const defaultDeviceDisplayName = opts.defaultDeviceDisplayName; const fragmentQueryParams = opts.fragmentQueryParams || {};
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,40 +198,39 @@ 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 false;
return Promise.resolve(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'; const accessToken = localStorage.getItem("mx_access_token");
const accessToken = localStorage.getItem("mx_access_token"); const userId = localStorage.getItem("mx_user_id");
const userId = localStorage.getItem("mx_user_id"); const deviceId = localStorage.getItem("mx_device_id");
const deviceId = localStorage.getItem("mx_device_id");
let isGuest; let isGuest;
if (localStorage.getItem("mx_is_guest") !== null) { if (localStorage.getItem("mx_is_guest") !== null) {
isGuest = localStorage.getItem("mx_is_guest") === "true"; isGuest = localStorage.getItem("mx_is_guest") === "true";
} else { } else {
// legacy key name // legacy key name
isGuest = localStorage.getItem("matrix-is-guest") === "true"; isGuest = localStorage.getItem("matrix-is-guest") === "true";
} }
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);
} else { return true;
console.log("No previous session found."); } else {
return Promise.resolve(false); console.log("No previous session found.");
} return false;
}); }
} }
function _handleLoadSessionFailure(e) { function _handleLoadSessionFailure(e) {