2020-10-16 12:49:42 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 3 (imaginary) interfaces are implemented here:
|
|
|
|
// - OfflineAvailability (done by registering the sw)
|
|
|
|
// - UpdateService (see checkForUpdate method, and should also emit events rather than showing confirm dialog here)
|
|
|
|
// - ConcurrentAccessBlocker (see preventConcurrentSessionAccess method)
|
|
|
|
export class ServiceWorkerHandler {
|
2020-10-26 15:44:11 +01:00
|
|
|
constructor() {
|
2020-10-16 12:49:42 +02:00
|
|
|
this._waitingForReply = new Map();
|
|
|
|
this._messageIdCounter = 0;
|
2020-10-26 15:44:11 +01:00
|
|
|
this._navigation = null;
|
2020-10-16 12:49:42 +02:00
|
|
|
this._registration = null;
|
|
|
|
this._registrationPromise = null;
|
2020-10-16 14:52:34 +02:00
|
|
|
this._currentController = null;
|
2021-03-18 19:34:41 +01:00
|
|
|
this.haltRequests = false;
|
2020-10-16 12:49:42 +02:00
|
|
|
}
|
|
|
|
|
2020-10-26 15:44:11 +01:00
|
|
|
setNavigation(navigation) {
|
|
|
|
this._navigation = navigation;
|
|
|
|
}
|
|
|
|
|
2020-10-16 12:49:42 +02:00
|
|
|
registerAndStart(path) {
|
|
|
|
this._registrationPromise = (async () => {
|
|
|
|
navigator.serviceWorker.addEventListener("message", this);
|
|
|
|
navigator.serviceWorker.addEventListener("controllerchange", this);
|
|
|
|
this._registration = await navigator.serviceWorker.register(path);
|
2020-10-16 14:52:34 +02:00
|
|
|
await navigator.serviceWorker.ready;
|
|
|
|
this._currentController = navigator.serviceWorker.controller;
|
2021-03-18 19:34:41 +01:00
|
|
|
this._registration.addEventListener("updatefound", this);
|
2020-10-16 12:49:42 +02:00
|
|
|
this._registrationPromise = null;
|
2021-03-18 19:58:50 +01:00
|
|
|
// do we have a new service worker waiting to activate?
|
|
|
|
if (this._registration.waiting && this._registration.active) {
|
2021-03-18 19:34:41 +01:00
|
|
|
this._proposeUpdate();
|
|
|
|
}
|
2020-10-16 12:49:42 +02:00
|
|
|
console.log("Service Worker registered");
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
_onMessage(event) {
|
|
|
|
const {data} = event;
|
|
|
|
const replyTo = data.replyTo;
|
|
|
|
if (replyTo) {
|
|
|
|
const resolve = this._waitingForReply.get(replyTo);
|
|
|
|
if (resolve) {
|
|
|
|
this._waitingForReply.delete(replyTo);
|
|
|
|
resolve(data.payload);
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 15:23:01 +01:00
|
|
|
if (data.type === "hasSessionOpen") {
|
|
|
|
const hasOpen = this._navigation.observe("session").get() === data.payload.sessionId;
|
|
|
|
event.source.postMessage({replyTo: data.id, payload: hasOpen});
|
|
|
|
} else if (data.type === "hasRoomOpen") {
|
|
|
|
const hasSessionOpen = this._navigation.observe("session").get() === data.payload.sessionId;
|
|
|
|
const hasRoomOpen = this._navigation.observe("room").get() === data.payload.roomId;
|
|
|
|
event.source.postMessage({replyTo: data.id, payload: hasSessionOpen && hasRoomOpen});
|
|
|
|
} else if (data.type === "closeSession") {
|
2020-10-16 12:49:42 +02:00
|
|
|
const {sessionId} = data.payload;
|
|
|
|
this._closeSessionIfNeeded(sessionId).finally(() => {
|
|
|
|
event.source.postMessage({replyTo: data.id});
|
|
|
|
});
|
2021-03-18 19:34:41 +01:00
|
|
|
} else if (data.type === "haltRequests") {
|
|
|
|
// this flag is read in fetch.js
|
|
|
|
this.haltRequests = true;
|
|
|
|
event.source.postMessage({replyTo: data.id});
|
2021-03-31 16:51:25 +02:00
|
|
|
} else if (data.type === "openRoom") {
|
|
|
|
this._navigation.push("room", data.payload.roomId);
|
2020-10-16 12:49:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_closeSessionIfNeeded(sessionId) {
|
2020-10-26 15:44:11 +01:00
|
|
|
const currentSession = this._navigation?.path.get("session");
|
2020-10-16 12:49:42 +02:00
|
|
|
if (sessionId && currentSession?.value === sessionId) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const unsubscribe = this._navigation.pathObservable.subscribe(path => {
|
|
|
|
const session = path.get("session");
|
|
|
|
if (!session || session.value !== sessionId) {
|
|
|
|
unsubscribe();
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this._navigation.push("session");
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:34:41 +01:00
|
|
|
async _proposeUpdate() {
|
|
|
|
if (document.hidden) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-24 15:19:10 +01:00
|
|
|
const version = await this._sendAndWaitForReply("version", null, this._registration.waiting);
|
2021-03-18 19:34:41 +01:00
|
|
|
if (confirm(`Version ${version.version} (${version.buildHash}) is available. Reload to apply?`)) {
|
|
|
|
// prevent any fetch requests from going to the service worker
|
|
|
|
// from any client, so that it is not kept active
|
|
|
|
// when calling skipWaiting on the new one
|
|
|
|
await this._sendAndWaitForReply("haltRequests");
|
|
|
|
// only once all requests are blocked, ask the new
|
|
|
|
// service worker to skipWaiting
|
|
|
|
this._send("skipWaiting", null, this._registration.waiting);
|
2020-10-16 12:49:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEvent(event) {
|
|
|
|
switch (event.type) {
|
|
|
|
case "message":
|
|
|
|
this._onMessage(event);
|
|
|
|
break;
|
|
|
|
case "updatefound":
|
|
|
|
this._registration.installing.addEventListener("statechange", this);
|
|
|
|
break;
|
2021-03-18 19:34:41 +01:00
|
|
|
case "statechange": {
|
|
|
|
if (event.target.state === "installed") {
|
|
|
|
this._proposeUpdate();
|
|
|
|
event.target.removeEventListener("statechange", this);
|
|
|
|
}
|
2020-10-16 12:49:42 +02:00
|
|
|
break;
|
2021-03-18 19:34:41 +01:00
|
|
|
}
|
2020-10-16 12:49:42 +02:00
|
|
|
case "controllerchange":
|
2020-10-16 14:52:34 +02:00
|
|
|
if (!this._currentController) {
|
|
|
|
// Clients.claim() in the SW can trigger a controllerchange event
|
|
|
|
// if we had no SW before. This is fine,
|
|
|
|
// and now our requests will be served from the SW.
|
|
|
|
this._currentController = navigator.serviceWorker.controller;
|
|
|
|
} else {
|
|
|
|
// active service worker changed,
|
|
|
|
// refresh, so we can get all assets
|
2021-03-18 19:34:41 +01:00
|
|
|
// (and not only some if we would not refresh)
|
2020-10-16 14:52:34 +02:00
|
|
|
// up to date from it
|
|
|
|
document.location.reload();
|
|
|
|
}
|
2020-10-16 12:49:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _send(type, payload, worker = undefined) {
|
|
|
|
if (this._registrationPromise) {
|
|
|
|
await this._registrationPromise;
|
|
|
|
}
|
|
|
|
if (!worker) {
|
|
|
|
worker = this._registration.active;
|
|
|
|
}
|
|
|
|
worker.postMessage({type, payload});
|
|
|
|
}
|
|
|
|
|
|
|
|
async _sendAndWaitForReply(type, payload, worker = undefined) {
|
|
|
|
if (this._registrationPromise) {
|
|
|
|
await this._registrationPromise;
|
|
|
|
}
|
|
|
|
if (!worker) {
|
|
|
|
worker = this._registration.active;
|
|
|
|
}
|
|
|
|
this._messageIdCounter += 1;
|
|
|
|
const id = this._messageIdCounter;
|
|
|
|
const promise = new Promise(resolve => {
|
|
|
|
this._waitingForReply.set(id, resolve);
|
|
|
|
});
|
|
|
|
worker.postMessage({type, id, payload});
|
|
|
|
return await promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
async checkForUpdate() {
|
|
|
|
if (this._registrationPromise) {
|
|
|
|
await this._registrationPromise;
|
|
|
|
}
|
|
|
|
this._registration.update();
|
|
|
|
}
|
|
|
|
|
2020-10-16 18:06:20 +02:00
|
|
|
get version() {
|
|
|
|
return window.HYDROGEN_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
get buildHash() {
|
|
|
|
return window.HYDROGEN_GLOBAL_HASH;
|
|
|
|
}
|
|
|
|
|
2020-10-16 12:49:42 +02:00
|
|
|
async preventConcurrentSessionAccess(sessionId) {
|
|
|
|
return this._sendAndWaitForReply("closeSession", {sessionId});
|
|
|
|
}
|
2021-03-18 20:41:01 +01:00
|
|
|
|
|
|
|
async getRegistration() {
|
|
|
|
if (this._registrationPromise) {
|
|
|
|
await this._registrationPromise;
|
|
|
|
}
|
|
|
|
return this._registration;
|
|
|
|
}
|
2020-10-16 12:49:42 +02:00
|
|
|
}
|