Sengi-Windows-MacOS-Linux/src/app/services/service-worker.service.ts

54 lines
1.7 KiB
TypeScript
Raw Normal View History

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';
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;
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-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.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$);
// 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-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> {
return this.updates.checkForUpdate();
2020-03-07 06:31:14 +01:00
}
2020-02-26 05:20:58 +01:00
}