map-ify audioPromises

This commit is contained in:
David Baker 2020-09-24 18:28:46 +01:00
parent 4269c26e76
commit 10338798d9

View File

@ -79,7 +79,7 @@ type Call = any;
export default class CallHandler { export default class CallHandler {
private calls = new Map<string, Call>(); private calls = new Map<string, Call>();
private audioPromises = {}; private audioPromises = new Map<string, Promise<void>>();
static sharedInstance() { static sharedInstance() {
if (!window.mxCallHandler) { if (!window.mxCallHandler) {
@ -119,7 +119,7 @@ export default class CallHandler {
return null; return null;
} }
play(audioId) { play(audioId: string) {
// TODO: Attach an invisible element for this instead // TODO: Attach an invisible element for this instead
// which listens? // which listens?
const audio = document.getElementById(audioId) as HTMLMediaElement; const audio = document.getElementById(audioId) as HTMLMediaElement;
@ -137,32 +137,32 @@ export default class CallHandler {
console.log("Unable to play audio clip", e); console.log("Unable to play audio clip", e);
} }
}; };
if (this.audioPromises[audioId]) { if (this.audioPromises.has(audioId)) {
this.audioPromises[audioId] = this.audioPromises[audioId].then(() => { this.audioPromises.set(audioId, this.audioPromises.get(audioId).then(() => {
audio.load(); audio.load();
return playAudio(); return playAudio();
}); }));
} else { } else {
this.audioPromises[audioId] = playAudio(); this.audioPromises.set(audioId, playAudio());
} }
} }
} }
pause(audioId) { pause(audioId: string) {
// TODO: Attach an invisible element for this instead // TODO: Attach an invisible element for this instead
// which listens? // which listens?
const audio = document.getElementById(audioId) as HTMLMediaElement; const audio = document.getElementById(audioId) as HTMLMediaElement;
if (audio) { if (audio) {
if (this.audioPromises[audioId]) { if (this.audioPromises.has(audioId)) {
this.audioPromises[audioId] = this.audioPromises[audioId].then(() => audio.pause()); this.audioPromises.set(audioId, this.audioPromises.get(audioId).then(() => audio.pause()));
} else { } else {
// pause doesn't actually return a promise, but might as well do this for symmetry with play(); // pause doesn't return a promise, so just do it
this.audioPromises[audioId] = audio.pause(); audio.pause();
} }
} }
} }
private setCallListeners(call) { private setCallListeners(call: Call) {
call.on("error", (err) => { call.on("error", (err) => {
console.error("Call error:", err); console.error("Call error:", err);
if ( if (
@ -218,7 +218,7 @@ export default class CallHandler {
}); });
} }
private setCallState(call, roomId, status) { private setCallState(call: Call, roomId: string, status: string) {
console.log( console.log(
`Call state in ${roomId} changed to ${status} (${call ? call.call_state : "-"})`, `Call state in ${roomId} changed to ${status} (${call ? call.call_state : "-"})`,
); );
@ -391,7 +391,7 @@ export default class CallHandler {
} }
} }
private async startCallApp(roomId, type) { private async startCallApp(roomId: string, type: string) {
dis.dispatch({ dis.dispatch({
action: 'appsDrawer', action: 'appsDrawer',
show: true, show: true,