Re: [w3c/ServiceWorker] Provide a one-line way to listen for a waiting Service Worker (#1222)

1. I suggested `.waiting` to parallel `.ready`
2. If the new API is an event, instead of a promise, I'd prefer the event to fire from `navigator.serviceWorker` rather than the registration. ServiceWorkerRegistration's `updatefound` event tells you about the `installing` SW, and `navigator.serviceWorker`'s `controllerchange` event tells you about the `activated` SW. This API feels inconsistent; it's slightly more convenient to listen to `navigator.serviceWorker` than it is to get the registration and listen to that.
3. I don't think a full-blown `updatestatechange` event would be very useful. I can't think of any use case for knowing when there's a Service Worker activating but not yet activated. (What would client-side code do in response to that?) But I can't even think of a use case for knowing when the SW is `installing`, except so as to listen for its state changes to wait for it to start waiting. If I'm right that the only interesting states on the client side are `waiting` and `activated`, and we already have a solid event for `activated`, then it's just the `waiting` state that would benefit from sugar.
4. Promises are just nicer than event listeners.

It's hard to beat this one-liner: `navigator.serviceWorker.waiting.then(alertUser)`

This is definitely uglier.
```js
navigator.serviceWorker.ready.then(function(reg) {
  reg.addEventListener('updatestatechange', function(event) {
    if (event.state === 'waiting') alertUser(event.registration);
  });
});
```

If `updatestatechange` were on `navigator.serviceWorker`, it would be acceptable.

```js
navigator.serviceWorker.addEventListener('updatestatechange', function(event) {
    if (event.state === 'waiting') alertUser(event.registration);
  });
});
```

But at that point, it would be nicer if it were an `updatewaiting` event.

```js
navigator.serviceWorker.addEventListener('updatewaiting', function(event) {
    alertUser(event.registration);
  });
});
```

But then, what would I use the event for? It wouldn't be extendable or anything. Might as well just provide a promise and eliminate the event altogether.

`navigator.serviceWorker.waiting.then(alertUser)`

-- 
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/1222#issuecomment-351629836

Received on Thursday, 14 December 2017 07:26:58 UTC