Skip to content

Commit

Permalink
Silently apply updates instead of asking user to confirm
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Oct 24, 2022
1 parent 0976b93 commit 1f6a723
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion frontend/platform/ServiceWorkerHandler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
import { ServiceWorkerHandler as BaseHandler } from "hydrogen-web/src/platform/web/dom/ServiceWorkerHandler.js";

export class ServiceWorkerHandler extends BaseHandler {
private get registration() {
return super.getRegistration();
}

public registerAndStart(path: string) {
super.registerAndStart(path);
}

public async preventConcurrentSessionAccess(sessionId: string) {
return super._sendAndWaitForReply("closeSession", { sessionId });
return this._sendAndWaitForReply("closeSession", { sessionId });
}

async _proposeUpdate() {
if (document.hidden) {
return;
}

const version = await this._sendAndWaitForReply("version", null, this.registration.waiting);

// Prevent any fetch requests from going to the service worker from any client,
// so that it's not kept active when calling skipWaiting on the new one.
await this._sendAndWaitForReply("haltRequests", null);

console.log(`Updating to version ${version.version} (${version.buildHash})`);

// Once all requests are blocked, ask the new service worker to skipWaiting.
return this._send("skipWaiting", null, this.registration.waiting);
}

async _sendAndWaitForReply(type, payload: {}|null, worker = undefined) {
return super._sendAndWaitForReply(type, payload, worker);
}

async _send(type, payload, worker = undefined) {
return super._send(type, payload, worker);
}
}

0 comments on commit 1f6a723

Please sign in to comment.