Skip to content

Commit

Permalink
Fix waitForParticipant (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcherry authored Sep 30, 2024
1 parent a18a45a commit 019709c
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions agents/src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,34 @@ export class JobContext {
throw new Error('room is not connected');
}

console.log(this.#room.remoteParticipants.values());

for (const p of this.#room.remoteParticipants.values()) {
if ((!identity || p.identity === identity) && p.info.kind != ParticipantKind.AGENT) {
return p;
}
}

return new Promise((resolve) => {
this.#room.once(RoomEvent.ParticipantConnected, () => {
resolve(this.#room.remoteParticipants.values().next().value);
});
return new Promise((resolve, reject) => {
const onParticipantConnected = (participant: RemoteParticipant) => {
if (
(!identity || participant.identity === identity) &&
participant.info.kind != ParticipantKind.AGENT
) {
clearHandlers();
resolve(participant);
}
};
const onDisconnected = () => {
clearHandlers();
reject(new Error('Room disconnected while waiting for participant'));
};

const clearHandlers = () => {
this.#room.off(RoomEvent.ParticipantConnected, onParticipantConnected);
this.#room.off(RoomEvent.Disconnected, onDisconnected);
};

this.#room.on(RoomEvent.ParticipantConnected, onParticipantConnected);
this.#room.on(RoomEvent.Disconnected, onDisconnected);
});
}

Expand Down

0 comments on commit 019709c

Please sign in to comment.