2020-02-26 05:20:58 +01:00
|
|
|
import { Injectable, ApplicationRef } from '@angular/core';
|
|
|
|
import { SwUpdate } from '@angular/service-worker';
|
|
|
|
import { interval, concat, BehaviorSubject } from 'rxjs';
|
|
|
|
|
2020-05-01 04:38:16 +02:00
|
|
|
import { NotificationService } from './notification.service';
|
|
|
|
|
2020-02-26 05:20:58 +01:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ServiceWorkerService {
|
2020-02-28 02:25:37 +01:00
|
|
|
private isListening = false;
|
|
|
|
|
2020-05-01 04:38:16 +02:00
|
|
|
constructor(
|
|
|
|
appRef: ApplicationRef,
|
|
|
|
private updates: SwUpdate,
|
|
|
|
private notificationService: NotificationService) {
|
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);
|
|
|
|
|
2020-05-01 04:38:16 +02:00
|
|
|
this.notificationService.notifyRestartNotification('A new version is available!');
|
2020-02-26 05:20:58 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-03-07 17:01:20 +01:00
|
|
|
checkForUpdates(): Promise<void> {
|
2020-05-01 04:38:16 +02:00
|
|
|
return this.updates.checkForUpdate();
|
2020-03-07 06:31:14 +01:00
|
|
|
}
|
2020-02-26 05:20:58 +01:00
|
|
|
}
|