-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example): service worker update handling
- Loading branch information
Showing
4 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import {Component} from '@angular/core'; | ||
|
||
import { ServiceWorkerService } from './service-worker.service'; | ||
|
||
@Component({selector: 'app-component', templateUrl: 'app.component.html'}) | ||
export class AppComponent { | ||
constructor(private swService: ServiceWorkerService) { | ||
this.swService.launchUpdateCheckingRoutine() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {ApplicationRef, Injectable, Inject, PLATFORM_ID} from '@angular/core'; | ||
import {SwUpdate} from '@angular/service-worker'; | ||
import {concat, interval} from 'rxjs'; | ||
import {first} from 'rxjs/operators'; | ||
import {isPlatformBrowser} from '@angular/common'; | ||
|
||
@Injectable() | ||
export class ServiceWorkerService { | ||
constructor( | ||
private appRef: ApplicationRef, | ||
private swUpdate: SwUpdate, | ||
@Inject(PLATFORM_ID) private platform: string | ||
) {} | ||
|
||
launchUpdateCheckingRoutine(checkIntervaSeconds: number = 6 * 60 * 60) { | ||
if (!this.isAvailable()) return; | ||
|
||
const timeInterval$ = concat( | ||
this.appRef.isStable.pipe(first((isStable) => !!isStable)), | ||
interval(checkIntervaSeconds * 1000) | ||
); | ||
|
||
timeInterval$.subscribe(() => this.swUpdate.checkForUpdate()); | ||
this.swUpdate.available.subscribe(() => this.forceUpdateNow()); | ||
} | ||
|
||
private forceUpdateNow() { | ||
this.swUpdate.activateUpdate().then(() => document.location.reload()); | ||
} | ||
|
||
private isAvailable() { | ||
return isPlatformBrowser(this.platform) && this.swUpdate.isEnabled; | ||
} | ||
} |