Convert 3PID lookup in address picker to async / await

pull/21833/head
J. Ryan Stinnett 2019-07-24 18:38:09 +01:00
parent 8b6c70f955
commit dd792e10a9
1 changed files with 16 additions and 15 deletions

View File

@ -443,7 +443,7 @@ module.exports = React.createClass({
}); });
if (this._cancelThreepidLookup) this._cancelThreepidLookup(); if (this._cancelThreepidLookup) this._cancelThreepidLookup();
if (addrType === 'email') { if (addrType === 'email') {
this._lookupThreepid(addrType, query).done(); this._lookupThreepid(addrType, query);
} }
} }
this.setState({ this.setState({
@ -498,7 +498,7 @@ module.exports = React.createClass({
return hasError ? null : selectedList; return hasError ? null : selectedList;
}, },
_lookupThreepid: function(medium, address) { _lookupThreepid: async function(medium, address) {
let cancelled = false; let cancelled = false;
// Note that we can't safely remove this after we're done // Note that we can't safely remove this after we're done
// because we don't know that it's the same one, so we just // because we don't know that it's the same one, so we just
@ -509,28 +509,29 @@ module.exports = React.createClass({
}; };
// wait a bit to let the user finish typing // wait a bit to let the user finish typing
return Promise.delay(500).then(() => { await Promise.delay(500);
if (cancelled) return null; if (cancelled) return null;
return MatrixClientPeg.get().lookupThreePid(medium, address);
}).then((res) => { try {
if (res === null || !res.mxid) return null; const lookup = await MatrixClientPeg.get().lookupThreePid(medium, address);
if (cancelled) return null; if (cancelled || lookup === null || !lookup.mxid) return null;
const profile = await MatrixClientPeg.get().getProfileInfo(lookup.mxid);
if (cancelled || profile === null) return null;
return MatrixClientPeg.get().getProfileInfo(res.mxid);
}).then((res) => {
if (res === null) return null;
if (cancelled) return null;
this.setState({ this.setState({
suggestedList: [{ suggestedList: [{
// a UserAddressType // a UserAddressType
addressType: medium, addressType: medium,
address: address, address: address,
displayName: res.displayname, displayName: profile.displayname,
avatarMxc: res.avatar_url, avatarMxc: profile.avatar_url,
isKnown: true, isKnown: true,
}], }],
}); });
}); } catch (e) {
console.error(e);
}
}, },
_getFilteredSuggestions: function() { _getFilteredSuggestions: function() {