Skip to content

Commit a5224c1

Browse files
authored
make leaveRoomSession async (#3756)
* make leaveRoomSession async. This does not resolve the promise until the event is actually send. No network connection would make awaiting on this blocking. Signed-off-by: Timo K <[email protected]> * add timeout to leave Signed-off-by: Timo K <[email protected]> * formatting Signed-off-by: Timo K <[email protected]> --------- Signed-off-by: Timo K <[email protected]>
1 parent 513201b commit a5224c1

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/matrixrtc/MatrixRTCSession.ts

+26-7
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
154154
/**
155155
* Performs cleanup & removes timers for client shutdown
156156
*/
157-
public stop(): void {
158-
this.leaveRoomSession();
157+
public async stop(): Promise<void> {
158+
await this.leaveRoomSession(1000);
159159
if (this.expiryTimeout) {
160160
clearTimeout(this.expiryTimeout);
161161
this.expiryTimeout = undefined;
@@ -195,19 +195,35 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
195195
* and stops scheduled updates.
196196
* This will not unsubscribe from updates: remember to call unsubscribe() separately if
197197
* 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.
198201
*/
199-
public leaveRoomSession(): void {
202+
public async leaveRoomSession(timeout: number | undefined = undefined): Promise<boolean> {
200203
if (!this.isJoined()) {
201204
logger.info(`Not joined to session in room ${this.room.roomId}: ignoring leave call`);
202-
return;
205+
return new Promise((resolve) => resolve(false));
203206
}
204207

205208
logger.info(`Leaving call session in room ${this.room.roomId}`);
206209
this.relativeExpiry = undefined;
207210
this.activeFoci = undefined;
208211
this.membershipId = undefined;
209212
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+
});
211227
}
212228

213229
/**
@@ -411,7 +427,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
411427
memberships: this.makeNewMemberships(memberships, myCallMemberEvent, myPrevMembership),
412428
};
413429

414-
let resendDelay;
430+
let resendDelay = 0;
415431
try {
416432
await this.client.sendStateEvent(
417433
this.room.roomId,
@@ -428,6 +444,9 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
428444
logger.warn(`Failed to send call member event: retrying in ${resendDelay}`);
429445
}
430446

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+
}
432451
}
433452
}

0 commit comments

Comments
 (0)