From 01a53c9a5e2da775c248725d28c81f416bdb7175 Mon Sep 17 00:00:00 2001 From: SondreB Date: Sat, 6 Jul 2024 13:37:08 +0200 Subject: [PATCH] Check for updates every 15 minutes --- app/src/app/update.service.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/app/src/app/update.service.ts b/app/src/app/update.service.ts index 63a947cd..14777bbb 100644 --- a/app/src/app/update.service.ts +++ b/app/src/app/update.service.ts @@ -1,14 +1,17 @@ -import { Injectable } from '@angular/core'; +import { Injectable, NgZone } from '@angular/core'; import { SwUpdate } from '@angular/service-worker'; -import { Subscription } from 'rxjs'; +import { Subscription, interval } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class NewVersionCheckerService { isNewVersionAvailable: boolean = false; newVersionSubscription?: Subscription; + intervalSource = interval(15 * 60 * 1000); // every 15 mins + intervalSubscription?: Subscription; constructor( - private swUpdate: SwUpdate + private swUpdate: SwUpdate, + private zone: NgZone ) { this.checkForUpdate(); } @@ -21,6 +24,24 @@ export class NewVersionCheckerService { } checkForUpdate(): void { + this.intervalSubscription?.unsubscribe(); + if (!this.swUpdate.isEnabled) { + return; + } + + this.zone.runOutsideAngular(() => { + this.intervalSubscription = this.intervalSource.subscribe(async () => { + try { + this.isNewVersionAvailable = await this.swUpdate.checkForUpdate(); + console.log(this.isNewVersionAvailable ? 'A new version is available.' : 'Already on the latest version.'); + } catch (error) { + console.error('Failed to check for updates:', error); + } + }); + }) + } + + checkForUpdateOnLoad(): void { this.newVersionSubscription?.unsubscribe(); if (!this.swUpdate.isEnabled) { console.log('Service worker updates are disabled for this app.');