From 321309e2949811234162ca019c926438d5977d03 Mon Sep 17 00:00:00 2001 From: MasterAcnolo Date: Wed, 5 Aug 2026 22:15:53 +0200 Subject: [PATCH] refactor: notify.helpers.js logic. Add a timeout and a better notification management to avoid race condition on Unix Systems --- server/helpers/notify.helpers.js | 58 ++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/server/helpers/notify.helpers.js b/server/helpers/notify.helpers.js index e614720..c262d26 100644 --- a/server/helpers/notify.helpers.js +++ b/server/helpers/notify.helpers.js @@ -2,6 +2,12 @@ const { Notification, shell } = require("electron"); const { iconPaths } = require("./path.helpers"); const { logger } = require("../logger"); +/** + * Set that is holding all notifications. Used to avoid Race Condition and notification crash + * @type {Set} + */ +const activeNotifications = new Set(); + /** * Displays a system notification when a download completes successfully. * @@ -21,7 +27,27 @@ function notifyDownloadFinished(folder, notifyEnabled = true) { icon: iconPaths.confirm, }); - notif.on("click", () => shell.openPath(folder)); + // Protect notification to garbage collection and race condition, by adding a 150ms timeout before trying to open the folder + activeNotifications.add(notif); + + notif.on("click", () => { + activeNotifications.delete(notif); + + setTimeout(() => { + shell.openPath(folder).then((errorMessage) => { + if (errorMessage) { + logger.error(`Impossible d'ouvrir le dossier : ${errorMessage}`); + } + }).catch(err => { + logger.error(`Erreur inattendue de shell.openPath : ${err}`); + }); + }, 150); + }); + + notif.on("close", () => { + activeNotifications.delete(notif); + }); + notif.show(); } @@ -41,11 +67,16 @@ function notifyCookiesBrowserError(){ icon: iconPaths.error, }); - notif.on("click", () => - shell.openExternal( - "https://youtube.com/shorts/cN9f4s1Mf88?si=519QCVd_-fzJqRf1" - ) - ); + activeNotifications.add(notif); + + notif.on("click", () => { + shell.openExternal("https://www.firefox.com/en-US/download/"); + activeNotifications.delete(notif); + }); + + notif.on("close", () => { + activeNotifications.delete(notif); + }); notif.show(); } @@ -65,11 +96,16 @@ function notifyFirefoxBrowserMissing() { icon: iconPaths.error, }); - notif.on("click", () => - shell.openExternal( - "https://youtube.com/shorts/cN9f4s1Mf88?si=519QCVd_-fzJqRf1" - ) - ); + activeNotifications.add(notif); + + notif.on("click", () => { + shell.openExternal("https://www.firefox.com/en-US/download/"); + activeNotifications.delete(notif); + }); + + notif.on("close", () => { + activeNotifications.delete(notif); + }); notif.show(); }