better comment explaining the need for initServiceWorker() in event handlers (ref. #31)

merge-requests/12/merge
Michał 'rysiek' Woźniak 2022-02-01 03:10:28 +00:00
rodzic fe81a2232a
commit c37cd53a89
1 zmienionych plików z 19 dodań i 2 usunięć

Wyświetl plik

@ -760,7 +760,14 @@ self.addEventListener('activate', async event => {
self.addEventListener('fetch', async event => {
return void event.respondWith(async function () {
// initialize the SW
// initialize the SW; this is necessary as SW can be stopped at any time
// and restarted when an event gets triggered -- `fetch` is just such an event.
//
// `install` and `activate` events only handle the initial installation of the SW
// this means that we might end up here without initServiceWorker() ever being run
// and so in a situation where plugins have never been actually set-up!
//
// the good news is that the config.json should have been cached already
await initServiceWorker()
// if event.resultingClientId is available, we need to use this
// otherwise event.clientId is what we want
@ -833,7 +840,17 @@ self.addEventListener('fetch', async event => {
* assumptions to be considered:
* every message contains clientId (so that we know where to respond if/when we need to)
*/
self.addEventListener('message', (event) => {
self.addEventListener('message', async (event) => {
// initialize the SW; this is necessary as SW can be stopped at any time
// and restarted when an event gets triggered -- `message` is just such an event.
//
// `install` and `activate` events only handle the initial installation of the SW
// this means that we might end up here without initServiceWorker() ever being run
// and so in a situation where plugins have never been actually set-up!
//
// the good news is that the config.json should have been cached already
await initServiceWorker()
// inform
var msg = 'Message received!'