FIX: improves chat audio notification reliability (#27089)

Debouncing the audio was causing the audio to be lost sometimes, somewhat randomly. It's not supposed to be necessary.

The commit also refactors the code to async/await.
This commit is contained in:
Joffrey JAFFEUX 2024-05-20 20:48:30 +02:00 committed by GitHub
parent fb9a43c282
commit 7a0c4c5296
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 16 deletions

View File

@ -87,7 +87,7 @@ export default class PreferencesChatController extends Controller {
@action
onChangeChatSound(sound) {
if (sound) {
this.chatAudioManager.playImmediately(sound);
this.chatAudioManager.play(sound);
}
this.model.set("user_option.chat_sound", sound);
}

View File

@ -1,9 +1,6 @@
import Service from "@ember/service";
import { isTesting } from "discourse-common/config/environment";
import { getURLWithCDN } from "discourse-common/lib/get-url";
import { debounce } from "discourse-common/utils/decorators";
const AUDIO_DEBOUNCE_DELAY = 3000;
export const CHAT_SOUNDS = {
bell: [{ src: "/plugins/chat/audio/bell.mp3", type: "audio/mpeg" }],
@ -39,16 +36,7 @@ export default class ChatAudioManager extends Service {
this._audioCache = {};
}
playImmediately(soundName) {
return this._play(soundName);
}
@debounce(AUDIO_DEBOUNCE_DELAY, true)
play(soundName) {
return this._play(soundName);
}
_play(soundName) {
async play(soundName) {
const audio =
this._audioCache[soundName] || this._audioCache[DEFAULT_SOUND_NAME];
@ -63,13 +51,15 @@ export default class ChatAudioManager extends Service {
}
}
return audio.play().catch(() => {
try {
await audio.play();
} catch (e) {
if (!isTesting()) {
// eslint-disable-next-line no-console
console.info(
"[chat] User needs to interact with DOM before we can play notification sounds."
);
}
});
}
}
}