[ServiceWorker] Client.postMessage should return a Promise (#677)

[Client.postMessage](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#client-postmessage-method) can fail for various reasons, for example:

- user or JS closed the tab
- user or JS navigated* the tab to a non-same-origin URL
- tab is still open, but browser evicted it from RAM

As suggested in https://github.com/slightlyoff/ServiceWorker/issues/609#issuecomment-74920652 and https://github.com/slightlyoff/ServiceWorker/issues/626#issuecomment-74918907, it would be useful if Client.postMessage returned a Promise that would be resolved once the message event has been fired on the client, or rejected if the client ceases to exist before the message event can be fired.

Example use case [from](https://github.com/slightlyoff/ServiceWorker/issues/609#issuecomment-74920652) @jakearchibald:
> After a push message, a SW may find a focused & visible client and post a message to it, but that fails because the tab has since been closed. Developers should be able to catch that case and show a notification instead.

@annevk [is concerned](https://github.com/slightlyoff/ServiceWorker/issues/609#issuecomment-75936104) that this would expose GC, but it remains unclear why it's bad for JS to be able to tell which tabs are still open/alive - especially since this can already be achieved by polling. So far, these concerns don't seem to outweigh the utility of the use case above.

*: some browsers have a back-forward cache (a.k.a. fastback) feature like https://wiki.mozilla.org/DocShell:Fastback - in such cases navigation doesn't destroy a client, it merely pauses all timers. In such cases where timers are paused, I guess the promise should neither resolve nor reject, but instead wait until the timers are resumed (e.g. because the user returns to the cached page) or the client ceases to exist (e.g. because the client is evicted from the back-forward cache).

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/677

Received on Friday, 10 April 2015 12:47:54 UTC