- From: Jungkee Song <notifications@github.com>
- Date: Tue, 09 Dec 2014 04:59:24 -0800
- To: slightlyoff/ServiceWorker <ServiceWorker@noreply.github.com>
- Message-ID: <slightlyoff/ServiceWorker/issues/453/66278724@github.com>
`ServiceWorkerClient` would be great. Having discussed with @jakearchibald last week, we plan to have `ServiceWorkerClient` object represent both Window client and Worker client. (That is, a `ServiceWorkerClient` object can be used for all the global environments except SW global environment.) And we also would like to avoid passing `MessagePort` objects around as discussed earlier. Having said that, how about the following usage?
```js
// both in Window global and in Worker global
navigator.serviceWorker.getRegistration().then(registration => {
if (registration.active) {
registration.active.onmessage = (e) => { console.log(e.data); };
registration.active.postMessage("Howdy from client.");
}
});
// in service worker
self.addEventListener("message", (e) => {
var client = e.source; // e.source is the ServiceWorkerClient object that represents the message sender
if (client.type == "window" || client.type == "shared worker")
client.postMessage("Howdy from SW."));
});
// aligned with what authors do with clients gotten from clients.getAll()
self.addEventListener("fetch", (e) => {
caches.match(e.request).then(response => {
clients.getAll().then(clients => {
clients.forEach(client => client.postMessage(response.clone()));
});
});
});
```
---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/453#issuecomment-66278724
Received on Tuesday, 9 December 2014 12:59:50 UTC