From f14edd52420b9c593bec0d99b6185e3c29376aae Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 3 Oct 2024 15:03:44 +0530 Subject: [PATCH] fix: initOnEvents not removed [CW-3594] (#10200) The `initOnEvents` was used to get the notification sound file and trigger the 30 second loop, but since the function was replaced to using class syntax, the removeEvent listener was not working. This PR fixes it by reverting to the old syntax but moving it inside the constructor instead and also adding a `once: true` to ensure it is always removed automatically --- .../DashboardAudioNotificationHelper.js | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/app/javascript/dashboard/helper/AudioAlerts/DashboardAudioNotificationHelper.js b/app/javascript/dashboard/helper/AudioAlerts/DashboardAudioNotificationHelper.js index 8da464c90..123a03360 100644 --- a/app/javascript/dashboard/helper/AudioAlerts/DashboardAudioNotificationHelper.js +++ b/app/javascript/dashboard/helper/AudioAlerts/DashboardAudioNotificationHelper.js @@ -22,6 +22,21 @@ class DashboardAudioNotificationHelper { this.currentUser = null; this.currentUserId = null; this.audioAlertTone = 'ding'; + + this.onAudioListenEvent = async () => { + try { + await getAlertAudio('', { + type: 'dashboard', + alertTone: this.audioAlertTone, + }); + initOnEvents.forEach(event => { + document.removeEventListener(event, this.onAudioListenEvent, false); + }); + this.playAudioEvery30Seconds(); + } catch (error) { + // Ignore audio fetch errors + } + }; } setInstanceValues({ @@ -38,26 +53,13 @@ class DashboardAudioNotificationHelper { this.currentUserId = currentUser.id; this.audioAlertTone = audioAlertTone; initOnEvents.forEach(e => { - document.addEventListener(e, this.onAudioListenEvent, false); + document.addEventListener(e, this.onAudioListenEvent, { + once: true, + }); }); initFaviconSwitcher(); } - async onAudioListenEvent() { - try { - await getAlertAudio('', { - type: 'dashboard', - alertTone: this.audioAlertTone, - }); - initOnEvents.forEach(event => { - document.removeEventListener(event, this.onAudioListenEvent, false); - }); - this.playAudioEvery30Seconds(); - } catch (error) { - // Ignore audio fetch errors - } - } - executeRecurringNotification() { if (!window.WOOT_STORE) { this.clearSetTimeout();