2020-02-26 05:20:58 +01:00
|
|
|
import { Injectable, ApplicationRef } from '@angular/core';
|
|
|
|
import { SwUpdate } from '@angular/service-worker';
|
|
|
|
import { first } from 'rxjs/operators';
|
|
|
|
import { interval, concat, BehaviorSubject } from 'rxjs';
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ServiceWorkerService {
|
|
|
|
newAppVersionIsAvailable = new BehaviorSubject<boolean>(false);
|
|
|
|
|
2020-02-28 02:25:37 +01:00
|
|
|
private isListening = false;
|
|
|
|
|
2020-03-07 06:31:14 +01:00
|
|
|
constructor(appRef: ApplicationRef, private updates: SwUpdate) {
|
2020-02-26 05:20:58 +01:00
|
|
|
|
|
|
|
//https://angular.io/guide/service-worker-communications
|
2020-02-27 06:12:18 +01:00
|
|
|
|
2020-02-26 05:20:58 +01:00
|
|
|
updates.available.subscribe(event => {
|
|
|
|
console.log('current version is', event.current);
|
|
|
|
console.log('available version is', event.available);
|
|
|
|
|
|
|
|
this.newAppVersionIsAvailable.next(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Allow the app to stabilize first, before starting polling for updates with `interval()`.
|
2020-02-28 01:59:02 +01:00
|
|
|
// const updateCheckTimer$ = interval(10 * 1000);
|
|
|
|
// const appIsStable$ = appRef.isStable; //.pipe(first(isStable => isStable === true));
|
|
|
|
// const everySixHoursOnceAppIsStable$ = concat(appIsStable$, updateCheckTimer$);
|
2020-02-27 06:12:18 +01:00
|
|
|
// everySixHoursOnceAppIsStable$.subscribe(() => {
|
|
|
|
// updates.checkForUpdate();
|
|
|
|
// });
|
|
|
|
|
2020-02-28 01:59:02 +01:00
|
|
|
const updateCheckTimer$ = interval(6 * 60 * 60 * 1000);
|
2020-02-28 02:25:37 +01:00
|
|
|
|
|
|
|
appRef.isStable.subscribe(() => {
|
|
|
|
if (this.isListening) return;
|
|
|
|
this.isListening = true;
|
|
|
|
|
|
|
|
updateCheckTimer$.subscribe(() => {
|
|
|
|
updates.checkForUpdate();
|
|
|
|
});
|
2020-02-27 06:12:18 +01:00
|
|
|
});
|
2020-02-26 05:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
loadNewAppVersion() {
|
|
|
|
document.location.reload();
|
|
|
|
}
|
2020-03-07 06:31:14 +01:00
|
|
|
|
|
|
|
checkForUpdates(): any {
|
|
|
|
console.log("Check for update");
|
|
|
|
this.updates.checkForUpdate()
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
2020-02-26 05:20:58 +01:00
|
|
|
}
|