forked from deniszholob/factorio-cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.component.ts
49 lines (44 loc) · 1.26 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Angular
import { Component, OnDestroy } from '@angular/core';
// PWA
import {
SwUpdate,
VersionEvent,
VersionReadyEvent,
} from '@angular/service-worker';
import { filter, map, Subject, takeUntil } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent implements OnDestroy {
private readonly clearSub$ = new Subject<void>();
constructor(private swUpdate: SwUpdate) {
this.subToUpdateApp();
}
ngOnDestroy(): void {
this.clearSub$.next();
this.clearSub$.complete();
}
/** Service worker auto refresh the page if new version available */
private subToUpdateApp(): void {
this.checkServiceWorkerForUpdates().subscribe(() => {
this.swUpdate.activateUpdate().then(() => document.location.reload());
});
}
/** https://angular.io/api/service-worker/SwUpdate */
private checkServiceWorkerForUpdates() {
return this.swUpdate.versionUpdates.pipe(
takeUntil(this.clearSub$),
filter(
(evt: VersionEvent): evt is VersionReadyEvent =>
evt.type === 'VERSION_READY'
),
map((evt: VersionReadyEvent) => ({
type: 'UPDATE_AVAILABLE',
current: evt.currentVersion,
available: evt.latestVersion,
}))
);
}
}