blob: 1a7b1cad38804a89267d51d55abd974e2a27df53 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
const EventEmitter = require('events')
module.exports = class ClientSideServiceWorker extends EventEmitter{
constructor (opts) {
super()
this.fileName = opts.fileName
this.startDelay = opts.startDelay
this.serviceWorkerApi = navigator.serviceWorker
this.serviceWorkerApi.onmessage = (messageEvent) => this.emit('message', messageEvent)
this.serviceWorkerApi.onerror = (err) => this.emit('error', err)
this.on('message', (messageEvent) => {debugger})
if (opts.initStart) this.startWorker()
}
get controller () {
return this.sw || this.serviceWorkerApi.controller
}
startWorker () {
return this.registerWorker()
.then((sw) => {
this.sw = sw
this.sw.onerror = (err) => this.emit('error', err)
this.sw = sw
this.emit('ready', this.sw)
})
.catch((err) => this.emit('error', err))
}
registerWorker () {
return this.serviceWorkerApi.register(this.fileName)
.then((registerdWorker) => {
return new Promise((resolve, reject) => {
let timeOutId = setTimeout(() => {
if (this.serviceWorkerApi.controller) return resolve(this.serviceWorkerApi.controller)
if (registerdWorker.active) return resolve(registerdWorker.active)
return reject(new Error('ClientSideServiceWorker: No controller found and onupdatefound timed out'))
}, this.startDelay || 1000 )
registerdWorker.onupdatefound = (event) => {
this.emit('updatefound')
registerdWorker.update()
}
})
})
}
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)
}
}
this.controller.postMessage(message, [messageChannel.port2])
})
}
}
|