From c9c24222055e346a9b1bc4867e25eff48b898966 Mon Sep 17 00:00:00 2001 From: Vadim Date: Sat, 24 Feb 2024 19:25:11 +0200 Subject: [PATCH] perf(sw): skip the first update check that requires manual interaction (#820) --- libs/client-service-worker/jest.config.ts | 2 +- .../src/lib/services/service-worker.service.ts | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/libs/client-service-worker/jest.config.ts b/libs/client-service-worker/jest.config.ts index 5f7a7cf3..7323d8a1 100644 --- a/libs/client-service-worker/jest.config.ts +++ b/libs/client-service-worker/jest.config.ts @@ -8,7 +8,7 @@ const config: Config.InitialOptions = { // TODO: bump unit test coverage and remove this override global: { branches: 0, - functions: 16, + functions: 15, lines: 48, statements: 48, }, diff --git a/libs/client-service-worker/src/lib/services/service-worker.service.ts b/libs/client-service-worker/src/lib/services/service-worker.service.ts index 8812432f..8c3c0430 100644 --- a/libs/client-service-worker/src/lib/services/service-worker.service.ts +++ b/libs/client-service-worker/src/lib/services/service-worker.service.ts @@ -2,7 +2,7 @@ import { DOCUMENT } from '@angular/common'; import { ApplicationRef, Inject, Injectable, NgZone } from '@angular/core'; import { MatSnackBar, MatSnackBarConfig, MatSnackBarRef, TextOnlySnackBar } from '@angular/material/snack-bar'; import { SwUpdate } from '@angular/service-worker'; -import { catchError, combineLatest, concat, filter, first, from, interval, map, of, switchMap, tap } from 'rxjs'; +import { catchError, combineLatest, concat, defer, filter, first, from, interval, map, of, skip, switchMap, tap } from 'rxjs'; @Injectable({ providedIn: 'root', @@ -43,7 +43,6 @@ export class AppServiceWorkerService { this.displaySnackBar(`Failed to install app version '${event.version.hash}': ${event.error}`); return null; case 'VERSION_READY': - this.displaySnackBar(`Current app version: ${event.currentVersion.hash}`); this.displaySnackBar(`New app version ready for use: ${event.latestVersion.hash}`); return event; default: @@ -57,19 +56,18 @@ export class AppServiceWorkerService { * Checks for available update and prompts the user to update the application. */ private readonly checkForUpdates$ = concat(this.appStable$, this.updateInterval$).pipe( - switchMap(() => from(this.service.checkForUpdate())), + switchMap(() => from(defer(() => this.service.checkForUpdate()))), catchError((error: Error) => { this.displaySnackBar(`Failed to check for updates. ${error.message}`, void 0); return of(false); }), filter(update => update), + skip(1), tap(() => { this.displaySnackBar( 'There is an application update available. It is recommended to update to avoid unexpected behavior.', 'Update', - { - duration: Number(Infinity), - }, + { duration: Number(Infinity) }, true, ); }),