-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
79 lines (73 loc) · 2.13 KB
/
sw.js
File metadata and controls
79 lines (73 loc) · 2.13 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* eslint-env browser, serviceworker */
// if (false) { var self, clients, registration }
var pendingTimeout = null
var pendingInterval = null
var remainingTimeout = null
self.onnotificationclick = function (event) {
// console.log('On notification click: ', event.notification.tag)
event.notification.close()
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll(
/*{
type: "window"
}*/
).then(function (clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i]
if ('focus' in client) {
var msgChan = new MessageChannel()
client.postMessage({clicked: true}, [msgChan.port2])
return client.focus()
}
}
// if (clients.openWindow)
// return clients.openWindow('/');
}))
}
var sendTick = function () {
clients.matchAll(
/*{
type: "window"
}*/
).then(function (clientList) {
if (remainingTimeout < 0) {
remainingTimeout = 0
clearInterval(pendingInterval)
}
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i]
if ('focus' in client) {
var msgChan = new MessageChannel()
client.postMessage({tick: true, remainingTimeout: remainingTimeout}, [msgChan.port2])
remainingTimeout -= 1000
return
}
}
})
}
var stopTimer = function () {
remainingTimeout = 0
sendTick()
clearTimeout(pendingTimeout)
clearInterval(pendingInterval)
}
self.onmessage = function (messageEvent) {
// console.debug('serviceWorker received message:', messageEvent)
if (messageEvent.data.type === 'STOP_TIMER') {
stopTimer()
var {title, notification} = messageEvent.data
setTimeout(function () {
registration.showNotification(title, notification)
}, 0)
}
if (messageEvent.data.type === 'SHOW_NOTIFICATION') {
stopTimer()
var {title, notification, timeout} = messageEvent.data
remainingTimeout = timeout
pendingTimeout = setTimeout(function () {
registration.showNotification(title, notification)
}, timeout)
pendingInterval = setInterval(sendTick, 1000)
}
}