From bb5ae741bca14dd42064790a6930b470ceed655d Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 15 May 2018 17:12:59 +0100 Subject: [PATCH 1/3] Wait for deletion of widgets as well addition We were previously waiting for them to appear which is silly if we were deleting them. --- src/ScalarMessaging.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/ScalarMessaging.js b/src/ScalarMessaging.js index 0c10642cd7..14d363894e 100644 --- a/src/ScalarMessaging.js +++ b/src/ScalarMessaging.js @@ -292,22 +292,30 @@ function inviteUser(event, roomId, userId) { * arrives) or rejects after a timeout * * @param {string} widgetId The ID of the widget to wait for + * @param {boolean} add True to wait for the widget to be added, + * false to wait for it to be deleted. * @returns {Promise} that resolves when the widget is available */ -function waitForUserWidget(widgetId) { +function waitForUserWidget(widgetId, add) { return new Promise((resolve, reject) => { const currentAccountDataEvent = MatrixClientPeg.get().getAccountData('m.widgets'); - if ( - currentAccountDataEvent && - currentAccountDataEvent.getContent() && - currentAccountDataEvent.getContent()[widgetId] !== undefined - ) { + + function satisfiesCondition(ev) { + if (!ev || !currentAccountDataEvent.getContent()) return false; + if (add) { + return ev.getContent()[widgetId] !== undefined; + } else { + return ev.getContent()[widgetId] === undefined; + } + } + + if (satisfiesCondition(currentAccountDataEvent)) { resolve(); return; } function onAccountData(ev) { - if (ev.getType() === 'm.widgets' && ev.getContent() && ev.getContent()[widgetId] !== undefined) { + if (satisfiesCondition(currentAccountDataEvent)) { MatrixClientPeg.get().removeListener('accountData', onAccountData); clearTimeout(timerId); resolve(); @@ -395,7 +403,7 @@ function setWidget(event, roomId) { // wait for this, the action will complete but if the user is fast enough, // the widget still won't actually be there. client.setAccountData('m.widgets', userWidgets).then(() => { - return waitForUserWidget(widgetId); + return waitForUserWidget(widgetId, widgetUrl !== null); }).then(() => { sendResponse(event, { success: true, From e4a221e42ded1641fdc1ef4f067fc17b3555f25a Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 15 May 2018 17:28:55 +0100 Subject: [PATCH 2/3] More helpful function name --- src/ScalarMessaging.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ScalarMessaging.js b/src/ScalarMessaging.js index 14d363894e..01ee140eb3 100644 --- a/src/ScalarMessaging.js +++ b/src/ScalarMessaging.js @@ -300,7 +300,9 @@ function waitForUserWidget(widgetId, add) { return new Promise((resolve, reject) => { const currentAccountDataEvent = MatrixClientPeg.get().getAccountData('m.widgets'); - function satisfiesCondition(ev) { + // Tests an account data event, returning true if it's in the state + // we're waiting for it to be in + function eventInIntendedState(ev) { if (!ev || !currentAccountDataEvent.getContent()) return false; if (add) { return ev.getContent()[widgetId] !== undefined; From fadf264a1bb763c4ce65be873a8e9d0b204e07b5 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 15 May 2018 17:34:02 +0100 Subject: [PATCH 3/3] Rename uses of function too --- src/ScalarMessaging.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ScalarMessaging.js b/src/ScalarMessaging.js index 01ee140eb3..9457e6ccfb 100644 --- a/src/ScalarMessaging.js +++ b/src/ScalarMessaging.js @@ -311,13 +311,13 @@ function waitForUserWidget(widgetId, add) { } } - if (satisfiesCondition(currentAccountDataEvent)) { + if (eventInIntendedState(currentAccountDataEvent)) { resolve(); return; } function onAccountData(ev) { - if (satisfiesCondition(currentAccountDataEvent)) { + if (eventInIntendedState(currentAccountDataEvent)) { MatrixClientPeg.get().removeListener('accountData', onAccountData); clearTimeout(timerId); resolve();