@@ -292,22 +292,30 @@ 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
+ 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 ) ) {
305
313
resolve ( ) ;
306
314
return ;
307
315
}
308
316
309
317
function onAccountData ( ev ) {
310
- if ( ev . getType ( ) === 'm.widgets' && ev . getContent ( ) && ev . getContent ( ) [ widgetId ] !== undefined ) {
318
+ if ( satisfiesCondition ( currentAccountDataEvent ) ) {
311
319
MatrixClientPeg . get ( ) . removeListener ( 'accountData' , onAccountData ) ;
312
320
clearTimeout ( timerId ) ;
313
321
resolve ( ) ;
@@ -395,7 +403,7 @@ function setWidget(event, roomId) {
395
403
// wait for this, the action will complete but if the user is fast enough,
396
404
// the widget still won't actually be there.
397
405
client . setAccountData ( 'm.widgets' , userWidgets ) . then ( ( ) => {
398
- return waitForUserWidget ( widgetId ) ;
406
+ return waitForUserWidget ( widgetId , widgetUrl !== null ) ;
399
407
} ) . then ( ( ) => {
400
408
sendResponse ( event , {
401
409
success : true ,
0 commit comments