Skip to content
Snippets Groups Projects
Commit 37600fab authored by François Grand's avatar François Grand
Browse files

refactor: service worker: display confirmation dialog when application update is ready

refs #604
parent 2b5c5af4
No related branches found
No related tags found
2 merge requests!225Release v4.17.0,!206Resolve "PWA: l'application ne se met pas à jour"
import { Injectable } from "@angular/core";
import { SwUpdate, VersionEvent } from '@angular/service-worker';
import { SwUpdate } from '@angular/service-worker';
import { I18nService } from "./internationalisation.service";
import { NotificationsService } from "./notifications.service";
import { UserConfirmationService } from "./user-confirmation.service";
@Injectable()
export class ServiceWorkerUpdateService {
constructor(
private swUpdate: SwUpdate,
private notificationService: NotificationsService,
private i18nService: I18nService
private i18nService: I18nService,
private userConfirmationService: UserConfirmationService
) {
swUpdate.versionUpdates.subscribe(evt => {
switch (evt.type) {
case 'VERSION_DETECTED':
let ver = (evt as any).version?.appData?.version ?? "<NA>";
console.log("SwUpdate VERSION_DETECTED", ver);
console.log("ServiceWorkerUpdateService: VERSION_DETECTED", ver);
notificationService.notify(i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_DETECTED", { "ver": ver }), 10000);
break;
case 'VERSION_READY':
const currVer = (evt as any).currentVersion?.appData?.version ?? "<NA>";
const newVer = (evt as any).latestVersion?.appData?.version ?? "<NA>";
console.log("SwUpdate VERSION_READY", currVer, newVer);
console.log("ServiceWorkerUpdateService: VERSION_READY", currVer, "->", newVer);
notificationService.notify(i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_READY", { "ver": newVer }), 10000);
// PLANTE si on stocke le message dans une variable !!!!
......@@ -29,17 +31,28 @@ export class ServiceWorkerUpdateService {
// notificationService.notify(msg, 10000);
// -> ReferenceError: can't access lexical declaration 'xxx' before initialization
// avec xxx qui varie d'une fois à l'autre !!!
userConfirmationService.askUserConfirmation("Confirmation",
i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_READY", { "ver": newVer })).then(data => {
if (data["confirm"]) {
console.log("ServiceWorkerUpdateService: application update confirmed");
window.location.reload();
}
else {
console.log("ServiceWorkerUpdateService: application update canceled");
}
});
break;
case 'VERSION_INSTALLATION_FAILED':
ver = (evt as any).version?.appData?.version ?? "NA";
console.log("SwUpdate VERSION_INSTALLATION_FAILED", ver);
console.log("ServiceWorkerUpdateService: VERSION_INSTALLATION_FAILED", ver);
notificationService.notify(i18nService.localizeText("ERROR_SERVICE_WORKER_INSTALL_FAILED", { "ver": ver }), 10000);
break;
}
});
swUpdate.unrecoverable.subscribe(event => {
console.log("SwUpdate.unrecoverable", event.reason, event.type);
console.log("SwUpdate.unrecoverable reason", event.reason, "type", event.type);
notificationService.notify("SwUpdate: unrecoverable state. Reason=" + event.reason + ", type=" + event.type, 10000);
});
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment