Skip to content

Commit

Permalink
Documentation + simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Feb 1, 2024
1 parent 9981066 commit a97b3b2
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/Conference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class Conference {
private membersInRooms: Record<string, string[]> = {};

private memberRecalculationPromise = Promise.resolve();
private membershipRecalculationQueue: string[] = [];
private membershipRecalculationQueue = new Set<string>();

constructor(public readonly backend: IScheduleBackend, public readonly id: string, public readonly client: ConferenceMatrixClient, private readonly config: IConfig) {
this.client.on("room.event", async (roomId: string, event) => {
Expand All @@ -103,7 +103,7 @@ export class Conference {
}

// On any member event, recaulculate the membership.
this.enqueueRecalculateRoomMembershop(roomId);
this.enqueueRecalculateRoomMembership(roomId);

if (event['content']?.['third_party_invite']) {
const emailInviteToken = event['content']['third_party_invite']['signed']?.['token'];
Expand Down Expand Up @@ -876,6 +876,15 @@ export class Conference {
return [];
}

/**
* Recalculate the number of joined and left users in a room,
* and then update the total count for the conference.
*
* Prefer to call `enqueueRecalculateRoomMembership` as it will
* queue and debounce calls appropriately.
*
* @param roomId The roomId to recalculate.
*/
private async recalculateRoomMembership(roomId: string) {
try {
const myUserId = await this.client.getUserId();
Expand All @@ -891,8 +900,13 @@ export class Conference {
}
}

private async enqueueRecalculateRoomMembershop(roomId: string) {
if (this.membershipRecalculationQueue.includes(roomId)) {
/**
* Queue up a call to `recalculateRoomMembership`.
* @param roomId The roomId to recalculate.
* @returns A promise that resolves when the call has been made.
*/
private async enqueueRecalculateRoomMembership(roomId: string) {
if (this.membershipRecalculationQueue.has(roomId)) {
return;
}

Expand All @@ -901,15 +915,11 @@ export class Conference {
return;
}

this.membershipRecalculationQueue.unshift(roomId);
this.membershipRecalculationQueue.add(roomId);
// We ensure that recalculations are linear.
return this.memberRecalculationPromise = this.memberRecalculationPromise.then(() => {
// Pop off whatever is first.
const queueRoomId = this.membershipRecalculationQueue.pop();
if (!queueRoomId) {
return;
}
return this.recalculateRoomMembership(queueRoomId);
this.membershipRecalculationQueue.delete(roomId);
return this.recalculateRoomMembership(roomId);
})
}
}

0 comments on commit a97b3b2

Please sign in to comment.