docs(service-worker): fix example of manually checking for updates (#28020)
Poll for updates in a way that does not prevent the SW from being registered. Discussed in https://github.com/angular/angular/pull/27332#pullrequestreview-179504620. PR Close #28020
This commit is contained in:

committed by
Andrew Kushnir

parent
89a39bb22a
commit
8c3f98fdbb
@ -1,15 +1,17 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ApplicationRef, Injectable } from '@angular/core';
|
||||
import { SwUpdate } from '@angular/service-worker';
|
||||
|
||||
|
||||
// #docregion sw-check-update
|
||||
import { interval } from 'rxjs';
|
||||
import { concat, interval } from 'rxjs';
|
||||
import { first } from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class CheckForUpdateService {
|
||||
|
||||
constructor(updates: SwUpdate) {
|
||||
interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate());
|
||||
constructor(appRef: ApplicationRef, updates: SwUpdate) {
|
||||
// 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(6 * 60 * 60 * 1000);
|
||||
const everySixHoursOnceAppIsStable$ = concat(appIsStable$, everySixHours$);
|
||||
|
||||
everySixHoursOnceAppIsStable$.subscribe(() => updates.checkForUpdate());
|
||||
}
|
||||
}
|
||||
// #enddocregion sw-check-update
|
||||
|
Reference in New Issue
Block a user