Last active
January 9, 2023 15:12
-
-
Save michaeldoye/d48a45c2f3f634cc215c183929406bd0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { SwUpdate } from '@angular/service-worker'; | |
import { interval } from 'rxjs'; | |
import { MatSnackBar } from '@angular/material'; | |
@Injectable() | |
export class WorkerService { | |
constructor(public updates: SwUpdate, public snackBar: MatSnackBar) { | |
// If updates are enabled | |
if (updates.isEnabled) { | |
// poll the service worker to check for updates | |
interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate()); | |
} | |
} | |
// Called from app.components.ts constructor | |
public checkForUpdates() { | |
if (this.updates.isEnabled) { | |
this.updates.available.subscribe(event => { | |
console.log('current version is', event.current); | |
console.log('available version is', event.available); | |
this.promptUser(event); | |
}); | |
this.updates.activated.subscribe(event => { | |
console.log('old version was', event.previous); | |
console.log('new version is', event.current); | |
}); | |
} | |
} | |
// If there is an update, promt the user | |
private promptUser(e): void { | |
if(e.available) { | |
let snackBarRef = this.snackBar.open( | |
'A new version of the dashboard is available', | |
'Update', | |
{horizontalPosition: 'left'} | |
); | |
snackBarRef.onAction().subscribe(() => document.location.reload()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@michaeldoye
is there a way to look for changes in the app content?
In App A I have a vehicle list and each vehicle has a testset. If I add a testset to the vehicle and activate the testset in App B, I want to get informed about a new Testset in Vehicle XY in the App A. But I just get it work, when I change the code, not when the data in the app is changed.
Do you have an idea how i could reach this?