@@ -154,8 +154,8 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
154
154
/**
155
155
* Performs cleanup & removes timers for client shutdown
156
156
*/
157
- public stop ( ) : void {
158
- this . leaveRoomSession ( ) ;
157
+ public async stop ( ) : Promise < void > {
158
+ await this . leaveRoomSession ( 1000 ) ;
159
159
if ( this . expiryTimeout ) {
160
160
clearTimeout ( this . expiryTimeout ) ;
161
161
this . expiryTimeout = undefined ;
@@ -195,19 +195,35 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
195
195
* and stops scheduled updates.
196
196
* This will not unsubscribe from updates: remember to call unsubscribe() separately if
197
197
* desired.
198
+ * The membership update required to leave the session will retry if it fails.
199
+ * Without network connection the promise will never resolve.
200
+ * A timeout can be provided so that there is a guarantee for the promise to resolve.
198
201
*/
199
- public leaveRoomSession ( ) : void {
202
+ public async leaveRoomSession ( timeout : number | undefined = undefined ) : Promise < boolean > {
200
203
if ( ! this . isJoined ( ) ) {
201
204
logger . info ( `Not joined to session in room ${ this . room . roomId } : ignoring leave call` ) ;
202
- return ;
205
+ return new Promise ( ( resolve ) => resolve ( false ) ) ;
203
206
}
204
207
205
208
logger . info ( `Leaving call session in room ${ this . room . roomId } ` ) ;
206
209
this . relativeExpiry = undefined ;
207
210
this . activeFoci = undefined ;
208
211
this . membershipId = undefined ;
209
212
this . emit ( MatrixRTCSessionEvent . JoinStateChanged , false ) ;
210
- this . triggerCallMembershipEventUpdate ( ) ;
213
+
214
+ const timeoutPromise = new Promise ( ( r ) => {
215
+ if ( timeout ) {
216
+ // will never resolve if timeout is not set
217
+ setTimeout ( r , timeout , "timeout" ) ;
218
+ }
219
+ } ) ;
220
+ return new Promise ( ( resolve ) => {
221
+ Promise . race ( [ this . triggerCallMembershipEventUpdate ( ) , timeoutPromise ] ) . then ( ( value ) => {
222
+ // The timeoutPromise returns the string 'timeout' and the membership update void
223
+ // A success implies that the membership update was quicker then the timeout.
224
+ resolve ( value != "timeout" ) ;
225
+ } ) ;
226
+ } ) ;
211
227
}
212
228
213
229
/**
@@ -411,7 +427,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
411
427
memberships : this . makeNewMemberships ( memberships , myCallMemberEvent , myPrevMembership ) ,
412
428
} ;
413
429
414
- let resendDelay ;
430
+ let resendDelay = 0 ;
415
431
try {
416
432
await this . client . sendStateEvent (
417
433
this . room . roomId ,
@@ -428,6 +444,9 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
428
444
logger . warn ( `Failed to send call member event: retrying in ${ resendDelay } ` ) ;
429
445
}
430
446
431
- if ( resendDelay ) this . memberEventTimeout = setTimeout ( this . triggerCallMembershipEventUpdate , resendDelay ) ;
447
+ if ( resendDelay ) {
448
+ await new Promise ( ( resolve ) => setTimeout ( resolve , resendDelay ) ) ;
449
+ await this . triggerCallMembershipEventUpdate ( ) ;
450
+ }
432
451
}
433
452
}
0 commit comments