- From: dfabulich <notifications@github.com>
- Date: Thu, 14 Dec 2017 09:26:08 -0800
- To: w3c/ServiceWorker <ServiceWorker@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <w3c/ServiceWorker/issues/1247@github.com>
Breaking off from #1222.
`ServiceWorker.state` has five states: `installing`, `installed` (waiting), `activating`, `activated`, and `redundant`.
Today, client-side code can listen for `installing` SWs with `ServiceWorkerRegistration.onupdatefound`; `activated` can be tracked with `navigator.serviceWorker.oncontrollerchange`.
Some users apparently want to track `redundant` SWs for error handling, and perhaps someone would want to log the `activating` event for performance tracking. Lots of users want to track the `installed` state so they can show a refresh banner.
The other three states are annoying to listen for.
```js
function listenForStateChanges(reg, callback) {
if (reg.installing) reg.installing.addEventListener('statechange', callback);
if (reg.waiting) reg.waiting.addEventListener('statechange', callback);
reg.addEventListener('updatefound', function() {
reg.installing.addEventListener('statechange', callback);
});
}
```
In #1222 I suggested a promise-based API for a waiting SW, but it seems like it might be nice if `statechange` bubbled up to the registration, so users who wanted to track the `redundant` and `activating` states would just do this:
```js
reg.addEventListener('statechange', callback);
```
Listening for a redundant service worker might look like this:
```js
navigator.serviceWorker.ready.then(function(reg) {
reg.addEventListener('statechange', function(e) {
if (e.target.state === 'redundant') console.log('redundant');
});
});
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/ServiceWorker/issues/1247
Received on Thursday, 14 December 2017 17:26:32 UTC