Skip to content

Commit 5e4e9b1

Browse files
committed
Monitor network status changes
Based on Pluralsight Progressive Web Apps: Sensor Integrations By Glen Smith, m5: Sensing Online and Offline
1 parent ae7a3c6 commit 5e4e9b1

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

angular/src/app/app.component.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@
330330
</g>
331331
</svg>
332332

333-
<span>{{ title }} app is running!</span>
333+
<span>{{ title }} app is running! Status: {{isOnline ? 'online' : 'offline'}}</span>
334334

335335
<svg id="rocket-smoke" alt="Rocket Ship Smoke" xmlns="http://www.w3.org/2000/svg" width="516.119" height="1083.632" viewBox="0 0 516.119 1083.632">
336336
<path id="Path_40" data-name="Path 40" d="M644.6,141S143.02,215.537,147.049,870.207s342.774,201.755,342.774,201.755S404.659,847.213,388.815,762.2c-27.116-145.51-11.551-384.124,271.9-609.1C671.15,139.365,644.6,141,644.6,141Z" transform="translate(-147.025 -140.939)" fill="#f5f5f5"/>
@@ -462,7 +462,8 @@ <h2>Next Steps</h2>
462462
</a>
463463

464464
<a class="circle-link" title="Augury" href="https://augury.rangle.io/" target="_blank" rel="noopener">
465-
<svg alt="Angular Augury Logo" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="21.81" height="23.447" viewBox="0 0 21.81 23.447">
465+
<svg alt="Angular Augury Logo" xmlns="http://www.w3.org/2000/svg" width="21.81" height="23.447"
466+
viewBox="0 0 21.81 23.447">
466467
<defs>
467468
<clipPath id="clip-path">
468469
<rect id="Rectangle_13" data-name="Rectangle 13" width="10.338" height="10.27" fill="none"/>

angular/src/app/app.component.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { Component, OnInit } from '@angular/core';
22
import { SwPush, SwUpdate } from '@angular/service-worker';
33
import { SubscriberService } from './subscriber.service';
4+
import { fromEvent } from 'rxjs';
45

56
@Component({
67
selector: 'app-root',
78
templateUrl: './app.component.html',
89
styleUrls: ['./app.component.scss']
910
})
1011
export class AppComponent implements OnInit {
12+
isOnline = navigator.onLine;
1113
isServiceWorkerEnabled = false;
1214
subscribed = false;
1315
subscriberId: string;
@@ -32,6 +34,7 @@ export class AppComponent implements OnInit {
3234
// this.requestNotificationPermissions();
3335

3436
this.setUpUpdateActivation();
37+
this.monitorStatus();
3538
}
3639

3740
disablePushNotifications() {
@@ -88,4 +91,9 @@ export class AppComponent implements OnInit {
8891
}
8992
});
9093
}
94+
95+
private monitorStatus() {
96+
fromEvent(window, 'online').subscribe(() => this.isOnline = true);
97+
fromEvent(window, 'offline').subscribe(() => this.isOnline = false);
98+
}
9199
}

0 commit comments

Comments
 (0)