Adds ServiceWorker Service
Adds ServiceWorker Service for Updating PWA
This commit is contained in:
16
src/app/shared/service-worker/update.service.spec.ts
Normal file
16
src/app/shared/service-worker/update.service.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { UpdateService } from './update.service';
|
||||
|
||||
describe('UpdateService', () => {
|
||||
let service: UpdateService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(UpdateService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
26
src/app/shared/service-worker/update.service.ts
Normal file
26
src/app/shared/service-worker/update.service.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import {SwUpdate} from "@angular/service-worker";
|
||||
import {interval} from "rxjs";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UpdateService {
|
||||
|
||||
constructor(private swUpdate: SwUpdate) {
|
||||
if (swUpdate.isEnabled) {
|
||||
interval(6 * 60 * 60).subscribe(() => swUpdate.checkForUpdate()
|
||||
.then(() => console.log('checking for updates')));
|
||||
}
|
||||
}
|
||||
|
||||
public checkForUpdates(): void {
|
||||
this.swUpdate.available.subscribe(event => this.promptUser());
|
||||
}
|
||||
|
||||
private promptUser(): void {
|
||||
console.log('updating to new version');
|
||||
this.swUpdate.activateUpdate().then(() => document.location.reload());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user