refactor: notify.helpers.js logic. Add a timeout and a better notification management to avoid race condition on Unix Systems

This commit is contained in:
MasterAcnolo
2026-08-05 22:15:53 +02:00
committed by MasterAcnolo
parent b9334461bf
commit 321309e294

View File

@@ -2,6 +2,12 @@ const { Notification, shell } = require("electron");
const { iconPaths } = require("./path.helpers"); const { iconPaths } = require("./path.helpers");
const { logger } = require("../logger"); const { logger } = require("../logger");
/**
* Set that is holding all notifications. Used to avoid Race Condition and notification crash
* @type {Set<any>}
*/
const activeNotifications = new Set();
/** /**
* Displays a system notification when a download completes successfully. * Displays a system notification when a download completes successfully.
* *
@@ -21,7 +27,27 @@ function notifyDownloadFinished(folder, notifyEnabled = true) {
icon: iconPaths.confirm, 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(); notif.show();
} }
@@ -41,11 +67,16 @@ function notifyCookiesBrowserError(){
icon: iconPaths.error, icon: iconPaths.error,
}); });
notif.on("click", () => activeNotifications.add(notif);
shell.openExternal(
"https://youtube.com/shorts/cN9f4s1Mf88?si=519QCVd_-fzJqRf1" notif.on("click", () => {
) shell.openExternal("https://www.firefox.com/en-US/download/");
); activeNotifications.delete(notif);
});
notif.on("close", () => {
activeNotifications.delete(notif);
});
notif.show(); notif.show();
} }
@@ -65,11 +96,16 @@ function notifyFirefoxBrowserMissing() {
icon: iconPaths.error, icon: iconPaths.error,
}); });
notif.on("click", () => activeNotifications.add(notif);
shell.openExternal(
"https://youtube.com/shorts/cN9f4s1Mf88?si=519QCVd_-fzJqRf1" notif.on("click", () => {
) shell.openExternal("https://www.firefox.com/en-US/download/");
); activeNotifications.delete(notif);
});
notif.on("close", () => {
activeNotifications.delete(notif);
});
notif.show(); notif.show();
} }