Skip to content

Commit

Permalink
Check for updates every 15 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
sondreb committed Jul 6, 2024
1 parent 841790f commit 01a53c9
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions app/src/app/update.service.ts
Original file line number Diff line number Diff line change
@@ -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();
}
Expand All @@ -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.');
Expand Down

0 comments on commit 01a53c9

Please sign in to comment.