1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

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 <toger5@hotmail.de>

* add timeout to leave

Signed-off-by: Timo K <toger5@hotmail.de>

* formatting

Signed-off-by: Timo K <toger5@hotmail.de>

---------

Signed-off-by: Timo K <toger5@hotmail.de>
This commit is contained in:
Timo
2023-10-09 13:52:19 +02:00
committed by GitHub
parent 513201b9c1
commit a5224c1820

View File

@@ -154,8 +154,8 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
/**
* Performs cleanup & removes timers for client shutdown
*/
public stop(): void {
this.leaveRoomSession();
public async stop(): Promise<void> {
await this.leaveRoomSession(1000);
if (this.expiryTimeout) {
clearTimeout(this.expiryTimeout);
this.expiryTimeout = undefined;
@@ -195,11 +195,14 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
* and stops scheduled updates.
* This will not unsubscribe from updates: remember to call unsubscribe() separately if
* desired.
* The membership update required to leave the session will retry if it fails.
* Without network connection the promise will never resolve.
* A timeout can be provided so that there is a guarantee for the promise to resolve.
*/
public leaveRoomSession(): void {
public async leaveRoomSession(timeout: number | undefined = undefined): Promise<boolean> {
if (!this.isJoined()) {
logger.info(`Not joined to session in room ${this.room.roomId}: ignoring leave call`);
return;
return new Promise((resolve) => resolve(false));
}
logger.info(`Leaving call session in room ${this.room.roomId}`);
@@ -207,7 +210,20 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
this.activeFoci = undefined;
this.membershipId = undefined;
this.emit(MatrixRTCSessionEvent.JoinStateChanged, false);
this.triggerCallMembershipEventUpdate();
const timeoutPromise = new Promise((r) => {
if (timeout) {
// will never resolve if timeout is not set
setTimeout(r, timeout, "timeout");
}
});
return new Promise((resolve) => {
Promise.race([this.triggerCallMembershipEventUpdate(), timeoutPromise]).then((value) => {
// The timeoutPromise returns the string 'timeout' and the membership update void
// A success implies that the membership update was quicker then the timeout.
resolve(value != "timeout");
});
});
}
/**
@@ -411,7 +427,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
memberships: this.makeNewMemberships(memberships, myCallMemberEvent, myPrevMembership),
};
let resendDelay;
let resendDelay = 0;
try {
await this.client.sendStateEvent(
this.room.roomId,
@@ -428,6 +444,9 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
logger.warn(`Failed to send call member event: retrying in ${resendDelay}`);
}
if (resendDelay) this.memberEventTimeout = setTimeout(this.triggerCallMembershipEventUpdate, resendDelay);
if (resendDelay) {
await new Promise((resolve) => setTimeout(resolve, resendDelay));
await this.triggerCallMembershipEventUpdate();
}
}
}