Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit bb5ae74

Browse files
committed
Wait for deletion of widgets as well addition
We were previously waiting for them to appear which is silly if we were deleting them.
1 parent db092c8 commit bb5ae74

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/ScalarMessaging.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,30 @@ function inviteUser(event, roomId, userId) {
292292
* arrives) or rejects after a timeout
293293
*
294294
* @param {string} widgetId The ID of the widget to wait for
295+
* @param {boolean} add True to wait for the widget to be added,
296+
* false to wait for it to be deleted.
295297
* @returns {Promise} that resolves when the widget is available
296298
*/
297-
function waitForUserWidget(widgetId) {
299+
function waitForUserWidget(widgetId, add) {
298300
return new Promise((resolve, reject) => {
299301
const currentAccountDataEvent = MatrixClientPeg.get().getAccountData('m.widgets');
300-
if (
301-
currentAccountDataEvent &&
302-
currentAccountDataEvent.getContent() &&
303-
currentAccountDataEvent.getContent()[widgetId] !== undefined
304-
) {
302+
303+
function satisfiesCondition(ev) {
304+
if (!ev || !currentAccountDataEvent.getContent()) return false;
305+
if (add) {
306+
return ev.getContent()[widgetId] !== undefined;
307+
} else {
308+
return ev.getContent()[widgetId] === undefined;
309+
}
310+
}
311+
312+
if (satisfiesCondition(currentAccountDataEvent)) {
305313
resolve();
306314
return;
307315
}
308316

309317
function onAccountData(ev) {
310-
if (ev.getType() === 'm.widgets' && ev.getContent() && ev.getContent()[widgetId] !== undefined) {
318+
if (satisfiesCondition(currentAccountDataEvent)) {
311319
MatrixClientPeg.get().removeListener('accountData', onAccountData);
312320
clearTimeout(timerId);
313321
resolve();
@@ -395,7 +403,7 @@ function setWidget(event, roomId) {
395403
// wait for this, the action will complete but if the user is fast enough,
396404
// the widget still won't actually be there.
397405
client.setAccountData('m.widgets', userWidgets).then(() => {
398-
return waitForUserWidget(widgetId);
406+
return waitForUserWidget(widgetId, widgetUrl !== null);
399407
}).then(() => {
400408
sendResponse(event, {
401409
success: true,

0 commit comments

Comments
 (0)