PR feedback

pull/21833/head
David Baker 2018-05-15 12:06:23 +01:00
parent 142ce4c256
commit f4d69e26e9
1 changed files with 8 additions and 6 deletions

View File

@ -293,18 +293,22 @@ function inviteUser(event, roomId, userId) {
*/ */
function waitForUserWidget(widgetId) { function waitForUserWidget(widgetId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (ev.getContent() && ev.getContent()[widgetId] !== undefined) {
resolve();
return;
}
let timerId; let timerId;
function onAccountData(ev) { function onAccountData(ev) {
if (ev.getContent()[widgetId] !== undefined) { if (ev.getContent() && ev.getContent()[widgetId] !== undefined) {
MatrixClientPeg.get().removeListener('accountData', onAccountData); MatrixClientPeg.get().removeListener('accountData', onAccountData);
clearTimeout(timerId); clearTimeout(timerId);
resolve(); resolve();
} }
} }
timerId = setTimeout(() => { timerId = setTimeout(() => {
console.log("Timed out waiting for widget ID " + widgetId + " to appear");
MatrixClientPeg.get().removeListener('accountData', onAccountData); MatrixClientPeg.get().removeListener('accountData', onAccountData);
reject(); reject(new Error("Timed out waiting for widget ID " + widgetId + " to appear"));
}, 10000); }, 10000);
MatrixClientPeg.get().on('accountData', onAccountData); MatrixClientPeg.get().on('accountData', onAccountData);
}); });
@ -383,10 +387,8 @@ function setWidget(event, roomId) {
// since the widget won't appear added until this happens. If we don't // since the widget won't appear added until this happens. If we don't
// wait for this, the action will complete but if the user is fast enough, // wait for this, the action will complete but if the user is fast enough,
// the widget still won't actually be there. // the widget still won't actually be there.
// start listening now otherwise we could race
const widgetAddPromise = waitForUserWidget(widgetId);
client.setAccountData('m.widgets', userWidgets).then(() => { client.setAccountData('m.widgets', userWidgets).then(() => {
return widgetAddPromise; return waitForUserWidget(widgetId);
}).then(() => { }).then(() => {
sendResponse(event, { sendResponse(event, {
success: true, success: true,