summaryrefslogtreecommitdiff
path: root/worker.js
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2017-07-26 14:10:05 +0300
committerAndrew Dolgov <[email protected]>2017-07-26 14:10:05 +0300
commitd7d4b51384451a978637e147efe10a7cb4164772 (patch)
tree23d9d04ae7ec77e7dcd6085c89f1932e8f50f68d /worker.js
parent3797c984f0310bf4539691c1b4aa1f753280c37d (diff)
when force-updating cache reload client when work is done
Diffstat (limited to 'worker.js')
-rw-r--r--worker.js24
1 files changed, 23 insertions, 1 deletions
diff --git a/worker.js b/worker.js
index 83241a8..7d27d4d 100644
--- a/worker.js
+++ b/worker.js
@@ -37,11 +37,25 @@ self.addEventListener('install', function(event) {
);
});
+function send_message(client, msg) {
+ client.postMessage(msg);
+}
+
+function send_broadcast(msg) {
+ clients.matchAll().then(clients => {
+ clients.forEach(client => {
+ send_message(client, msg);
+ })
+ })
+}
+
self.addEventListener('message', function(event){
if (event.data == 'refresh-cache') {
console.log("refreshing cache...");
caches.open(CACHE_NAME).then(function(cache) {
+ var promises = [];
+
cache.keys().then(function(keys) {
for (var i = 0; i < keys.length; i++) {
@@ -50,7 +64,7 @@ self.addEventListener('message', function(event){
//console.log(keys[i]);
- fetch(keys[i]).then(function(resp) {
+ var promise = fetch(keys[i]).then(function(resp) {
if (resp.status == 200) {
cache.put(resp.url, resp);
} else if (resp.status == 404) {
@@ -58,8 +72,16 @@ self.addEventListener('message', function(event){
}
});
+ promises.push(promise);
+
}
});
+
+ Promise.all(promises).then(function() {
+ console.log('all done');
+ send_broadcast('client-reload');
+ });
+
});
}
});