swcaller
v0.0.4
Published
call a function in service worker
Downloads
2
Readme
swcaller
call a function in service worker
Example
in index.js
const swcaller = require('swcaller/index.js')
await navigator.serviceWorker.register('/sw.js', { scope: '/' })
var out = await swcaller.call({ type: 'hello', name: 'alan turing' })
console.log(out) // 'hello alan turing!'
in service worker file sw.js
self.addEventListener('message', function(event) {
if (event.data.type == 'hello') {
var port = event.ports && event.ports[0]
if (!port) return
port.postMessage(`hello ${event.data.name}!`);
})