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

83 lines
2.2 KiB

// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS206: Consider reworking classes to avoid initClass
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
const Cls = (app.ServiceWorker = class ServiceWorker {
static initClass() {
$.extend(this.prototype, Events);
}
static isEnabled() {
return !!navigator.serviceWorker && app.config.service_worker_enabled;
}
constructor() {
this.onUpdateFound = this.onUpdateFound.bind(this);
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;
1 year ago
return this.registration.update().catch(function () {});
}
updateInBackground() {
1 year ago
if (!this.registration) {
return;
}
this.notifyUpdate = false;
1 year ago
return this.registration.update().catch(function () {});
}
reload() {
return this.updateInBackground().then(() => app.reboot());
}
updateRegistration(registration) {
this.registration = registration;
1 year ago
$.on(this.registration, "updatefound", this.onUpdateFound);
}
onUpdateFound() {
1 year ago
if (this.installingRegistration) {
$.off(this.installingRegistration, "statechange", this.onStateChange());
}
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");
}
}
});
Cls.initClass();