-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
35 lines (30 loc) · 948 Bytes
/
Copy pathworker.js
File metadata and controls
35 lines (30 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
let notifications = [];
function writeLog(log) {
postMessage({type: 'writeLog', msg: log});
}
writeLog("Worker loaded");
onmessage = (e) => {
const type = e.data.type;
if (type === "showNotification") {
writeLog("I will try to show a notification");
showNotification(e.data.msg);
}
else if (type === "clearNotifications") {
writeLog("I will clear all notifications but not ones who was displayed before page refresh");
notifications.forEach(n => n.close());
notifications = [];
}
};
function showNotification(msg) {
const notification = new Notification(msg);
notification.onclick = function () {
this.close();
}
notification.onshow = function () {
writeLog("Notification displayed")
}
notification.onclose = function () {
notifications = notifications.filter(n => n !== notification);
}
notifications.push(notification);
}