-
-
Save pdehaan/bb831551a6bc5fa58afb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sendMsgToSW(){ | |
sendMessage({ | |
command: 'helloWorld', | |
url: 'foo' | |
}).then(function(val) { | |
// If the promise resolves, just display a success message. | |
writeLog('postMessage promise returned'); | |
writeLog(val); | |
return 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