From b2fddef47ef1fa314a19a730c3eb491bd28d5fd3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Wed, 22 Feb 2023 13:15:22 +0100
Subject: [PATCH] fix: update failure due to error in service worker update
 service (VERSION_READY case)

refs #604
---
 .../services/service-worker-update.service.ts | 29 ++++++++++++-------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/src/app/services/service-worker-update.service.ts b/src/app/services/service-worker-update.service.ts
index 4256e92f..47b47c9e 100644
--- a/src/app/services/service-worker-update.service.ts
+++ b/src/app/services/service-worker-update.service.ts
@@ -1,5 +1,5 @@
 import { Injectable } from "@angular/core";
-import { SwUpdate } from '@angular/service-worker';
+import { SwUpdate, VersionEvent } from '@angular/service-worker';
 import { I18nService } from "./internationalisation.service";
 import { NotificationsService } from "./notifications.service";
 
@@ -13,26 +13,33 @@ export class ServiceWorkerUpdateService {
         swUpdate.versionUpdates.subscribe(evt => {
             switch (evt.type) {
                 case 'VERSION_DETECTED':
-                    let ver = evt.version.appData["version"];
-                    let msg = i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_DETECTED", { "ver": ver });
-                    notificationService.notify(msg, 10000);
+                    let ver = (evt as any).version?.appData?.version ?? "<NA>";
+                    console.log("SwUpdate VERSION_DETECTED", ver);
+                    notificationService.notify(i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_DETECTED", { "ver": ver }), 10000);
                     break;
 
                 case 'VERSION_READY':
-                    const newVer = evt.latestVersion.appData["version"];
-                    // const currVer = evt.currentVersion.appData["version"];
-                    msg = i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_READY", { "ver": newVer });
-                    notificationService.notify(msg, 10000);
+                    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);
+
+                    notificationService.notify(i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_READY", { "ver": newVer }), 10000);
+                    // PLANTE si on stocke le message dans une variable !!!!
+                    // const msg = i18nService.localizeText("INFO_SERVICE_WORKER_VERSION_READY", { "ver": newVer });
+                    // notificationService.notify(msg, 10000);
+                    // -> ReferenceError: can't access lexical declaration 'xxx' before initialization
+                    // avec xxx qui varie d'une fois à l'autre !!!
                     break;
 
                 case 'VERSION_INSTALLATION_FAILED':
-                    ver = evt.version.appData["version"];
-                    msg = i18nService.localizeText("ERROR_SERVICE_WORKER_INSTALL_FAILED", { "ver": ver });
-                    notificationService.notify(msg, 10000);
+                    ver = (evt as any).version?.appData?.version ?? "NA";
+                    console.log("SwUpdate 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);
             notificationService.notify("SwUpdate: unrecoverable state. Reason=" + event.reason + ", type=" + event.type, 10000);
         });
     }
-- 
GitLab