Skip to content

Commit bb66235

Browse files
flolualexeagle
authored andcommitted
feat(example): service worker update handling
1 parent 4d5b9c7 commit bb66235

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

examples/angular/src/app/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ ng_ts_library(
1616
"//src/app/todos",
1717
"//src/app/todos/reducers",
1818
"//src/shared/material",
19+
"@npm//@angular/common",
1920
"@npm//@angular/core",
2021
"@npm//@angular/platform-browser",
2122
"@npm//@angular/router",
2223
"@npm//@angular/service-worker",
2324
"@npm//@ngrx/store",
25+
"@npm//rxjs",
2426
],
2527
)
2628

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import {Component} from '@angular/core';
22

3+
import { ServiceWorkerService } from './service-worker.service';
4+
35
@Component({selector: 'app-component', templateUrl: 'app.component.html'})
46
export class AppComponent {
7+
constructor(private swService: ServiceWorkerService) {
8+
this.swService.launchUpdateCheckingRoutine()
9+
}
510
}

examples/angular/src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {AppRoutingModule} from './app-routing.module';
1111
import {AppComponent} from './app.component';
1212
import {HomeModule} from './home/home';
1313
import {todoReducer} from './todos/reducers/reducers';
14+
import { ServiceWorkerService } from './service-worker.service';
1415

1516
@NgModule({
1617
declarations: [AppComponent],
@@ -20,6 +21,7 @@ import {todoReducer} from './todos/reducers/reducers';
2021
BrowserModule.withServerTransition({ appId: 'angular-bazel-example' }),
2122
ServiceWorkerModule.register('ngsw-worker.js')
2223
],
24+
providers:[ServiceWorkerService],
2325
exports: [AppComponent],
2426
bootstrap: [AppComponent],
2527
})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {ApplicationRef, Injectable, Inject, PLATFORM_ID} from '@angular/core';
2+
import {SwUpdate} from '@angular/service-worker';
3+
import {concat, interval} from 'rxjs';
4+
import {first} from 'rxjs/operators';
5+
import {isPlatformBrowser} from '@angular/common';
6+
7+
@Injectable()
8+
export class ServiceWorkerService {
9+
constructor(
10+
private appRef: ApplicationRef,
11+
private swUpdate: SwUpdate,
12+
@Inject(PLATFORM_ID) private platform: string
13+
) {}
14+
15+
launchUpdateCheckingRoutine(checkIntervaSeconds: number = 6 * 60 * 60) {
16+
if (!this.isAvailable()) return;
17+
18+
const timeInterval$ = concat(
19+
this.appRef.isStable.pipe(first((isStable) => !!isStable)),
20+
interval(checkIntervaSeconds * 1000)
21+
);
22+
23+
timeInterval$.subscribe(() => this.swUpdate.checkForUpdate());
24+
this.swUpdate.available.subscribe(() => this.forceUpdateNow());
25+
}
26+
27+
private forceUpdateNow() {
28+
this.swUpdate.activateUpdate().then(() => document.location.reload());
29+
}
30+
31+
private isAvailable() {
32+
return isPlatformBrowser(this.platform) && this.swUpdate.isEnabled;
33+
}
34+
}

0 commit comments

Comments
 (0)