You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
const EventEmitter = require('events')
|
|
|
|
module.exports = class serviceWorkerController extends EventEmitter{
|
|
constructor (opts) {
|
|
super()
|
|
this.fileName = opts.fileName
|
|
this.registerOpts = opts.registerOpts
|
|
this.serviceWorker = navigator.serviceWorker
|
|
}
|
|
|
|
|
|
startWorker () {
|
|
// check to see if their is a preregistered service worker
|
|
if (!this.serviceWorker.controller) {
|
|
return Promise.resolve(this.registerWorker())
|
|
} else {
|
|
return Promise.resolve(this.serviceWorker.ready)
|
|
}
|
|
}
|
|
|
|
registerWorker () {
|
|
return this.serviceWorker.register(this.fileName, this.registerOpts)
|
|
.then(sw => {
|
|
return sw
|
|
})
|
|
}
|
|
|
|
syncSW (registeredSW) {
|
|
return registeredSW.sync.register('sync')
|
|
.then(() => {
|
|
console.log('sync')
|
|
})
|
|
}
|
|
|
|
sendMessage (message) {
|
|
const self = this
|
|
return new Promise((resolve, reject) => {
|
|
var messageChannel = new MessageChannel()
|
|
messageChannel.port1.onmessage = (event) => {
|
|
if (event.data.err) {
|
|
reject(event.data.error)
|
|
} else {
|
|
resolve(event.data.data)
|
|
}
|
|
}
|
|
self.serviceWorker.controller.postMessage(message, [messageChannel.port2])
|
|
})
|
|
}
|
|
}
|
|
|