|
1 | 1 | import { ServiceWorkerHandler as BaseHandler } from "hydrogen-web/src/platform/web/dom/ServiceWorkerHandler.js"; |
2 | 2 |
|
3 | 3 | export class ServiceWorkerHandler extends BaseHandler { |
| 4 | + private get registration() { |
| 5 | + return super.getRegistration(); |
| 6 | + } |
| 7 | + |
4 | 8 | public registerAndStart(path: string) { |
5 | 9 | super.registerAndStart(path); |
6 | 10 | } |
7 | 11 |
|
8 | 12 | public async preventConcurrentSessionAccess(sessionId: string) { |
9 | | - return super._sendAndWaitForReply("closeSession", { sessionId }); |
| 13 | + return this._sendAndWaitForReply("closeSession", { sessionId }); |
| 14 | + } |
| 15 | + |
| 16 | + async _proposeUpdate() { |
| 17 | + if (document.hidden) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + const version = await this._sendAndWaitForReply("version", null, this.registration.waiting); |
| 22 | + |
| 23 | + // Prevent any fetch requests from going to the service worker from any client, |
| 24 | + // so that it's not kept active when calling skipWaiting on the new one. |
| 25 | + await this._sendAndWaitForReply("haltRequests", null); |
| 26 | + |
| 27 | + console.log(`Updating to version ${version.version} (${version.buildHash})`); |
| 28 | + |
| 29 | + // Once all requests are blocked, ask the new service worker to skipWaiting. |
| 30 | + return this._send("skipWaiting", null, this.registration.waiting); |
| 31 | + } |
| 32 | + |
| 33 | + async _sendAndWaitForReply(type, payload: {}|null, worker = undefined) { |
| 34 | + return super._sendAndWaitForReply(type, payload, worker); |
| 35 | + } |
| 36 | + |
| 37 | + async _send(type, payload, worker = undefined) { |
| 38 | + return super._send(type, payload, worker); |
10 | 39 | } |
11 | 40 | } |
0 commit comments