From 79de8979d5a72e824173ef9d7626b50ef1244309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr> Date: Mon, 21 Nov 2022 11:19:37 +0100 Subject: [PATCH] feat: add service worker update service refs #445 --- ngsw-config.json | 3 ++ scripts/deploy-new-stable-version.sh | 9 +++++ src/app/app.component.ts | 4 ++- src/app/app.module.ts | 4 ++- .../services/service-worker-update.service.ts | 34 +++++++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/app/services/service-worker-update.service.ts diff --git a/ngsw-config.json b/ngsw-config.json index a09056dac..2a1566195 100644 --- a/ngsw-config.json +++ b/ngsw-config.json @@ -1,6 +1,9 @@ { "$schema": "./node_modules/@angular/service-worker/config/schema.json", "index": "/index.html", + "appData": { + "version": "4.16.0" + }, "assetGroups": [ { "name": "app", diff --git a/scripts/deploy-new-stable-version.sh b/scripts/deploy-new-stable-version.sh index 5b9168857..e981740c5 100755 --- a/scripts/deploy-new-stable-version.sh +++ b/scripts/deploy-new-stable-version.sh @@ -28,6 +28,11 @@ then exit 2 fi +if [[ ! -f ngsw-config.json ]]; then + echo "Fichier de configuration du service worker ngsw-config.json non trouvé" >&2 + exit 1 +fi + # 1. JaLHyd ################################################################### echo "BUILDING JALHYD" @@ -68,6 +73,10 @@ git push --tags --force echo "BUILDING NGHYD" cd .. +# 2.1 service worker configuration (application version) + +sed -i "/\"version\": \"/s/\": \".*/\": \"$VERSION\"/" ngsw-config.json + # 2.2 update Git repository git checkout master git pull --rebase diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 993c7a33c..6c9375cae 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -33,6 +33,7 @@ import { saveAs } from "file-saver"; import * as XLSX from "xlsx"; import * as pako from "pako"; +import { ServiceWorkerUpdateService } from "./services/service-worker-update.service"; // to be able to check for window.cordova declare let window: any; @@ -90,7 +91,8 @@ export class AppComponent implements OnInit, OnDestroy, Observer { private loadSessionDialog: MatDialog, private confirmCloseCalcDialog: MatDialog, private hotkeysService: HotkeysService, - private matomoTracker: MatomoTracker + private matomoTracker: MatomoTracker, + private serviceWorkerUpdateService: ServiceWorkerUpdateService ) { ServiceFactory.httpService = httpService; ServiceFactory.applicationSetupService = appSetupService; diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 6a0be9102..5be7fc649 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -125,6 +125,7 @@ import { BasinFieldsetContainerComponent } from "./components/basin-fieldset-con import { PrebarrageService } from "./services/prebarrage.service"; import { ServiceWorkerModule } from '@angular/service-worker'; import { environment } from '../environments/environment'; +import { ServiceWorkerUpdateService } from "./services/service-worker-update.service"; const appRoutes: Routes = [ { path: "list/search", component: CalculatorListComponent }, @@ -278,7 +279,8 @@ const appRoutes: Routes = [ { provide: ErrorStateMatcher, useClass: ImmediateErrorStateMatcher - } + }, + ServiceWorkerUpdateService ], schemas: [NO_ERRORS_SCHEMA], bootstrap: [AppComponent] diff --git a/src/app/services/service-worker-update.service.ts b/src/app/services/service-worker-update.service.ts new file mode 100644 index 000000000..0cc5205e4 --- /dev/null +++ b/src/app/services/service-worker-update.service.ts @@ -0,0 +1,34 @@ +import { Injectable } from "@angular/core"; +import { SwUpdate } from '@angular/service-worker'; +import { NotificationsService } from "./notifications.service"; + +@Injectable() +export class ServiceWorkerUpdateService { + constructor( + private swUpdate: SwUpdate, + private notificationService: NotificationsService + ) { + swUpdate.versionUpdates.subscribe(evt => { + switch (evt.type) { + case 'VERSION_DETECTED': + let ver = evt.version.appData["version"]; + console.log(`new version detected ${ver}`); + notificationService.notify(`Downloading new version: ${ver}`, 10000); + break; + + case 'VERSION_READY': + const currVer = evt.currentVersion.appData["version"]; + const newVer = evt.latestVersion.appData["version"]; + console.log(`new version ready ${newVer}, current = ${currVer}`); + notificationService.notify(`New version ready for use: ${newVer}, replacing ${currVer}`, 10000); + break; + + case 'VERSION_INSTALLATION_FAILED': + ver = evt.version.appData["version"]; + console.log(`version install failed ${ver} : ${evt.error}`); + notificationService.notify(`Failed to install version '${ver}': ${evt.error}`, 10000); + break; + } + }); + } +} -- GitLab