Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client.waitUntilRoomReadyForGroupCalls() #2641

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,6 @@ type EmittedEvents = ClientEvent
| GroupCallEventHandlerEvent.Incoming
| GroupCallEventHandlerEvent.Ended
| GroupCallEventHandlerEvent.Participants
| GroupCallEventHandlerEvent.Room
| HttpApiEvent.SessionLoggedOut
| HttpApiEvent.NoConsent
| BeaconEvent;
Expand Down Expand Up @@ -1594,6 +1593,21 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
).create();
}

/**
* Wait until an initial state for the given room has been processed by the
* client and the client is aware of any ongoing group calls. Awaiting on
* the promise returned by this method before calling getGroupCallForRoom()
* avoids races where getGroupCallForRoom is called before the state for that
* room has been processed. It does not, however, fix other races, eg. two
* clients both creating a group call at the same time.
* @param roomId The room ID to wait for
* @returns A promise that resolves once existing group calls in the room
* have been processed.
*/
public waitUntilRoomReadyForGroupCalls(roomId: string): Promise<void> {
return this.groupCallEventHandler.waitUntilRoomReadyForGroupCalls(roomId);
}

/**
* Get an existing group call for the provided room.
* @param roomId
Expand Down
37 changes: 34 additions & 3 deletions src/webrtc/groupCallEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,28 @@ export enum GroupCallEventHandlerEvent {
Incoming = "GroupCall.incoming",
Ended = "GroupCall.ended",
Participants = "GroupCall.participants",
Room = "GroupCall.Room",
}

export type GroupCallEventHandlerEventHandlerMap = {
[GroupCallEventHandlerEvent.Incoming]: (call: GroupCall) => void;
[GroupCallEventHandlerEvent.Ended]: (call: GroupCall) => void;
[GroupCallEventHandlerEvent.Participants]: (participants: RoomMember[], call: GroupCall) => void;
[GroupCallEventHandlerEvent.Room]: (room: Room) => void;
};

interface RoomDeferred {
prom: Promise<void>;
resolve?: () => void;
}

export class GroupCallEventHandler {
public groupCalls = new Map<string, GroupCall>(); // roomId -> GroupCall

// All rooms we know about and whether we've seen a 'Room' event
// for them. The promise will be fulfilled once we've processed that
// event which means we're "up to date" on what calls are in a room
// and get
private roomDeferreds = new Map<string, RoomDeferred>();

constructor(private client: MatrixClient) { }

public async start(): Promise<void> {
Expand Down Expand Up @@ -82,6 +91,26 @@ export class GroupCallEventHandler {
this.client.removeListener(RoomStateEvent.Events, this.onRoomStateChanged);
}

private getRoomDeferred(roomId: string): RoomDeferred {
let deferred: RoomDeferred = this.roomDeferreds.get(roomId);
if (deferred === undefined) {
let resolveFunc: () => void;
deferred = {
prom: new Promise<void>(resolve => {
resolveFunc = resolve;
}),
};
deferred.resolve = resolveFunc;
this.roomDeferreds.set(roomId, deferred);
}

return deferred;
}

public waitUntilRoomReadyForGroupCalls(roomId: string): Promise<void> {
return this.getRoomDeferred(roomId).prom;
}

public getGroupCallById(groupCallId: string): GroupCall {
return [...this.groupCalls.values()].find((groupCall) => groupCall.groupCallId === groupCallId);
}
Expand All @@ -98,9 +127,11 @@ export class GroupCallEventHandler {
}

this.createGroupCallFromRoomStateEvent(callEvent);
this.client.emit(GroupCallEventHandlerEvent.Room, room);
break;
}

logger.info("Group call event handler processed room", room);
this.getRoomDeferred(room.roomId).resolve();
}

private createGroupCallFromRoomStateEvent(event: MatrixEvent): GroupCall | undefined {
Expand Down