You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/app/serviceworker.js

70 lines
1.7 KiB

app.ServiceWorker = class ServiceWorker extends Events {
static isEnabled() {
return !!navigator.serviceWorker && app.config.service_worker_enabled;
}
constructor() {
super();
this.onStateChange = this.onStateChange.bind(this);
this.registration = null;
this.notifyUpdate = true;
1 year ago
navigator.serviceWorker
.register(app.config.service_worker_path, { scope: "/" })
.then(
1 year ago
(registration) => this.updateRegistration(registration),
(error) => console.error("Could not register service worker:", error),
);
}
update() {
1 year ago
if (!this.registration) {
return;
}
this.notifyUpdate = true;
return this.registration.update().catch(() => {});
}
updateInBackground() {
1 year ago
if (!this.registration) {
return;
}
this.notifyUpdate = false;
return this.registration.update().catch(() => {});
}
reload() {
return this.updateInBackground().then(() => app.reboot());
}
updateRegistration(registration) {
this.registration = registration;
$.on(this.registration, "updatefound", () => this.onUpdateFound());
}
onUpdateFound() {
1 year ago
if (this.installingRegistration) {
$.off(this.installingRegistration, "statechange", this.onStateChange);
1 year ago
}
this.installingRegistration = this.registration.installing;
1 year ago
$.on(this.installingRegistration, "statechange", this.onStateChange);
}
onStateChange() {
1 year ago
if (
this.installingRegistration &&
this.installingRegistration.state === "installed" &&
navigator.serviceWorker.controller
) {
this.installingRegistration = null;
this.onUpdateReady();
}
}
onUpdateReady() {
1 year ago
if (this.notifyUpdate) {
this.trigger("updateready");
}
}
};