@@ -292,22 +292,32 @@ function inviteUser(event, roomId, userId) {
292
292
* arrives) or rejects after a timeout
293
293
*
294
294
* @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.
295
297
* @returns {Promise } that resolves when the widget is available
296
298
*/
297
- function waitForUserWidget ( widgetId ) {
299
+ function waitForUserWidget ( widgetId , add ) {
298
300
return new Promise ( ( resolve , reject ) => {
299
301
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 ) ) {
305
315
resolve ( ) ;
306
316
return ;
307
317
}
308
318
309
319
function onAccountData ( ev ) {
310
- if ( ev . getType ( ) === 'm.widgets' && ev . getContent ( ) && ev . getContent ( ) [ widgetId ] !== undefined ) {
320
+ if ( eventInIntendedState ( currentAccountDataEvent ) ) {
311
321
MatrixClientPeg . get ( ) . removeListener ( 'accountData' , onAccountData ) ;
312
322
clearTimeout ( timerId ) ;
313
323
resolve ( ) ;
@@ -395,7 +405,7 @@ function setWidget(event, roomId) {
395
405
// wait for this, the action will complete but if the user is fast enough,
396
406
// the widget still won't actually be there.
397
407
client . setAccountData ( 'm.widgets' , userWidgets ) . then ( ( ) => {
398
- return waitForUserWidget ( widgetId ) ;
408
+ return waitForUserWidget ( widgetId , widgetUrl !== null ) ;
399
409
} ) . then ( ( ) => {
400
410
sendResponse ( event , {
401
411
success : true ,
0 commit comments