aboutsummaryrefslogtreecommitdiff
path: root/node_modules/vuepress/lib/app/SWUpdateEvent.js
blob: fe6ab31c338493108d5933c72fe0b6e81c776345 (plain)
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
export default class SWUpdateEvent {
  constructor (registration) {
    Object.defineProperty(this, 'registration', {
      value: registration,
      configurable: true,
      writable: true
    })
  }

  /**
   * Check if the new service worker exists or not.
   */
  update () {
    return this.registration.update()
  }

  /**
   * Activate new service worker to work 'location.reload()' with new data.
   */
  skipWaiting () {
    const worker = this.registration.waiting
    if (!worker) {
      return Promise.resolve()
    }

    console.log('[vuepress:sw] Doing worker.skipWaiting().')
    return new Promise((resolve, reject) => {
      const channel = new MessageChannel()

      channel.port1.onmessage = (event) => {
        console.log('[vuepress:sw] Done worker.skipWaiting().')
        if (event.data.error) {
          reject(event.data.error)
        } else {
          resolve(event.data)
        }
      }

      worker.postMessage({ type: 'skip-waiting' }, [channel.port2])
    })
  }
}