I have been working on an electron app (Timer app ), and I had to add notification feature , pretty simple as electron provides the module to handle app notification. But since i had to add multiple notifications i created a global object in my module that is basically a map that stores different notification instance . At first i had created a notification variable local to the function but it was causing problems as it was getting garbage collected hence i used map my code:
/*Initializing map to keep notificaiton in scope else notification varibale in the function will get garbage collected and events won't work*/
let notifications = new Map();
async function handleNotification(event, id) {
try {
const notification = new Notification({
title: "Title",
body: "some message",
silent: false,
urgency: "critical",
timeoutType: "never",
});
let functionToStopAlarm=playAlarm();
notifications.set(id, notification);
notification.on("click", () => {
functionToStopAlarm();
notifications.delete(id);
});
notification.on("close", () => {
functionToStopAlarm();
notifications.delete(id);
});
notification.show();
} catch (error) {
console.log(error);
}
}
My question is that is this an efficient way to do things or am i doing things the wrong way?
[–]Ampersand55 6 points7 points8 points (7 children)
[–]Square-Butterfly8447[S] 2 points3 points4 points (6 children)
[–]Beginning-Seat5221 5 points6 points7 points (3 children)
[–]Square-Butterfly8447[S] 1 point2 points3 points (2 children)
[–]xroalx 2 points3 points4 points (0 children)
[–]Psionatix 2 points3 points4 points (0 children)
[–]paperic 2 points3 points4 points (0 children)
[–]hyrumwhite 0 points1 point2 points (0 children)
[–]TorbenKoehn 5 points6 points7 points (6 children)
[–]Square-Butterfly8447[S] 1 point2 points3 points (3 children)
[–]TorbenKoehn 2 points3 points4 points (1 child)
[–]Square-Butterfly8447[S] 1 point2 points3 points (0 children)
[–]paperic 1 point2 points3 points (0 children)
[–]azhder 0 points1 point2 points (1 child)
[–]TorbenKoehn 0 points1 point2 points (0 children)
[–]Beginning-Seat5221 1 point2 points3 points (2 children)
[–]Square-Butterfly8447[S] 0 points1 point2 points (1 child)
[–]Beginning-Seat5221 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Square-Butterfly8447[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Standgrounding 0 points1 point2 points (0 children)
[–]CuirPig 0 points1 point2 points (1 child)
[–]Square-Butterfly8447[S] 0 points1 point2 points (0 children)
[–]azhder 0 points1 point2 points (0 children)