From 7a0c4c5296bff324350ecf5de3c74472b0ab0973 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Mon, 20 May 2024 20:48:30 +0200 Subject: [PATCH] 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. --- .../discourse/controllers/preferences-chat.js | 2 +- .../discourse/services/chat-audio-manager.js | 20 +++++-------------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/plugins/chat/assets/javascripts/discourse/controllers/preferences-chat.js b/plugins/chat/assets/javascripts/discourse/controllers/preferences-chat.js index 15862aaa844..d7609513e1d 100644 --- a/plugins/chat/assets/javascripts/discourse/controllers/preferences-chat.js +++ b/plugins/chat/assets/javascripts/discourse/controllers/preferences-chat.js @@ -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); } diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-audio-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-audio-manager.js index 0c676d2b86d..21ec88e1da5 100644 --- a/plugins/chat/assets/javascripts/discourse/services/chat-audio-manager.js +++ b/plugins/chat/assets/javascripts/discourse/services/chat-audio-manager.js @@ -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." ); } - }); + } } }