From 7c59e397102310e5987ef1d0640f46658e2840cd Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 5 Jun 2020 17:52:09 +0100 Subject: [PATCH] Sort out what we wait for after login We were waiting only for the client to become logged in rather than for setLoggedIn() to finish but then we were waiting for the first sync to complete which is far longer. We need setLoggedIn to have finished for crypto to be set up so we can query cross-signing keys, so just wait for that anyway, the logic becomes a lot simpler and we're waiting the same amount of time because we have to wait for the first sync to finish. We can also download keys in parallel. --- src/components/structures/MatrixChat.tsx | 43 +++++++++--------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx index b5b77e3ae6..4d85108aaf 100644 --- a/src/components/structures/MatrixChat.tsx +++ b/src/components/structures/MatrixChat.tsx @@ -1868,42 +1868,31 @@ export default class MatrixChat extends React.PureComponent { this.accountPasswordTimer = null; }, 60 * 5 * 1000); - // Wait for the client to be logged in (but not started) - // which is enough to ask the server about account data. - const loggedIn = new Promise(resolve => { - const actionHandlerRef = dis.register(payload => { - if (payload.action !== "on_logged_in") { - return; - } - dis.unregister(actionHandlerRef); - resolve(); - }); - }); - - // Create and start the client in the background - const setLoggedInPromise = Lifecycle.setLoggedIn(credentials); - await loggedIn; + // Create and start the client + await Lifecycle.setLoggedIn(credentials); const cli = MatrixClientPeg.get(); - // We're checking `isCryptoAvailable` here instead of `isCryptoEnabled` - // because the client hasn't been started yet. - const cryptoAvailable = isCryptoAvailable(); - if (!cryptoAvailable) { + const cryptoEnabled = cli.isCryptoEnabled(); + if (!cryptoEnabled) { this.onLoggedIn(); } - this.setState({ pendingInitialSync: true }); - await this.firstSyncPromise.promise; + const promisesList = [this.firstSyncPromise.promise]; + if (cryptoEnabled) { + // wait for the client to finish downloading cross-signing keys for us so we + // know whether or not we have keys set up on this account + promisesList.push(cli.downloadKeys([cli.getUserId()])); + } - if (!cryptoAvailable) { + this.setState({ pendingInitialSync: true }); + + await Promise.all(promisesList); + + if (!cryptoEnabled) { this.setState({ pendingInitialSync: false }); return setLoggedInPromise; } - // wait for the client to finish downloading cross-signing keys for us so we - // know whether or not we have keys set up on this account - await cli.downloadKeys([cli.getUserId()]); - const crossSigningIsSetUp = cli.getStoredCrossSigningForUser(cli.getUserId()); if (crossSigningIsSetUp) { this.setStateForNewView({ view: Views.COMPLETE_SECURITY }); @@ -1913,8 +1902,6 @@ export default class MatrixChat extends React.PureComponent { this.onLoggedIn(); } this.setState({ pendingInitialSync: false }); - - return setLoggedInPromise; }; // complete security / e2e setup has finished