push-sw.worker.js (983B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /* global clients */ 5 6 "use strict"; 7 8 // Send a message to all controlled windows. 9 /* eslint-disable-next-line no-redeclare */ 10 function postMessage(message) { 11 return clients.matchAll().then(function (clientlist) { 12 clientlist.forEach(function (client) { 13 client.postMessage(message); 14 }); 15 }); 16 } 17 18 // Don't wait for the next page load to become the active service worker. 19 self.addEventListener("install", function (event) { 20 event.waitUntil(self.skipWaiting()); 21 }); 22 23 // Claim control over the currently open test page when activating. 24 self.addEventListener("activate", function (event) { 25 event.waitUntil( 26 self.clients.claim().then(function () { 27 return postMessage("sw-claimed"); 28 }) 29 ); 30 }); 31 32 // Forward all "push" events to the controlled window. 33 self.addEventListener("push", function (event) { 34 event.waitUntil(postMessage("sw-pushed")); 35 });