-
Notifications
You must be signed in to change notification settings - Fork 2
/
sw.js
118 lines (108 loc) · 3.44 KB
/
sw.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const cacheName = "v8";
let reminders;
let checker;
const interval = 10000;
let inadvance;
let address;
function checkReminders(){
console.log('checkReminders',reminders);
let now = new Date();
for(key in reminders){
if(reminders[key]){
wait = reminders[key].data.date - now;
// If message is within the current day
if(typeof inadvance!=="number") advance = 0;
if(wait < inadvance){
let title = "Put your "+reminders[key].data.bin.toLowerCase()+' bin out';
let options = {}
if(reminders[key].data.nicedate) options.body = reminders[key].data.nicedate;
if(reminders[key].data.date) options.timestamp = reminders[key].data.date.getTime();
if(reminders[key].data.icon) options.icon = reminders[key].data.icon;
if(reminders[key].data.badge) options.badge = reminders[key].data.badge;
self.registration.showNotification(title, options);
delete reminders[key];
}else{
console.log('waiting '+((wait)/86400000).toFixed(2)+' days to post '+key);
}
}
}
return true;
}
self.addEventListener('install', function(e) {
console.log('install ServiceWorker');
e.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll([
'/bins/',
'/bins/index.html',
'/bins/resources/banner.jpg',
'/bins/resources/stuquery.js',
'/bins/resources/bins.css',
'/bins/resources/bins.js',
'/bins/data/list.json'
]);
})
);
console.log('install');
reminders = {};
inadvance = 86400000/2;
});
self.addEventListener('activate', function(e){
console.log('activate');
let cacheWhitelist = [cacheName] // the name of the new cache
e.waitUntil(
caches.keys().then (cacheNames => {
return Promise.all(
cacheNames.map( cacheName => {
/* Deleting all the caches except the ones that are in cacheWhitelist array */
if(cacheWhitelist.indexOf(cacheName) === -1){
return caches.delete(cacheName)
}
})
)
})
);
checker = setInterval(checkReminders,interval);
});
/* Evaluates the fetch request and checks to see if it is in the cache.
The response is passed back to the webpage via event.respondWith() */
self.addEventListener('fetch', function(e){
console.log('fetch '+e.request.url);
e.respondWith(
caches.match(e.request).then(function(response) {
if(response) console.log('Using cached version of '+e.request.url);
else console.log('Get resource '+e.request.url);
return response || fetch(e.request)
})
);
});
self.addEventListener('message', function(e){
console.log('Received message in Service Worker',e.data);
let data = (typeof e.data==="string") ? JSON.parse(e.data) : e.data;
function getKey(d){
return (d.date ? d.date.toISOString().substr(0,10) : 'default');
}
if(data.command === 'reminders'){
let _worker = self;
let i,d,key;
// Remove existing reminders
delete reminders;
for(i = 0; i < data.events.length; i++){
d = data.events[i];
if(d.date) d.date = new Date(d.date);
key = getKey(d);
reminders[key] = {'data':d};
}
clearInterval(checker);
checkReminders();
checker = setInterval(checkReminders,interval);
console.log('reminders',reminders);
}/*else if(data.command === 'setAddress'){
console.log('setAddress received by Worker',data,e.source);
address = data.address;
e.source.postMessage({'command':data.command,'address':address});
}else if(data.command === 'getAddress'){
console.log('getAddress received by Worker',address);
e.source.postMessage({'command':data.command,'address':address});
}*/
});