Skip to content

Instantly share code, notes, and snippets.

@edwindotcom
Last active October 2, 2015 22:53
Show Gist options
  • Save edwindotcom/6bef8e194e7c8db14711 to your computer and use it in GitHub Desktop.
Save edwindotcom/6bef8e194e7c8db14711 to your computer and use it in GitHub Desktop.
function sendMsgToSW(){
sendMessage({
command: 'helloWorld',
url: 'foo'
}).then(function() {
// If the promise resolves, just display a success message.
writeLog('postMessage promise returned');
}).then(function(val){
writeLog(val);
}).catch(console.error);
}
function sendMessage(message) {
// This wraps the message posting/response in a promise, which will resolve if the response doesn't
// contain an error, and reject with the error if it does. If you'd prefer, it's possible to call
// controller.postMessage() and set up the onmessage handler independently of a promise, but this is
// a convenient wrapper.
return new Promise(function(resolve, reject) {
var messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function(event) {
if (event.data.error) {
reject(event.data.error);
} else {
resolve(event.data);
// writeLog('port1.onmessage got: ' + event.data);
}
};
// This sends the message data as well as transferring messageChannel.port2 to the service worker.
// The service worker can then use the transferred port to reply via postMessage(), which
// will in turn trigger the onmessage handler on messageChannel.port1.
// See https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage
navigator.serviceWorker.controller.postMessage(message, [messageChannel.port2]);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment