added service-worker service

This commit is contained in:
Nicolas Constant 2020-02-25 23:20:58 -05:00
parent 611bccc383
commit 3d828e8a77
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
2 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { ServiceWorkerService } from './service-worker.service';
xdescribe('ServiceWorkerService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: ServiceWorkerService = TestBed.get(ServiceWorkerService);
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,35 @@
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);
constructor(appRef: ApplicationRef, updates: SwUpdate) {
//https://angular.io/guide/service-worker-communications
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()`.
const appIsStable$ = appRef.isStable.pipe(first(isStable => isStable === true));
const everySixHours$ = interval(2 * 60 * 60 * 1000);
const everySixHoursOnceAppIsStable$ = concat(appIsStable$, everySixHours$);
everySixHoursOnceAppIsStable$.subscribe(() => updates.checkForUpdate());
}
loadNewAppVersion() {
document.location.reload();
}
}