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

Commit 9dd90fb

Browse files
authored
Merge pull request #1907 from matrix-org/dbkr/wait_for_widget_delete
Wait for deletion of widgets as well addition
2 parents db092c8 + fadf264 commit 9dd90fb

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/ScalarMessaging.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,32 @@ 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+
// Tests an account data event, returning true if it's in the state
304+
// we're waiting for it to be in
305+
function eventInIntendedState(ev) {
306+
if (!ev || !currentAccountDataEvent.getContent()) return false;
307+
if (add) {
308+
return ev.getContent()[widgetId] !== undefined;
309+
} else {
310+
return ev.getContent()[widgetId] === undefined;
311+
}
312+
}
313+
314+
if (eventInIntendedState(currentAccountDataEvent)) {
305315
resolve();
306316
return;
307317
}
308318

309319
function onAccountData(ev) {
310-
if (ev.getType() === 'm.widgets' && ev.getContent() && ev.getContent()[widgetId] !== undefined) {
320+
if (eventInIntendedState(currentAccountDataEvent)) {
311321
MatrixClientPeg.get().removeListener('accountData', onAccountData);
312322
clearTimeout(timerId);
313323
resolve();
@@ -395,7 +405,7 @@ function setWidget(event, roomId) {
395405
// wait for this, the action will complete but if the user is fast enough,
396406
// the widget still won't actually be there.
397407
client.setAccountData('m.widgets', userWidgets).then(() => {
398-
return waitForUserWidget(widgetId);
408+
return waitForUserWidget(widgetId, widgetUrl !== null);
399409
}).then(() => {
400410
sendResponse(event, {
401411
success: true,

0 commit comments

Comments
 (0)