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
18 lines
707 B
TypeScript
Executable File
18 lines
707 B
TypeScript
Executable File
import { ApplicationRef, Injectable } from '@angular/core';
|
|
import { SwUpdate } from '@angular/service-worker';
|
|
import { concat, interval } from 'rxjs';
|
|
import { first } from 'rxjs/operators';
|
|
|
|
@Injectable()
|
|
export class CheckForUpdateService {
|
|
|
|
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());
|
|
}
|
|
}
|