Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Ack Jitsi events when we wait for them
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Townsend <robin@robin.town>
  • Loading branch information
robintown committed Mar 20, 2022
1 parent 0598c27 commit 82aeda4
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions src/stores/VoiceChannelStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,10 @@ export default class VoiceChannelStore extends EventEmitter {
messaging.on(`action:${ElementWidgetActions.UnmuteVideo}`, this.onUnmuteVideo);

// Actually perform the join
const waitForJoin = new Promise<void>(resolve =>
messaging.once(`action:${ElementWidgetActions.JoinCall}`, (ev: CustomEvent<IWidgetApiRequest>) => {
resolve();
this.ack(ev);
}),
);
const waitForJoin = this.waitForAction(ElementWidgetActions.JoinCall);
messaging.transport.send(ElementWidgetActions.JoinCall, {});
try {
await this.timeout(waitForJoin);
await waitForJoin;
} catch (e) {
// If it timed out, clean up our advance preparations
this.activeChannel = null;
Expand All @@ -128,60 +123,56 @@ export default class VoiceChannelStore extends EventEmitter {
public disconnect = async () => {
this.assertConnected();

const waitForHangup = new Promise<void>(resolve =>
this.activeChannel.once(`action:${ElementWidgetActions.HangupCall}`, () => resolve()),
);
const waitForHangup = this.waitForAction(ElementWidgetActions.HangupCall);
this.activeChannel.transport.send(ElementWidgetActions.HangupCall, {});
await this.timeout(waitForHangup);
await waitForHangup;

// onHangup cleans up for us
};

public muteAudio = async () => {
this.assertConnected();

const waitForMute = new Promise<void>(resolve =>
this.activeChannel.once(`action:${ElementWidgetActions.MuteAudio}`, () => resolve()),
);
const waitForMute = this.waitForAction(ElementWidgetActions.MuteAudio);
this.activeChannel.transport.send(ElementWidgetActions.MuteAudio, {});
await this.timeout(waitForMute);
await waitForMute;
};

public unmuteAudio = async () => {
this.assertConnected();

const waitForUnmute = new Promise<void>(resolve =>
this.activeChannel.once(`action:${ElementWidgetActions.UnmuteAudio}`, () => resolve()),
);
const waitForUnmute = this.waitForAction(ElementWidgetActions.UnmuteAudio);
this.activeChannel.transport.send(ElementWidgetActions.UnmuteAudio, {});
await this.timeout(waitForUnmute);
await waitForUnmute;
};

public muteVideo = async () => {
this.assertConnected();

const waitForMute = new Promise<void>(resolve =>
this.activeChannel.once(`action:${ElementWidgetActions.MuteVideo}`, () => resolve()),
);
const waitForMute = this.waitForAction(ElementWidgetActions.MuteVideo);
this.activeChannel.transport.send(ElementWidgetActions.MuteVideo, {});
await this.timeout(waitForMute);
await waitForMute;
};

public unmuteVideo = async () => {
this.assertConnected();

const waitForUnmute = new Promise<void>(resolve =>
this.activeChannel.once(`action:${ElementWidgetActions.UnmuteVideo}`, () => resolve()),
);
const waitForUnmute = this.waitForAction(ElementWidgetActions.UnmuteVideo);
this.activeChannel.transport.send(ElementWidgetActions.UnmuteVideo, {});
await this.timeout(waitForUnmute);
await waitForUnmute;
};

private assertConnected = () => {
if (!this.activeChannel) throw new Error("Not connected to any voice channel");
};

private timeout = async (wait: Promise<void>) => {
private waitForAction = async (action: ElementWidgetActions) => {
const wait = new Promise<void>(resolve =>
this.activeChannel.once(`action:${action}`, (ev: CustomEvent<IWidgetApiRequest>) => {
resolve();
this.ack(ev);
}),
);
if (await timeout(wait, false, VoiceChannelStore.TIMEOUT) === false) {
throw new Error("Communication with voice channel timed out");
}
Expand Down

0 comments on commit 82aeda4

Please sign in to comment.